Complete guide to set up WordPress on Amazon Linux2
Hello there , welcome to this brief yet informative tutorial on installing WordPress on EC2. Most of you who have ended up here are probably thinking of building amazing websites on the cloud. And I know some of you are also looking for help resolving issues while installing WordPress. Whatever the case may be , I want to fully support you in this adventure and help you create a fully working SSL enabled website with a custom domain and custom theme.
I know this title says this guide is for Amazon Linux 2, but this entire sequence of steps is applicable for other distributions as well. The commands might slightly differ, but I will help you understand the fundamentals and empower you to find a solution successfully.
So first things first. Before even listing the prerequisites, I will provide an objective for us to achieve.
Goal : Set up a working WordPress website with a custom theme, https + custom domain , hosted on AWS EC2
Now as part of prerequisites , you will need to buy a custom domain before proceeding.
Components:
a) Infrastructure:
- EC2 instance running Amazon Linux 2
- Elastic IP associated to EC2 [ https://phanichalla.com/blog/2022/04/setting-up-an-ec2-instance-and-assigning-an-elastic-ip-address/ ]
- Security group to allow access over port 80 and 443 for the duration of the setup
b) Software : httpd , certbot, MariaDB
c) Folders: Important folders for this setup are
Web root : /var/www/html
php ini : /etc
httpd conf : /etc/httpd/conf/
Themes : /var/www/html/wp-content/themes
Plugins : /var/www/html/wp-content/plugins
d) References: Read through the document here.
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/hosting-wordpress.html
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/SSL-on-amazon-linux-2.html#letsencrypt
Now that we have clarified some of the things we are going to touch, lets dive in.
Since you are building a website, naturally you must use a web server. httpd will be our webserver.
1) You can use these commands to install the required components first.
sudo yum update -y
sudo amazon-linux-extras install -y lamp-mariadb10.2-php7.2 php7.2
sudo yum install -y httpd mariadb-server
sudo systemctl start httpd
sudo systemctl enable httpd
2) Set up mariadb credentials
Refer to the image at the bottom of the page. Unable to insert SQL into the post.
3)Then download wordpress into a local folder
wget https://wordpress.org/latest.tar.gz
tar -xzf latest.tar.gz
4) Go to this link https://api.wordpress.org/secret-key/1.1/salt/, and copy the contents of the data after the password.
This will be used in the next step. The data is different every time you visit the url.
5) Copy wp-config-sample.php to wp-config.php using this command
cp wordpress/wp-config-sample.php wordpress/wp-config.php
Open the wp-config.php file. Add the following lines after changing the values appropriately.
define( ‘FS_METHOD’, ‘direct’ );
define(‘DB_NAME’, ‘dbname’);
define(‘DB_PASSWORD’, ‘password’);
define(‘AUTH_KEY’, ‘…’);
define(‘SECURE_AUTH_KEY’, ‘…’);
define(‘LOGGED_IN_KEY’, ‘…,’);
define(‘NONCE_KEY’, ‘…’);
define(‘AUTH_SALT’, ‘…);
define(‘SECURE_AUTH_SALT’, ‘…’);
define(‘LOGGED_IN_SALT’, ‘…’);
define(‘NONCE_SALT’, ‘…’);
define(‘WP_HOME’,’http://IP address’);
define(‘WP_SITEURL’,’http://IP address’);
5) Now that the config file is set up reasonably, you can move the contents of the WordPress folder into /var/www/html
cp -r wordpress/* /var/www/html/
6) Set up WordPress by going to your Elastic IP address in the browser and follow the instructions there.
7) Now wordpress setup is complete and DB is also setup and site is functional, but we still need to set up custom domain.
First go to /etc/httpd/conf/httpd.conf and add the following lines after “Listen 80”
DocumentRoot “/var/www/html”
ServerName “domain”
ServerAlias “www.domain.com”Edit the wp-config.php file and update IP to Domain
define(‘WP_HOME’,’http://domain’);
define(‘WP_SITEURL’,’http://domain’);And then after exiting the file, restart httpd for the changes to take effect.
sudo systemctl restart httpd
8) Next, let us install certbot for managing the TLS using letsencrypt certificates.
sudo wget -r –no-parent -A ‘epel-release-*.rpm’ https://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/
sudo rpm -Uvh dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-*.rpm
sudo yum-config-manager –enable epel*
sudo amazon-linux-extras install epel -y
sudo yum install -y certbot python2-certbot-apache
sudo certbot
Follow the next steps by providing your email etc.
Again update domain from http to https in wp-config.php
define(‘WP_HOME’,’https://IP address’);
define(‘WP_SITEURL’,’https://IP address’);
9) Now that the whole site is set up, we need to increase the php max upload file size limit to your desired limit.
Edit /etc/php.ini
Set the upload_max_filesize = 10MRestart php for the changes to take effect
sudo systemctl restart php-fpm.serviceRestart the server to reflect the changes.
sudo systemctl restart httpd
10) So with this, the WordPress site is set up with TLS , custom domain name and fully functional. If you want to install plugins or themes, you can head over to the dashboard and go to Appearances/Themes or Plugins menu on the left and upload the desired zip files.
11) Additionally, the TLS certificates from LetsEncrypt expire every 90 days. So in order to auto renew the certs, use cron job to check twice a day
sudo crontab -e
15 3,16 * * * root certbot renew –no-self-upgrade
sudo systemctl restart crond