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, like Facebook, WordPress, Sourceforge or others.
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 20/19/18.
Installation of Nginx Web Server
1. 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
2. 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
3. For Nginx to be able to run WordPress, you need to install PHP, PHP-FPM, and MariaDB packages.
$ 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
4. 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
5. 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
6. 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
After running the script, it will take you through a series of questions where you can answer yes(y)
to improve the security of your MariaDB installation in the following ways:
- 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
7. 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;
8. 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
9. 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
10. 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; } }
11. 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
12. 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
Completing the WordPress Installation via the Web Installer
13. Now open your web browser and complete the WordPress installation using the web installer.
http://mysite.com/ OR http://SERVER_IP/
14. Then add the website information such as title, admin username, password, and email address. Then click Install WordPress to continue the installation.
15. Once WordPress installation finishes, proceed to access the website administrator’s dashboard by clicking on the login button as highlighted in the following screen.
16. At the website admin’s login page, provide your username and password created above and click login, to access your site’s admin dashboard.
17. After installation completes undo permissions by issuing the following command.
$ sudo chown -R root /var/www/html/mysite.com
Enable HTTPS on WordPress
18. 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.
I know you are a few years ago but just FYI don’t need to install Universe PPA anymore for Certbot and also if installing the PHP-FPM module you can skip the PHP module
https://github.com/littlebizzy/slickstack/blob/master/ss-install-php.txt
SlickStack example for PHP-FPM on Ubuntu 20.04 (the Certbot is separate, check ss-encrypt script)
Hey great guide but im stuck at step 8 I did everything but when I point to my IP in my case its 192.168.3.130 it shows the welcome screen of “Welcome to nginx!” when i try 192.168.3.130/phpmyadmin/ it shows the 404 Not Found i then went to winscp to see if theres a symlink and i did see /usr/share/nginx/www which is the symlink to /usr/share/nginx not sure what im doing wrong
Thank you
If the root location of your vhost points to /srv/www/html, then make a symlik to phpmyadmin with the following command: ln -s /usr/share/phpmyadmin /srv/www/html/
Thanks for this post. That’s so simple and useful.
You can automate this using EasyEngine
EasyEngine setup and tune nginx php5-fpm mysql and postfix also provide 12 different caching options for WordPress
EasyEngine: https://github.com/rtCamp/easyengine/
Thanks!!