2 Install WordPress with Nginx on Ubuntu 18.04
Mehran Dehghanian edited this page 4 years ago

Update:

Not really reliable due to the slow plugin load and php response, might be VPS problem on nginx. haven't figured it's out yet.

Update your Ubuntu 18.04

Update your Ubuntu 18.04 Before you start installing NGINX, it is always recommended to upgrade your Ubuntu 18.04 to the latest. The following apt-get commands will do it for you.

apt-get update 
apt-get upgrade

The first command will update the list of available packages and their versions and the second one will actually install the newer versions of the packages that you have.

Install WordPress with NGINX on Ubuntu

Step 1: Install NGINX

NGINX is available in the default repositories of Ubuntu and can be installed with a single line command as shown below.

apt-get install nginx

Step 2: Install MariaDB

MariaDB is available in the default repository of Ubuntu. It is also possible to install it from the separate MariaDB repository. But we will stick to install it from default repository of Ubuntu. Issue the following commands from the terminal to install it and optionally you can run mysql_secure_installation to make it secure.

 apt-get install mariadb-server 
 systemctl enable mariadb.service
 mysql_secure_installation

The default password for MariaDB root user is blank. To update the password of the root user, get the MySQL prompt and update the password by issuing following command from MySQL shell.

$ mysql -u root -p
MariaDB [(none)]> use mysql;
MariaDB [mysql]> update user SET PASSWORD=PASSWORD("Passw0rd!") WHERE USER='root';

The installation of MariaDB is complete in your Ubuntu 18.04 system.

Step 3: Install PHP

The latest version of PHP (7.2) is available in the repositories of Ubuntu 18.04 and is the default candidate for installation so simply run the following command in terminal to install it.

apt-get install php7.2 php7.2-cli php7.2-fpm php7.2-mysql php7.2-json php7.2-opcache php7.2-mbstring php7.2-xml php7.2-gd php7.2-curl

Apart from installing php7.2, the above apt-get command also installs few other packages as well like MySQL, XML, Curl and GD packages and makes sure that your WordPress site can interact with the database, support for XMLRPC, and also to crop and resize images automatically.

Further, the php-fpm (Fast process manager) package is needed by NGINX to process PHP pages of your WordPress installation. Remember that FPM service will run automatically once the installation of PHP is over.

Step 4: Create WordPress Database

Once the MariaDB is installed and configured in your server, create a user and a database especially for WordPress installation. To do that, log in to the MariaDB server using mysql -u root -p command and complete the steps as described below.

$ mysql -u root -p
Enter password:

MariaDB [mysql]> CREATE DATABASE wordpress_db;
Query OK, 1 row affected (0.00 sec)

MariaDB [mysql]> GRANT ALL ON wordpress_db.* TO 'wpuser'@'localhost' IDENTIFIED BY 'Passw0rd!' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)

MariaDB [mysql]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

MariaDB [mysql]> exit

Step 5: Configure NGINX for WordPress

Let us now proceed with configuring NGINX server blocks to serve your WordPress domain. To start with, create the root folder for your WordPress installation.

mkdir -p /var/www/html/wordpress/public_html

To create NGINX server block for your WordPress domain, navigate to the /etc/nginx/sites-available folder. This is the default location for NGINX server blocks. Use your favorite editor to create a configuration file for NGINX server block and edit it like below.

cd /etc/nginx/sites-available
cat wordpress.conf
server {
            listen 80;
            root /var/www/html/wordpress/public_html;
            index index.php index.html;
            server_name SUBDOMAIN.DOMAIN.TLD;

	    access_log /var/log/nginx/SUBDOMAIN.access.log;
    	    error_log /var/log/nginx/SUBDOMAIN.error.log;

            location / {
                         try_files $uri $uri/ =404;
            }

            location ~ \.php$ {
                         include snippets/fastcgi-php.conf;
                         fastcgi_pass unix:/run/php/php7.2-fpm.sock;
            }
            
            location ~ /\.ht {
                         deny all;
            }

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

            location = /robots.txt {
                         allow all;
                         log_not_found off;
                         access_log off;
           }
       
            location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
                         expires max;
                         log_not_found off;
           }
}

To activate the server block create a symbolic link of the above configuration file inside /etc/nginx/sites-enabled folder.

 cd /etc/nginx/sites-enabled
 ln -s ../sites-available/wordpress.conf .

Reload NGINX to apply the new WordPress domain settings.

Step 6: Download and Configure WordPress

In this step, download the archived WordPress file using wget and unzip it to the root of the WordPress installation that we have created in the previous step. To accomplish it run the following commands from the terminal.

cd /var/www/html/wordpress/public_html
wget https://wordpress.org/latest.tar.gz
tar -zxvf latest.tar.gz
mv wordpress/* .
rm -rf wordpress

Change the ownership and apply correct permissions to the extracted WordPress files and folders. To do that, use the following command from the terminal.

cd /var/www/html/wordpress/public_html
chown -R www-data:www-data *
chmod -R 755 *

You are now ready to install your WordPress site using your favorite browser.

Source and complete guide