Looking for good programming challenges?

Use the search below to find our solutions for selected questions!

Setup WordPress on an AWS EC2 Instance

Sharing is caring!

Step 1: create db user

CREATE USER 'blog_user'@'%';
GRANT ALL PRIVILEGES ON blog_db.* To 'blog_user'@'%' IDENTIFIED BY 's3cret';

To change it later you can use:

SET PASSWORD FOR 'blog_user'@'%' = PASSWORD('xxxx');

Step 2: install WordPress
Login into your AWS instance and run the following:

$ cd /var/www/html
$ wget http://wordpress.org/latest.tar.gz
$ tar -xzvf latest.tar.gz
$ mv wordpress blog
$ cd blog
$ mv wp-config-sample.php wp-config.php

Step 3: configure WordPress

$ nano wp-config.php

Make sure it looks as follows:

define( 'DB_NAME', 'blog_db' );

/** MySQL database username */
define( 'DB_USER', 'blog_user' );

/** MySQL database password */
define( 'DB_PASSWORD', 's3cret' );

/** MySQL hostname */
define( 'DB_HOST', 'zzz.xxx.eu-central-2.rds.amazonaws.com:3306' );

Step 4: add .htaccess
In you /var/www/html/blog directory add a file named .htaccess with the following content:
RewriteEngine Off

# BEGIN WordPress
<ifmodule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</ifmodule>

Step 5: configure Apache
Edit /etc/httpd/conf.d/ssl.conf and make sure it includes the following:

<virtualhost *:443>
ServerName blog.com
ServerAlias www.blog.com

DocumentRoot "/var/www/html/blog"

ErrorLog logs/ssl_error_blog_log
TransferLog logs/ssl_access_blog_log
LogLevel warn

SSLEngine on
SSLProtocol all -SSLv3
SSLProxyProtocol all -SSLv3
SSLHonorCipherOrder on
SSLCertificateFile /etc/letsencrypt/live/blog.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/blog.com/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/liveblog.com/chain.pem
</virtualhost>

Step 6: give privileges for upload

$ sudo chown -R apache:apache /var/www/html/blog/

Step 7: set PHP upload limits

$ nano /etc/php.ini

Make sure the following are set:

upload_max_filesize=256mb
post_max_size=256M 
memory_limit=512M

More details about installing Apache on an AWS instance can be found at here. A how-to on installing PHP can be found here.