DEV Community

Harry Naruto
Harry Naruto

Posted on

Install WordPress with Nginx on Ubuntu

WordPress is one of the most popular and open source content management system (CMS) with a whopping 60% of market share as compared with other CMS like Drupal or Joomla.

WordPress can be used to develop any kind of website, be it for a blog, a small business or for a large enterprise. With its free and premium plugins and themes, a WordPress site can be extended quickly to offer advanced functionalities.

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
Enter fullscreen mode Exit fullscreen mode

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. Once you are done with upgrading the system, check the release version of your Ubuntu system with the following command.


# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.2 LTS
Release:        18.04
Codename:       bionic

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Once NGINX has been installed, it will run automatically. You can verify that by the following systemctl command.


 # systemctl status nginx
  nginx.service - A high-performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2019-02-12 09:12:08 UTC; 11s ago
     Docs: man:nginx(8)
  Process: 17726 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 17714 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 Main PID: 17729 (nginx)
    Tasks: 2 (limit: 1152)
   CGroup: /system.slice/nginx.service
           ├─17729 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           └─17732 nginx: worker process

Enter fullscreen mode Exit fullscreen mode

The output of the above command verifies that NGINX is loaded and active with PID of 17729.

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

Enter fullscreen mode Exit fullscreen mode

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';

Enter fullscreen mode Exit fullscreen mode

The installation of MariaDB is complete in your Ubuntu 18.04 system. Now proceed with installing PHP in the next step.

Step 3: Install PHP


# 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

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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;
           }
}

Enter fullscreen mode Exit fullscreen mode

Check the correctness of above configuration file using:


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

Enter fullscreen mode Exit fullscreen mode

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 .

Enter fullscreen mode Exit fullscreen mode

Reload NGINX to apply the new WordPress domain settings.


# systemctl reload nginx

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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
# mv wp-config-sample.php wp-config.php
# vi wp-config.php
 ...
 ...
 define('DB_NAME', 'wordpress_db');
 define('DB_USER', 'wpuser');
 define('DB_PASSWORD', 'Passw0rd!');
 ...
 ...

Enter fullscreen mode Exit fullscreen mode

wp_database_config

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

Step 7: Install WordPress

To complete the installation of WordPress, point your favorite web browser to SUBDOMAIN.DOMAIN.TLD and follow the steps as described below.

wp_1

The installer will prompt you to choose a language. Choose a language and click ‘Continue’.

wp_2

Now provide the site information like site title, username, password, email and click on ‘Install WordPress’ button.

wp_3

You are done with installing WordPress site. Click ‘Log In’ to login to Dashboard and proceed with configuring plugins and themes for your site.

wp_4

Provide the user name and password that we have entered previously to login for the first time.

wp_5

Congratulations! Your WordPress website is installed and ready for you to customize according to your requirements.

Conclusion

WordPress is the most popular CMS and we learned how to install it with NGINX on a Ubuntu server. You can now proceed further to create your website with it.

Top comments (0)