How to host a WordPress website in AWS EC2 instance – Part 2
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
This will be a 3-part series where in part 1 we have created an EC2 instance in AWS cloud to host our WordPress website, in the part 2 we will install and configure our WordPress website and in part 3 will complete the setup by enabling DNS and TSL/SSL for our WordPress website.
Install and configure WordPress website in AWS EC2 instance:
Prerequisites:
Step 1: Connect to your EC2 instance.
Step 2: Update software packages in your EC2 instance.
1 2 3 4 5 6 7 8 9 |
## --------------- ## Update packages ## --------------- ## Update software packages sudo yum update -y ## Update LAMP MariaDB and PHP packages for Amazon Linux 2. sudo amazon-linux-extras install -y lamp-mariadb10.2-php7.2 php7.2 |
Step 3: Install Apache webserver in your EC2 instance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
## ------ ## Apache ## ------ ## Install the Apache web server sudo yum install httpd -y ## Start and enable apache webserver sudo systemctl start httpd sudo systemctl enable httpd ## Change file permissions ## Add your user (in this case, ec2-user) to the apache group. sudo usermod -a -G apache ec2-user ## Exit the EC2 connection and connect back exit ## Connect back to your EC2 instance and verify your membership in the apache group groups ## Change the group ownership of /var/www and its contents to the apache group. sudo chown -R ec2-user:apache /var/www ## Change the directory permissions of /var/www and its subdirectories. sudo chmod 2775 /var/www && find /var/www -type d -exec sudo chmod 2775 {} \; ## Change the file permissions of /var/www and its subdirectories. find /var/www -type f -exec sudo chmod 0664 {} \; |
Now try to open your EC2 instance public IP address in a browser. If you see below test page, your webserver installation was successful.
Step 4: Install MySQL (MariaDB) database server in your EC2 instance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
## ------- ## MariaDB ## ------- ## Install the MariaDB database sudo yum install mariadb-server -y ## Start MariaDB database sudo systemctl start mariadb ## Secure MariaDB installation sudo mysql_secure_installation ## Enter current password for root (enter for none): ## Set root password? [Y/n] Y ## New password: ## Re-enter new password: ## Remove anonymous users? [Y/n] Y ## Disallow root login remotely? [Y/n] Y ## Remove test database and access to it? [Y/n] Y ## Reload privilege tables now? [Y/n] Y ## Enable MariaDB database sudo systemctl enable mariadb |
Step 5: Install phpMyAdmin in your EC2 instance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
## ---------- ## phpMyAdmin ## ---------- ## Install the required dependencies. sudo yum install php-mbstring php-xml -y ## Restart apache server sudo systemctl restart httpd ## Restart php-fpm. sudo systemctl restart php-fpm ## Download and install phpMyAdmin (on custom url) cd /var/www/html wget https://www.phpmyadmin.net/downloads/phpMyAdmin-latest-all-languages.tar.gz mkdir dbAdmin && tar -xvzf phpMyAdmin-latest-all-languages.tar.gz -C dbAdmin --strip-components 1 rm phpMyAdmin-latest-all-languages.tar.gz |
Now try to open phpMyAdmin in your browser using http://<ec2_public_ip>/<custom_name>
Note: Do not login to phpMyAdmin yet as its not secure (You can once we enable TSL/SSL in part 3)
Step 6: Install and configure WordPress in your EC2 instance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
## --------- ## WordPress ## --------- ## Download the latest WordPress installation package cd wget https://wordpress.org/latest.tar.gz ## Unzip and unarchive the installation package tar -xzf latest.tar.gz ## Connect to MariaDB instance mysql -u root -p ## Create a database user for WordPress CREATE USER ' ## Create a database for WordPress CREATE DATABASE ` ## Complete database setup GRANT ALL PRIVILEGES ON ` FLUSH PRIVILEGES; exit ## Replace <> with your username, password and database name, for example ## CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword'; ## CREATE DATABASE `mydatabase`; ## GRANT ALL PRIVILEGES ON `mydatabase`.* TO "myuser"@"localhost"; ## FLUSH PRIVILEGES; ## exit ## Copy the wp-config-sample.php file to a file called wp-config.php. cp wordpress/wp-config-sample.php wordpress/wp-config.php ## Edit the wp-config.php file vi wordpress/wp-config.php -------------------------------- define('DB_NAME', ' define('DB_USER', ' define('DB_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('FS_METHOD', ' ---------------------------------- ## Note: The values below are for example purposes only; do not use these values for your installation. ## Use https://api.wordpress.org/secret-key/1.1/salt/ to randomly generate the ## define('DB_NAME', 'mydatabase'); ## define('DB_USER', 'myuser'); ## define('DB_PASSWORD', 'mypassword'); ## define('AUTH_KEY', ' #U$+[RXN8:b^-L 0(WU_+ c+WFkI~c]o]-bHw+)/Aj[wTwSiZ ## define('SECURE_AUTH_KEY', 'Zsz._P=l/|y.Lq)XjlkwS1y5NJ76E6EJ.AV0pCKZZB,*~*r ?6OP$eJT@;+(ndLg'); ## define('LOGGED_IN_KEY', 'ju}qwre3V*+8f_zOWf?{LlGsQ]Ye@2Jh^,8x>)Y |;(^[Iw]Pi+LG#A4R?7N`YB3'); ## define('NONCE_KEY', 'P(g62HeZxEes|LnI^i=H,[XwK9I&[2s|:?0N}VJM%?;v2v]v+;+^9eXUahg@::Cj'); ## define('AUTH_SALT', 'C$DpB4Hj[JK:?{ql`sRVa:{:7yShy(9A@5wg+`JJVb1fk%_-Bx*M4(qc[Qg%JT!h'); ## define('SECURE_AUTH_SALT', 'd!uRu#}+q#{f$Z?Z9uFPG.${+S{n~1M&%@~gL>U>NV ## define('LOGGED_IN_SALT', ';j{00P*owZf)kVD+FVLn-~ >.|Y%Ug4#I^*LVd9QeZ^&XmK|e(76miC+&W&+^0P/'); ## define('NONCE_SALT', '-97r*V/cgxLmp?Zy4zUU4r99QQ_rGs2LTd%P;|_e1tS)8_B/,.6[=UK ## define('FS_METHOD', 'direct'); ## Copy the contents of the wordpress installation directory cp -r wordpress/* /var/www/html/ ## Allow WordPress to use permalinks sudo vi /etc/httpd/conf/httpd.conf ## Find section ## Update AllowOverride None to AllowOverride All ## install the PHP graphics drawing library sudo yum install php-gd -y ## Restart Apache web server sudo systemctl restart httpd |
Step 7: Complete your WordPress setup by opening your public IP in browser.
Bravo, you have successfully installed and configured your first WordPress website in AWS Cloud.
Hope you have enjoyed this 2nd part where we have installed and configured WordPress in our AWS EC2 instance. But if you observe closely still two vital components HTTPS communication and domain name are missing in our website. In the third part we will configure a domain name against our public IP address so that it can be browsed using user friendly name (like http://techaffaire.com) instead of IP address and also enable TSL/SSL for https communication (like https://techaffaire.com)