In this tutorial I am to going show you how to host WordPress in AWS. There are many ways to host WordPress in AWS. One of the easiest way to host WordPress is using Lightsail and you don’t have to manage server resource . It costs some bucks per month so that you can only have to manage WordPress rather than server. We are not going to use LIGHTSAIL for now. If you have some knowledge on LAMP server then this tutorial is for you. Even you don’t have knowledge about server please follow my instruction , We are going to build our server.
Before heading towards tutorial , I assume you know how to create AWS account, create EC2 instance, change security group , create and associate elastic public IP address and connect to instance via SSH. If you don’t know how to do these things please check my others tutorial or AWS documentation.If you follow my instruction without missing any point then you won’t have any problem creating your WordPress blog.
STEP 1 : Create EC2 instance (FREE TIER)
First create EC2 instance with
. Update security group to allow HTTP SSH and HTTPS request. Create Elastic IP and associate it with the instance you just created. Creating and using Elastic IP address doesn’t cost money but creating Elastic address and not using it will cost money. I strongly recommend you to use elastic address because WordPress uses permalink and if your public IP address changes then you will have problem loading WordPress pages. Now keep your server running and we will now connect your server via SSH.STEP 2: Install LAMP server
Lamp server consist of Linux server, Apache webserver, MySQL database and PHP.
We already have Linux server running. Now let’s create rest of the servers.
Go to you SSH console and first update all the packages so we don’t have to face any packages error later.
[ec2-user ~]$ sudo yum update -y
Now install LAMP server (Apache 2.4 , PHP 7.2 and MySQL 5.7)
[ec2-user ~]$ sudo yum install -y httpd
Install all PHP dependencies that WordPress needs (MySQL for PHP, mbstring and gd ). Trust me these dependencies will make your journey smoother.
[ec2-user ~]$ sudo amazon-linux-extras enable php7.4
[ec2-user ~]$ sudo yum clean metadata
[ec2-user ~]$ sudo yum install -y php php-{pear,cgi,common,curl,mbstring,gd,mysqlnd,gettext,bcmath,json,xml,fpm,intl,zip,imap}
Its time to install mysql . First we have to add mysql to yum repo and and locally install mysql community server
[ec2-user ~]$ sudo yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
[ec2-user ~]$ sudo yum localinstall -y https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
[ec2-user ~]$ sudo yum install -y mysql-community-server
Now start Apache service
[ec2-user ~]$ sudo systemctl start httpd
STEP 3: Change permission
Before we can download and install wordpress in our server we have to change the permission of directories so we can write wordpress files into it.
First we will add ec2-user in the apache group
[ec2-user ~]$ sudo usermod -a -G apache ec2-user
Change the group ownership of /var/www
and its contents to the apache
group.
[ec2-user ~]$ sudo chown -R ec2-user:apache /var/www
Now let’s add group write permissions and set the group ID on future subdirectories, change the directory permissions of /var/www
and its subdirectories and add group write permissions, recursively change the file permissions of /var/www
and its subdirectories
[ec2-user ~]$ find /var/www -type d -exec sudo chmod 2775 {} +
[ec2-user ~]$find /var/www -type f -exec sudo chmod 0664 {} +
STEP 4: Download and unpack wordpress
[ec2-user ~]$ wget https://wordpress.org/latest.tar.gz
Extract downloaded package
[ec2-user ~]$ tar -xzf latest.tar.gz
Now copy all extracted files into /var/www/html folder so that yourdomain.com can starts with wordpress homepage.
[ec2-user ~]$ cp -r wordpress/* /var/www/html/
STEP 5 Database
Now we need database so that wordpress can save all the data.
Start mysql service
[ec2-user ~]$ sudo systemctl start mysqld
First, we need to extract initial root password created by mysql server. Use following code to extract password which is located in mysql error log file.Copy 12 letters password to clipboard.
[ec2-user ~]$ sudo grep ‘temporary password’ /var/log/mysqld.log
Start secure installation and assign root password
[ec2-user ~]$ sudo mysql_secure_installation
- When prompted, type old password and choose new password for the root account.
- Type the current root password.
- Type
Y
to set a password, and type a secure password twice.
- Type
Y
to remove the anonymous user accounts. - Type
Y
to disable the remote root login. - Type
Y
to remove the test database. - Type
Y
to reload the privilege tables and save your changes.
Log into mysql as root and when prompted type password created for root
[ec2-user ~]$ mysql -u root -p
Create New database user wordpress_user
GRANT ALL PRIVILEGES ON *.* TO ‘wordpress_user’@’localhost’ IDENTIFIED BY ‘P@s$w0rd123!’;
FLUSH PRIVILEGES;
Log out of MySQL by typing:
\q
Log in as the new database user you just created:
mysql -u wordpress_user -p
Then, type the new database user’s password and press Enter.
Create the new database:
CREATE DATABASE wordpress_db;
Log out of MySQL by typing:
\q
Step 6 Connect WordPress and Database
Now let’s connect wordpress with database we just created.
First change current location to :
[ec2-user ~]$ cd /var/www/html
[ec2-user ~]$ cp wp-config-sample.php wp-config.php
Now change some database values using nano text editor
[ec2-user ~]$ nano wp-config.php
First add line as below. This will prevent WordPress from asking ftp details in future
define(‘FS_METHOD’,’direct’);
Now Find line starts withdefine(‘DB_NAME……….
Now edit values same as we create database
Database name
define(‘DB_NAME’, ‘wordpress_db‘);
Database username
define(‘DB_USER’, ‘wordpress_user‘);
and database user password ( NOT ROOT PASSWORD)
define(‘DB_PASSWORD’, ‘P@s$w0rd123!’);
save using ctrl + O
exit ctrl +x (save file)
Change Permission to allow WordPress to create directories
[ec2-user ~]$ sudo chown -R ec2-user:apache /var/www/html
[ec2-user ~]$ sudo chmod -R 774 /var/www/html
Enable .htaccess . Change Apache configuration httpd.conf to allow override permalink . If you miss this line , you wont have access to yourdomain.com/path . You can also change this manually using text editor.
[ec2-user ~]$ sudo sed -i ‘/<Directory “\/var\/www\/html”>/,/<\/Directory>/ s/AllowOverride None/AllowOverride all/’ /etc/httpd/conf/httpd.conf
STEP 7 restart apache
Now lets make apache and mysql server starts everytime you start server
[ec2-user ~]$ sudo systemctl enable httpd
[ec2-user ~]$ sudo systemctl enable mysqld
Finally, we can restart apache webserver to reflect all the changes.
[ec2-user ~]$ sudo systemctl restart httpd
Now lets check whether WordPress is working or not. Copy your public IP address to web browser . If you see WordPress installation page then congratulation you have your WordPress installed in your server. If you see database error then please check database details you entered into wp-config.php.
Now create user for WordPress and login . First thing , try out whether you can update plugins and themes. If you can update then all things went good. But if you see ‘cant create directories’ message then change permission of whole /html folder using chmod command. You can also connect your instance with FILEZILLA and changes file permission.
Connect your Instance with your registered domain so user can find you. Secure your Apache server by installing SSL certificate before configuring WordPress Install SSL for free