Setup WordPress with Nginx on Amazon Linux 2

Posted on 212 views

WordPress is an open source content management system (CMS) written entirely in PHP. It uses MySQL or MariaDB database server to store its data and website configurations. WordPress is designed to be modular with template system which enables developers to design their own themes and plugins. In this article we will be doing installation of PHP, MySQL and WordPress on Amazon Linux 2 powered by Nginx Web server.

You should have a running Amazon Linux 2 server with Bash shell access. A user with sudo privileges is required for escalation to root during installation of packages and modification of configuration files.

The minimum requirements for this setup are:

  • Amazon Linux 2 server
  • Memory: 512 MB
  • vCPU: 1
  • Disk Space: 1GB
  • Internet access
  • Domain name for your website

Once you have a Server running you need to configure your DNS by creating an A record pointing to your Amazon Linux 2 public IP address. My test domain used in this guide is mysite.computingpost.com pointing to 35.20.11.34

setup-wordpress-website-amazon-linux-01

Step 1: Update Amazon Linux Server

As always we work on a server that has been updated to ensure patches are applied to guarantee server security.

sudo yum -y update

Wait for the updates to be applied then reboot your server.

sudo systemctl reboot

Login back once the server is up.

$ ssh [email protected]
Last login: Wed Aug 12 17:07:32 2020 from 10.0.2.2

       __|  __|_  )
       _|  (     /   Amazon Linux 2 AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-2/

Step 2: Install PHP on Amazon Linux

The versions of PHP packages available on Amazon Linux extras repository are more than one. You can list them with the command below.

$ sudo  amazon-linux-extras | grep php
NOTE: The livepatch extra is in public preview, not meant for production use

 15  php7.2                   available    \
 17  lamp-mariadb10.2-php7.2  available    \
 31  php7.3                   available    \
 42  php7.4                   available    [ =stable ]

In this guide we’ll go with the latest stable release of PHP. Enable PHP 7.4 topic.

sudo amazon-linux-extras enable php7.4

Then install all required PHP extensions on Amazon Linux 2 server.

sudo yum clean metadata
sudo yum install php php-cli,fpm,pear,cgi,common,curl,mbstring,gd,mysqlnd,gettext,bcmath,json,xml,intl,zip,imap

Accept installation prompt.

Dependencies Resolved

==================================================================================================================================================================
 Package                                   Arch                         Version                                     Repository                               Size
==================================================================================================================================================================
Installing:
 php                                       x86_64                       7.4.7-1.amzn2                               amzn2extra-php7.4                       3.2 M
 php-bcmath                                x86_64                       7.4.7-1.amzn2                               amzn2extra-php7.4                        68 k
 php-cli                                   x86_64                       7.4.7-1.amzn2                               amzn2extra-php7.4                       4.9 M
 php-common                                x86_64                       7.4.7-1.amzn2                               amzn2extra-php7.4                       1.1 M
 php-fpm                                   x86_64                       7.4.7-1.amzn2                               amzn2extra-php7.4                       1.7 M
 php-gd                                    x86_64                       7.4.7-1.amzn2                               amzn2extra-php7.4                       180 k
 php-intl                                  x86_64                       7.4.7-1.amzn2                               amzn2extra-php7.4                       221 k
 php-json                                  x86_64                       7.4.7-1.amzn2                               amzn2extra-php7.4                        70 k
 php-mbstring                              x86_64                       7.4.7-1.amzn2                               amzn2extra-php7.4                       513 k
 php-mysqlnd                               x86_64                       7.4.7-1.amzn2                               amzn2extra-php7.4                       238 k
 php-pear                                  noarch                       1:1.10.7-3.amzn2.0.1                        amzn2-core                              354 k
 php-xml                                   x86_64                       7.4.7-1.amzn2                               amzn2extra-php7.4                       198 k
Installing for dependencies:
 apr                                       x86_64                       1.6.3-5.amzn2.0.2                           amzn2-core                              118 k
 apr-util                                  x86_64                       1.6.1-5.amzn2.0.2                           amzn2-core                               99 k
 apr-util-bdb                              x86_64                       1.6.1-5.amzn2.0.2                           amzn2-core                               19 k
 generic-logos-httpd                       noarch                       18.0.0-4.amzn2                              amzn2-core                               19 k
 httpd                                     x86_64                       2.4.43-1.amzn2                              amzn2-core                              1.3 M
 httpd-filesystem                          noarch                       2.4.43-1.amzn2                              amzn2-core                               23 k
 httpd-tools                               x86_64                       2.4.43-1.amzn2                              amzn2-core                               87 k
 libxslt                                   x86_64                       1.1.28-5.amzn2.0.2                          amzn2-core                              243 k
 libzip                                    x86_64                       1.3.2-1.amzn2.0.1                           amzn2-core                               62 k
 mailcap                                   noarch                       2.1.41-2.amzn2                              amzn2-core                               31 k
 mod_http2                                 x86_64                       1.15.3-2.amzn2                              amzn2-core                              146 k
 oniguruma                                 x86_64                       5.9.6-1.amzn2.0.3                           amzn2-core                              127 k
 php-pdo                                   x86_64                       7.4.7-1.amzn2                               amzn2extra-php7.4                       134 k
 php-process                               x86_64                       7.4.7-1.amzn2                               amzn2extra-php7.4                        88 k

Transaction Summary
==================================================================================================================================================================
Install  12 Packages (+14 Dependent packages)

Total download size: 15 M
Installed size: 61 M
Is this ok [y/d/N]: y

Verify default PHP version in your system.

$ php --version
PHP 7.4.7 (cli) (built: Jul  2 2020 23:17:59) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

Step 3: Install Nginx web server

As stated earlier we’ll be using Nginx web server to host our WordPress website.

Enable nginx topic.

sudo amazon-linux-extras enable nginx1

Install Nginx package from extras repository.

sudo yum clean metadata
sudo yum -y install nginx

Start and enable nginx service.

sudo systemctl enable --now nginx

The service should show status as running

$ systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
  Drop-In: /usr/lib/systemd/system/nginx.service.d
           └─php-fpm.conf
   Active: active (running) since Wed 2020-08-12 19:15:09 UTC; 6s ago
  Process: 3358 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 3355 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 3351 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 3360 (nginx)
   CGroup: /system.slice/nginx.service
           ├─3360 nginx: master process /usr/sbin/nginx
           └─3361 nginx: worker process

Aug 12 19:15:09 amazon-linux systemd[1]: Starting The nginx HTTP and reverse proxy server...
Aug 12 19:15:09 amazon-linux nginx[3355]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Aug 12 19:15:09 amazon-linux nginx[3355]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Aug 12 19:15:09 amazon-linux systemd[1]: Failed to read PID from file /run/nginx.pid: Invalid argument
Aug 12 19:15:09 amazon-linux systemd[1]: Started The nginx HTTP and reverse proxy server

If you have firewalld service active, allow http and https services.

sudo firewall-cmd --add-service=http,https --permanent
sudo firewall-cmd --reload

List allowed services.

$ sudo firewall-cmd --list-all
public
  target: default
  icmp-block-inversion: no
  interfaces:
  sources:
  services: ssh dhcpv6-client http https
  ports:
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

Step 4: Configure PHP FPM Service

As we’ll be using Nginx web server, we need to configure FastCGI Process Manager (FPM) service. You can check cool features of FPM implementation.

By default it is set to use apache user and group and we’ll need to update to nginx

$ sudo vim /etc/php-fpm.d/www.conf
user = nginx
group = nginx

Make sure it is listening on unix socket to accept FastCGI requests.

listen = /run/php-fpm/www.sock

Let’s also set permissions for unix socket:

listen.acl_users = apache,nginx

Set how the process manager will control the number of child processes to ondemand to save on memory utilization.

pm = ondemand

When done start and enable php-fpm service.

sudo systemctl enable php-fpm
sudo systemctl restart php-fpm

Check the service status with systemctl command:

$ systemctl status php-fpm
● php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2020-08-12 19:20:53 UTC; 8s ago
 Main PID: 3657 (php-fpm)
   Status: "Ready to handle connections"
   CGroup: /system.slice/php-fpm.service
           └─3657 php-fpm: master process (/etc/php-fpm.conf)

Aug 12 19:20:53 amazon-linux systemd[1]: Starting The PHP FastCGI Process Manager...
Aug 12 19:20:53 amazon-linux systemd[1]: Started The PHP FastCGI Process Manager.

Step 5: Install and Configure MariaDB / MySQL Database server.

You can use either MySQL or MariaDB Database server. Refer to the guides below for installation steps.

Install MySQL 8 on Amazon Linux 2

Install MariaDB 10.5 on Amazon Linux 2

With database server installed and started, create a user and database for WordPress website.

$ mysql -u root -p
CREATE DATABASE mysite;
CREATE USER "mysiteuser"@"localhost" IDENTIFIED BY "[email protected]#";
GRANT ALL PRIVILEGES ON mysite.* TO "mysiteuser"@"localhost";
FLUSH PRIVILEGES;
\q

Where:

  • mysite is to be replaced with the name of database to be created.
  • mysiteuser – Name of database user with access to mysite database
  • [email protected]# – Database user password
  • Access granted to localhost user. For remote replace localhost with application server IP address.

Test database connection:

$ mysql -u mysiteuser -p'[email protected]#';
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.21 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysite             |
+--------------------+
2 rows in set (0.01 sec)

mysql> QUIT
Bye

Step 6: Install WordPress 5.5 on Amazon Linux 2

With PHP and Nginx installed we should be able to download the latest release of WordPress to our local server and configure our first website.

Install wget if now available already.

sudo yum -y install wget

Download the latest release of WordPress.

wget wordpress.org/latest.tar.gz

Extract downloaded package.

tar xvf latest.tar.gz

Move wordpress folder to /var/www directory.

sudo mv wordpress /var/www/mysite.computingpost.com

Create WordPress configuration file from example configuration template.

cd /var/www/mysite.computingpost.com
sudo cp wp-config-sample.php wp-config.php

Edit the configuration file to set database connection details.

$ sudo vim wp-config.php

define( 'DB_NAME', 'mysite' );
define( 'DB_USER', 'mysiteuser' );
define( 'DB_PASSWORD', '[email protected]#' );

Step 7: Create Nginx Configuration file for WordPress

Let’s create a new Nginx configuration file for our website.

sudo vim /etc/nginx/conf.d/mysite.conf

This a basic site configuration file which can be improved for advanced settings.

server 
  listen 80;
  server_name mysite.computingpost.com www.mysite.computingpost.com;
  root /var/www/mysite.computingpost.com;
  access_log /var/log/nginx/mysite_access.log;
  error_log /var/log/nginx/mysite_error.log; 

  location / 
        index index.php index.html;
        try_files $uri $uri/ /index.php?$args;
  
  charset utf-8;
  gzip  on;
  location ~ /\. 
        access_log                      off;
        log_not_found                   off;
        deny                            all;
  
 
  location = /robots.txt 
               allow all;
               log_not_found off;
               access_log off;
  
  location ~* /(?:uploads

Verify configuration file syntax:

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Set web directory permissions:

sudo chown -R nginx:nginx /var/www/mysite.computingpost.com

Restart nginx service.

sudo systemctl restart nginx

Access your website domain on a browser to complete WordPress setup.

How-to-install-Nginx-PHP-FPM-and-Wordpress-on-Linux-Mint-20

Provide site title and admin username, email and password.

setup-wordpress-amazon-linux-01

Hit the login button after installation.

setup-wordpress-amazon-linux-02

Use username and password that you set in previous step.

setup-wordpress-amazon-linux-03

There you have WordPress dashboard.

setup-wordpress-amazon-linux-04-2048x1088

You’re good to proceed with Website design and installation of plugins which provide additional features suiting your use cases.

coffee

Gravatar Image
A systems engineer with excellent skills in systems administration, cloud computing, systems deployment, virtualization, containers, and a certified ethical hacker.