NGINX (pronounced engine-x) is an open-source powerful, light, and flexible HTTP server that has increased in popularity in last years and now is the main server interface that powers some of the most huge-traffic websites these days.
What makes it so fast and reliable is the fact that it uses the same modular design as Apache, but has a different approach regarding web sockets, using an event-driven – asynchronous architecture that does not spawn processes as fast as it receives requests and also uses simple configuration files.
For Ubuntu and Debian based systems, Nginx is already compiled as a package in their repositories and can be installed through apt package utility.
It also supports Virtual Hosts like Apache and uses a Fastcgi channel to communicate with PHP files on the server through PHP-FPM.
This tutorial covers installing and basic file configurations for Nginx to host a WordPress CMS website on a Virtual Host and setting applies to Ubuntu 18.04/20.04, Debian 10/9 and Linux Mint.
Installation of Nginx Web Server
Step1. Nginx installation for Ubuntu, Debian or Linux Mint is as straight as any other packages and can be installed with just a simple command.
$ sudo apt-get install nginx
Step2. Next, start, enable, and verify the status of Nginx use the following systemctl commands.
$ sudo systemctl start nginx $ sudo systemctl enable nginx $ sudo systemctl status nginx
Installation of PHP and MariaDB Server
$ sudo apt-get install php php-mysql php-fpm php-curl php-gd php-intl php-mbstring php-soap php-xml php-xmlrpc php-zip mariadb-server mariadb-client
Step3. Next, verify that the MariaDB database service is running and enabled to automatically start when your system is booted.
$ sudo systemctl status mariadb $ sudo systemctl is-enabled mariadb
Step4. In order to communicate with FastCGI backend, the PHP-FPM service must be active on the server.
$ sudo systemctl start php7.4-fpm $ sudo systemctl enable php7.4-fpm $ sudo systemctl status php7.4-fpm
Step5. Now you need to make your MariaDB installation secure by running the mysql_secure_installation
script which ships with the MariaDB package.
$ sudo mysql_secure_installation
in this process , You will be asked to Set root Password , Remove anonymous users , Disallow root login remotely , Remove test database , Reload privilege tables and need Enter ” Y ” to do them.
- Enter current password for root (enter for none):
Enter
- Set a root password? [Y/n]
y
- Remove anonymous users? [Y/n]
y
- Disallow root login remotely? [Y/n]
y
- Remove test database and access to it? [Y/n]
y
- Reload privilege tables now? [Y/n]
y
Installation of WordPress
Step6. A WordPress requires a database to store data on the server, so create a new WordPress database for your website using the mysql
command as shown.
# mysql -u root -p MariaDB [(none)]> CREATE DATABASE mysite; MariaDB [(none)]> GRANT ALL PRIVILEGES ON mysite.* TO 'mysiteadmin'@'localhost' IDENTIFIED BY 'SecureP@ssHere!'; MariaDB [(none)]> FLUSH PRIVILEGES; MariaDB [(none)]> EXIT;
Step7. Now is time to create a WordPress Virtual Host root path, download the WordPress archive, extract it then issue a recursive copy to /var/www/html/wordpress
.
$ sudo mkdir -p /var/www/html/mysite.com $ wget http://wordpress.org/latest.tar.gz $ tar xfvz latest.tar.gz $ sudo cp -r wordpress/* /var/www/html/mysite.com
Step8. For a smooth WordPress installation without any wp-config.php
creation file errors, grant Nginx www-data
system users with write permission over /var/www/html/mysite.com
the path and revert changes after installing WordPress.
$ sudo chown -R www-data /var/www/html/mysite.com $ sudo chmod -R 755 /var/www/html/mysite.com
Creating an NGINX Virtual Host for WordPress Website
Step9. Now is time to create a basic Virtual Host for WordPress website on the Nginx server. Run the following command to create a WordPress server configuration file.
$ sudo vim /etc/nginx/conf.d/mysite.com.conf
Then add the following content.
server { listen 80; listen [::]:80; root /var/www/html/mysite.com
; index index.php index.html index.htm; server_namemysite.com www.mysite.com
; error_log /var/log/nginx/mysite.com_error.log; access_log /var/log/nginx/mysite.com_access.log; client_max_body_size 100M; location / { try_files $uri $uri/ /index.php?$args; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.4-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } }
Step10. By default, Nginx routes all requests to the default
server block. Therefore, remove the default
server block to enable your WordPress website or other websites you want to host on the same server later.
$ sudo rm /etc/nginx/sites-enabled/default $ sudo rm /etc/nginx/sites-available/default
Step11. Next, check the NGINX configuration syntax for any errors before you can restart the Nginx service to apply the new changes.
$ sudo nginx -t $ sudo systemctl restart nginx
Step12. After all Apache nasty server configurations had been made and MySQL database and the administrative user had been created it’s now time to actually perform WordPress installation on our box.
First of all download the latest WordPress archive by issuing the following wget command.
$ wget http://wordpress.org/latest.tar.gz
Step13. Next extract WordPress archive and copy all extracted files to Apache Virtual Host DocumentRoot, that will be '/var/www/html'
on Ubuntu and Linux Mint systems.
$ sudo tar xvzf latest.tar.gz $ sudo cp -r wordpress/* /var/www/html/mysite.com
On Debian systems, run the following commands.
$ sudo tar xvzf latest.tar.gz $ sudo mkdir -p /var/www/html $ sudo cp -r wordpress/* /var/www/html/mysite.com
Completing the WordPress Installation via the Web Installer
Step14. Now open your web browser and complete the WordPress installation using the web installer.
http://mysite.com/ OR http://SERVER_IP/
Step15. Then add the website information such as title, admin username, password, and email address. Then click Install WordPress to continue the installation.
Step16. Once WordPress installation finishes, proceed to access the website administrator’s dashboard by clicking on the login button as highlighted in the following screen.
Step17. At the website admin’s login page, provide your username and password created above and click login, to access your site’s admin dashboard.
Step18. After installation completes undo permissions by issuing the following command.
$ sudo chown -R root /var/www/html/mysite.com
Enable HTTPS on WordPress
Step19. If you want to enable HTTPS on your WordPress website, you need to install a free SSL certificate from Let’s Encrypt as shown.
$ sudo apt-get update $ sudo apt-get install software-properties-common $ sudo add-apt-repository universe $ sudo apt-get update $ sudo apt-get install certbot python3-certbot-nginx $ sudo certbot --nginx
To confirm that your WordPress site is set up correctly using a Free SSL certificate, visit your website at https://yourwebsite.com/
and look for the lock icon in the URL bar. Alternatively, you can check your site’s HTTPS at https://www.ssllabs.com/ssltest/.
Congratulations! You have successfully installed the latest version of WordPress with NGINX on your server, now start building your new website or blog.
Step20. You are succeed , enjoy your new blog !