Blog

WordPress on Amazon Linux 2 with NGINX

| Published by Callan Milne on November 23, 2019 8:05 pm

Getting WordPress up and running on Amazon Linux 2 is super quick and easy. This guide will run you through setting up WordPress on a fresh AWS Linux 2 instance.

This guide assumes you’ve already got a database set-up, or that you’ll be setting one up yourself using something like Amazon RDS.

Once you’ve set up your EC2 instance (I recommend a t3a.small or t3a.medium), proceed to login via SSH and perform the following actions to set up WordPress with Nginx.

Update Packages

[[email protected] ~]$ sudo yum update

Install PHP

[[email protected] ~]$ sudo amazon-linux-extras install php7.3

Install PHP Modules

[[email protected] ~]$ sudo yum install php-gd php-mbstring php-dom php-imagick php-zip

Install & Configure NGINX

NGINX can be installed using amazon-linux-extras on Amazon Linux 2.

[[email protected] ~]$ sudo amazon-linux-extras install nginx1.12

Once Nginx is installed, you can create the configuration file for your WordPress website. I prefer to edit files with vim, but nano is also a good option.

[[email protected] ~]$ sudo vim /etc/nginx/conf.d/yourwebsite.conf

Here is a sample Nginx server configuration file. Make sure you replace yourwebsite.com with you actual website URL.

In this guide we will be setting WordPress up in the directory /var/www/wordpress.

server {
  server_name yourwebsite.com www.yourwebsite.com;
  root /var/www/wordpress;
  index index.php;

  location = /favicon.ico {
    log_not_found off;
    access_log off;
  }

  location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
  }

  location / {
    try_files $uri $uri/ /index.php?$args;
  }

  location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
    expires max;
    log_not_found off;
  }

  location ~ \.php$ {
    include fastcgi.conf;
    fastcgi_intercept_errors on;
    fastcgi_pass unix:/run/php-fpm/www.sock;
  }
}

Download WordPress

First, set up the /var/www directory. You’ll need to create this directory and set the owner to ec2-user while we download WordPress.

[[email protected] ~]$ sudo mkdir /var/www
[[email protected] ~]$ sudo chown ec2-user:ec2-user /var/www
[[email protected] ~]$ cd /var/www

Use wget to fetch the latest WordPress release tar archive. Then extract it to /var/www/wordpress


[[email protected] ~]$ wget https://wordpress.org/latest.tar.gz
[[email protected] ~]$ tar -xf latest.tar.gz

Change WordPress directory to be owned by nginx user so that only the nginx user (or super-user) may write to it.

[[email protected] ~]$ sudo chown -R nginx:nginx /var/www

This step ensures that the nginx user can read/write files. When you are installing or upgrading WordPress, themes, or plugins, or uploading media: the nginx user is the user which will be attempting to write to the file system.

Configure PHP-FPM

PHP-FPM needs to know we’re using the nginx user, not apache, which it uses by default. To make this change, we need to edit the PHP-FPM configuration file for the www process group.

[[email protected] ~]$ sudo vim /etc/php-fpm.d/www.conf

Find the user and group variables and set the value to nginx.

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
; RPM: apache user chosen to provide access to the same directories as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx

Restart NGINX & PHP-FPM

[[email protected] ~]$ sudo service php-fpm restart
[[email protected] ~]$ sudo service nginx restart

See the AWS tutorial on configuring SSL/TLS for instructions on how to set up Certbot.

Access & Set-up Your WordPress Site

Now you should be able to access your WordPress website in your browser. Follow the instructions to finish setting it up.

Please note SSL is a super important security step for your website. I recommend using Certbot. Instructions can be found in the official AWS Documentation (link in additional links below) for how to set this up. Important Note: Make sure you install python2-certbot-nginx and not python2-certbot-apache as the documentation instructs.

Additional Links