In this tutorial, you will learn how to install latest WordPress on EC2 Ubuntu 18.04 instance with Apache2 web server, PHP 7.2 and MySQL server.
WordPress is open-source software and most popular CMS (content management system) platform used by many developers and entrepreneurs around the world to run their blogs and fully functioning websites.
In addition, WordPress is a fully-managed customizable platform with thousands of themes and plugins supported by a large global community.
Also, you can learn how to offload your WordPress images without using plugins:
What will you do
- Installing Apache 2.4, PHP 7.2, Mysql Server for WordPress
- Changing Ownership and Permission
- Installing latest WordPress on Ubuntu Server
- Configure MySQL database for WordPress
- Configure WordPress Security Key
- Creating Apache Virtual Host for WordPress
- Allow Inbound Security Groups on EC2 Instance
- User Interface WordPress Configuration
Requirements
- AWS Account. Create your own AWS Account
- Amazon EC2 Ubuntu 18.04. See tutorial
- A user with sudo privilege command.
To get started, we assume that you have installed the EC2 instance already. If not then learn here how to deploy EC2 instance in AWS console. This guide will show you through step process on how to install latest WordPress on Ubuntu 18.04 server along with Apache 2.4, PHP 7.2, and latest Mysql Server.
Step 1. SSH into your Amazon EC2 instance
Open your favorite Terminal command line and ssh remote to your EC2 server:
ssh -i yourEC2key.pem ubuntu@172.XX.XX.XX
Step 2. Install Apache 2.4 Web Server
If you manage your website hosting using the WordPress platform, you will need to required installing a web server software. In this case, we use an Apache2 web server to manage our WordPress virtual hosting.
To begin the installation of Apache, type the following commands:
sudo add-apt-repository ppa:ondrej/apache2 sudo apt update sudo apt-get install apache2 -y
See: Learn how to Install latest Apache 2.4 Web Server
Apart from this, after the Apache installation completed, continue on step 3.
Step 3. Install PHP 7.2 for WordPress
WordPress has made of using PHP language, which makes PHP very important language in WordPress websites to enable static and dynamic content.
At this time, you can install PHP 7.2 version to build your site on the WordPress platform.
To install latest PHP version, add the PHP (PPA package) below using:
sudo add-apt-repository ppa:ondrej/php
While adding the PPA packages, Press [ENTER] on the prompt to continue.
... # LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php More info: https://launchpad.net/~ondrej/+archive/ubuntu/php Press [ENTER] to continue or Ctrl-c to cancel adding it.
However, you need to run apt update
to your system repository to install available upgrade packages, type command:
sudo apt update
To install PHP 7.2, type command below:
sudo apt install -y php7.2
Apart from this, you need to install the following PHP modules that required for your WordPress application.
sudo apt install -y libapache2-mod-php7.2 php7.2-common php7.2-mbstring php7.2-xmlrpc php7.2-gd php7.2-xml php7.2-mysql php7.2-cli php7.2-zip php7.2-curl php-imagick
After installation completed, you will make some changes from php.ini configuration file, run command:
sudo vim /etc/php/7.2/apache2/php.ini
As a result, Find for the specified list of variables below and set the value same as the following:
file_uploads = On allow_url_fopen = On memory_limit = 256M upload_max_filesize = 64M post_max_size = 64M max_execution_time = 360
Save and close the configuration file.
See: Learn how to install PHP on Ubuntu server.
Step 4. Install MySQL Server for WordPress
WordPress requires a database to store and retrieve every requested data. In this case, we are going to install the latest MySQL Server within your EC2 Ubuntu instance.
To install MySQL Server, type the following command below:
sudo apt update sudo apt-get install mysql-server -y
See: Learn how to install MySQL Server and create new database
After the installation completed, it’s time to install and setup your WordPress application.
Step 5. Install the latest WordPress in Ubuntu 18.04 Server
Download the latest WordPress from the official site using the command below:
cd /tmp && wget https://wordpress.org/latest.tar.gz
Next, extract the compressed files.
tar -zxvf latest.tar.gz
Next, move the entire extracted files to your root document directory, type the command:
sudo mv wordpress /var/www/wordpress
Then open root document directory using:
cd /var/www/wordpress/
Create an .htaccess file, type command:
sudo vim .htaccess
Add following lines into the .htacess file.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Save and close the file.
Next, Rename the wp-config-sample.php using the following command:
sudo mv wp-config-sample.php wp-config.php
Apart from this, continue on step 5 to changing the ownership of your WordPress files and directories.
Step 6. Changing Ownership and Permission for WordPress files
To ensure that your WordPress website works properly, you need to change the ownership of the files and directories into www-data users and groups, so that Apache will able to read and write to your WordPress files and directories.
To change WordPress ownership, type command:
sudo chown -R www-data:ubuntu /var/www/wordpress
Next, Change the right permission for the particular files and directories, type command:
sudo find /var/www/wordpress/ -type d -exec chmod 755 {} \; sudo find /var/www/wordpress/ -type f -exec chmod 644 {} \;
Apart from this, set the following important files to chmod 600
so that only the owner can fully read and write access to these files.
sudo chmod 600 /var/www/wordpress/wp-config.php sudo chmod 600 /var/www/wordpress/.htaccess
However, If you want to verify the ownership and permission for your WordPress files and directories, type command:
ls -lah
The output similarly to this:
Step 7. Setup MySQL Database in WordPress Configuration File
In this case, we are going to connect our WordPress website on the MySQL database. To do this, open the WordPress configuration file, type command:
sudo vim /var/www/wordpress/wp-config.php
Therefore, look for the MySQL database section and define the following values, similar to this:
// ** MySQL settings - You can get this info from your web host ** // /** The name of the database for WordPress */ define( 'DB_NAME', 'database_name_here' ); /** MySQL database username */ define( 'DB_USER', 'username_here' ); /** MySQL database password */ define( 'DB_PASSWORD', 'password_here' ); /** MySQL hostname */ define( 'DB_HOST', 'localhost' ); /** Database Charset to use in creating database tables. */ define( 'DB_CHARSET', 'utf8' );
After modifying the database connection, then save and close the file.
Step 8. Setup WordPress Security Key
WordPress Security Key is a set of random variables that improve encryption of information stored in the user’s cookies.
To grab your own WordPress Security Key
, type following command on your terminal console:
curl -s https://api.wordpress.org/secret-key/1.1/salt/
The output will response unique values, similar to this:
Note: Generate your own WordPress security key
and never copy the value shown in the example above.
Apart from this, open the WordPress configuration file, type command:
sudo vim /var/www/wordpress/wp-config.php
As a result, find the following existing format and replace your generated security key values:
define('AUTH_KEY', 'your unique phrase here'); define('SECURE_AUTH_KEY', 'your unique phrase here'); define('LOGGED_IN_KEY', 'your unique phrase here'); define('NONCE_KEY', 'your unique phrase here'); define('AUTH_SALT', 'your unique phrase here'); define('SECURE_AUTH_SALT', 'your unique phrase here'); define('LOGGED_IN_SALT', 'your unique phrase here'); define('NONCE_SALT', 'your unique phrase here');
Then, Save and close the file.
Step 9. Creating Apache Virtual Host for WordPress
To add an Apache virtual host for WordPress, Go to the Apache2 virtual configuration directory, type command:
cd /etc/apache2/sites-available
Next, Disable the default configuration file, type command:
sudo a2dissite 000-default.conf
Then reload the Apache2 configuration file.
sudo systemctl reload apache2
Now, Create a new WordPress virtual host configuration file, type command:
sudo vim wordpress.conf
Copy the following virtual host configuration file below and change the domain name and also the path location for root document directory.
Save and close the file.
Next, Enable the virtual host configuration file to available on public access, type command:
sudo a2ensite wordpress.conf
Also, Enable the Apache rewrite module to enable .htaccess file on rewrite modules , type command:
sudo a2enmod rewrite
Note: The rewrite module can be used to redirect one URL to another URL. In order to provides a rule-based rewriting engine to rewrite requested URLs on the fly.
Next, Verify your WordPress virtual host configuration using:
sudo apache2ctl configtest
The output similar to this:
Syntax OK
Therefore, If everything is fine. Reload your Apache2 service to implement the changes, type command:
sudo service apache2 restart
Above all, this section completes the backend setup.
Step 10. Allow Inbound Security Groups on EC2 Instance
In-order to public your website to available anywhere in the world then you will have to configure the Security Group
in the EC2 console. Login to your AWS account and open the Amazon EC2 console at https://console.aws.amazon.com/ec2/ . In the left navigation pane, select the Security Group
under Network & Security tab. Create new Security Group or modify the existing one and make sure the following ports (80, 443 & 22) are allowed from inbound rules. That’s it.
Step 11. User Interface WordPress Configuration
Last but not least, Grab the public IP address of your EC2 Ubuntu server using:
curl http://checkip.amazonaws.com
Copy the public IP address and paste it to your favorite web browser.
http://52.XX.XX.XX
Therefore, you will be redirected to WordPress configuration page and then you need to choose your website language.
Next, provide the required following information such as Site Title
, Email
, Username
and Password
. Never check the boxes for discourage search engines from indexing this site.
On the next page, you will be taken to the Login console.
While signed into the Login console, you will be redirected to the welcome page WordPress dashboard. As a result, it looks like this:
To install an SSL Certificate for Free using Let’s Encrypt use the following guide below:
That’s all.
Finally, you have successfully completed WordPress installation on your EC2 server. Now you can begin to install your WordPress themes. In addition to this, you can learn how to automate MySQLdump backup move to S3 bucket and archive after 30 days on Glacier.
If furthermore needed assistance from installing WordPress, please do not hesitate to contact me.
I hope this tutorial helped you and feel free to comment section below for more suggestions.
Tags: apache2, aws, linux, mysql, php, ubuntu, ubuntu 18.04, wordpress
Very helpful and detailed mate, thank you
Super helpful post.
I have an issue though. Even though i set all the file permissions as you described, the site does not display any images, only text.
The permissions look like this.
drwxr-xr-x 5 www-data ubuntu 4.0K Jun 12 22:27 .
drwxr-xr-x 4 root root 4.0K Jun 12 21:23 ..
-rw-r–r– 1 www-data ubuntu 461 Jun 12 21:32 .htaccess
-rw-r–r– 1 www-data ubuntu 405 Feb 6 06:33 index.php
-rw-r–r– 1 www-data ubuntu 20K Feb 12 11:54 license.txt
-rw-r–r– 1 www-data ubuntu 7.2K Jan 10 14:05 readme.html
-rw-r–r– 1 www-data ubuntu 6.8K Feb 6 06:33 wp-activate.php
drwxr-xr-x 9 www-data ubuntu 4.0K Jun 10 21:48 wp-admin
-rw-r–r– 1 www-data ubuntu 351 Feb 6 06:33 wp-blog-header.php
-rw-r–r– 1 www-data ubuntu 2.3K Jun 2 20:26 wp-comments-post.php
-rw——- 1 www-data ubuntu 2.9K Jun 12 21:26 wp-config.php
drwxr-xr-x 6 www-data ubuntu 4.0K Jun 12 22:00 wp-content
-rw-r–r– 1 www-data ubuntu 3.9K Feb 6 06:33 wp-cron.php
drwxr-xr-x 21 www-data ubuntu 12K Jun 10 21:48 wp-includes
-rw-r–r– 1 www-data ubuntu 2.5K Feb 6 06:33 wp-links-opml.php
-rw-r–r– 1 www-data ubuntu 3.3K Feb 6 06:33 wp-load.php
-rw-r–r– 1 www-data ubuntu 47K Feb 10 03:50 wp-login.php
-rw-r–r– 1 www-data ubuntu 8.4K Apr 14 11:34 wp-mail.php
-rw-r–r– 1 www-data ubuntu 19K Apr 10 03:59 wp-settings.php
-rw-r–r– 1 www-data ubuntu 31K Feb 6 06:33 wp-signup.php
-rw-r–r– 1 www-data ubuntu 4.7K Feb 6 06:33 wp-trackback.php
-rw-r–r– 1 www-data ubuntu 3.1K Feb 6 06:33 xmlrpc.php
Hi Mirza, thank you for showing interest in my blog.
Try this solution:
1. set the following file into right permission using:
Open your wp path directory at cd /var/www/mywebsite.com
Let Apache2 be owner
sudo chown www-data:www-data -R *
Change directory permissions rwxr-xr-x
sudo find . -type d -exec chmod 755 {} \;
Change file permissions rw-r–r–
sudo find . -type f -exec chmod 644 {} \;
2. make sure you enable the rewrite module:
sudo a2enmod rewrite
3. Inspect element on your browser, see an error in the console tab and network.
Hi,
I have tried, still no luck. I even tried setting all rw to full, chmod 777, but it still behaves the same.
Rewrite module is enabled.
Try to install this PHP module, maybe you missed these:
sudo apt install -y libapache2-mod-php7.2 php7.2-common php7.2-mbstring php7.2-xmlrpc php7.2-gd php7.2-xml php7.2-mysql php7.2-cli php7.2-zip php7.2-curl php-imagick
Your copy button doesn’t work
result : cd /tmp && wget https://wordpress.org/latest.tar.gz
Hi Sam,
Sorry to hear you that, can you try it again it’s now working in my end. Thanks for reporting.
How I map my namecheap domain in this wordpress.
Hey, I recommend this guide http://gregblogs.com/tlt-associate-a-namecheap-domain-with-an-amazon-ec2-instance/
my authentication is failing again and again after using this command sudo a2enmod rewrite.
Hi muskan,
You need to enable the rewrite module for you to able to run the .htaccess file in your wordpress.
Can you copy & paste the error here?
ubuntu@ip-172-31-50-246:/etc/apache2/sites-available$ sudo a2ensite wordpress.conf
Enabling site wordpress.
To activate the new configuration, you need to run:
systemctl reload apache2
ubuntu@ip-172-31-50-246:/etc/apache2/sites-available$ sudo systemctl reload apache2
ubuntu@ip-172-31-50-246:/etc/apache2/sites-available$ sudo a2enmod rewrite
Enabling module rewrite.
To activate the new configuration, you need to run:
systemctl restart apache2
ubuntu@ip-172-31-50-246:/etc/apache2/sites-available$ sudo systemctl reload apache2
ubuntu@ip-172-31-50-246:/etc/apache2/sites-available$ sudo apache2ctl configtestSyntax OK
ubuntu@ip-172-31-50-246:/etc/apache2/sites-available$ sudo service apache2 restart
ubuntu@ip-172-31-50-246:/etc/apache2/sites-available$ curl http://checkip.amazonaws.com
100.25.220.45
ubuntu@ip-172-31-50-246:/etc/apache2/sites-available$ http://100.25.220.45
-bash: http://100.25.220.45: No such file or directory
ubuntu@ip-172-31-50-246:/etc/apache2/sites-available$ https://100.25.220.45
-bash: https://100.25.220.45: No such file or directory
the problem is – in prompt its showing bash error but when we enter the url in browser its working
it means it s done?
I see, use Curl command if you need to run the URL on terminal. Just like this:
curl -I https://100.25.220.45
OR
curl https://100.25.220.45
If command above response HTTP CODE “200” meaning your website is OK.
Hello Sir,
thanks for writing such a great post. you save my job..
Awesome, YW 🙂
Thanks for this post, it’s quite detailed a easy to follow (if you know AWS & vim)
Everything to step 9, ‘syntax Ok’ was fine. The IP doesn’t load on my browser or respond with curl.
I’m not sure if it’s a AWS VPC/EC2 issue or the Ubuntu configurations. Any advice trouble shooting at this stage?
Hi Trevor, I saw your message in my inbox and thank you so much for following in my blog and sorry to hear your issue. Upon reviewing my entire blog, I have missed the security group tutorial. So I added down below see on step 10.
How to connect a domain?
Which domain provider currently you are using?
ServerAdmin admin@manassingh.in
DocumentRoot /var/www/wordpress
ServerName
ServerAlias
Options +FollowSymlinks
AllowOverride All
Require all granted
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
ProxyPass http://
ProxyPassReverse http://
it’s not working and stop the apache2 server also.
as you can see that I’m trying to connect my server with domain name.
Do you have video above of the step?
I am beginner. so i don’t know how to do each step.
And I used window. So I used Mobaxterm.
And now I testing AWS EC2.