Install WordPress on Nginx Ubuntu
Table of Contents
Nginx is a well-known web server that is used to serve webpages on the Internet. Nginx, which was first launched in October 2004, quickly became the web server and proxy server of choice for thousands of web developers. It has a number of benefits over its competitor Apache. In this article, we will learn how to install Nginx on Ubuntu and use it to host WordPress.
Before we begin, let me state unequivocally that this is not a comparison of Nginx vs Apache or any other web server. Apache was introduced in 1999 and offers several useful features. If you wish to install WordPress on Apache, we already have an article on the subject, so check it out.
Nginx was first launched in October 2004, about 17 years ago. Most web developers favour Nginx because of its capacity to handle multiple concurrent client requests quickly with limited hardware resources, making hosting websites on the Internet less expensive. Nginx can also be used as a reverse proxy, directing client requests to the primary hosting server, which does not have public Internet access, making the hosting server more secure than it would otherwise be.
Pre-requisites to install WordPress on Nginx
In this article, we will use Ubuntu operating system, PHP 8.1, Nginx, and WordPress. If you do not know what WordPress is, we have covered WordPress in a series of articles, WordPress 101.
- Ubuntu 20.04
- Nginx
- PHP 8
- WordPress
Ubuntu 20.04 is a long-term support release of Ubuntu. If you need assistance installing Ubuntu, please refer to this article on LinuxAndUbuntu. Once you have installed Ubuntu, we can move on to installing the rest of required packages.
If you’re using cloud services like AWS, Google Cloud Platform, Linode, DigitalOcean, or MassiveGRID, you can skip the installation stage. All cloud providers support one-click Ubuntu server installation with Nginx. So go with Ubuntu 20.04, codenamed as Focal Fossa. Most cloud providers pre-install the UFW firewall on Ubuntu by default. I recommend that you enable the firewall to improve server security and allow the TCP ports listed below.
In case UFW is not pre-installed, feel free to install it from the repository –
apt install ufw
ufw enable
ufw allow http
ufw allow https
ufw allow 22
http and https are port 80 and port 443. Port 22 is for SSH connection into the server. If your server uses any custom port for ssh, please allow the custom ssh port before closing the current SSH connection. After enabling UFW firewall, it’ll block all ports unless allowed manually. So once you close the ongoing SSH connection, it will not allow SSH access until the SSH port is allowed.
Also, update the operating system to make sure the server is fully patched with the latest security updates.
apt update
apt upgrade
It is not recommended to login as root and perform actions on the server. So setup a new user account and grant it
sudo
privileges.adduser username
The above command will intitate the user creation process. Follow the simple instructions and then grant the newly created user sudo
privileges using the following command –
usermod -aG sudo username
Now reboot
the server and ssh
into the server with new user account.
Install Nginx
Installing Nginx is same as installing any other package on the system. Use the apt package manager to install nginx from the repository –
sudo apt install nginx
When the installation process is over, start/restart the server using the following command –
service nginx restart
service nginx status
Visit the IP address to confirm the server is functioning properly. The following is the default nginx page that confirms the proper functioning of nginx on the server.
Confirgure Nginx to serve WordPress
Nginx can easily handle multiple websites on the same server using virtual hosts. By default, nginx creates a default profile that shows the above Nginx welcome page. We will first disable the default profile, then create a new profile to handle WordPress.
sudo unlink /etc/nginx/sites-enabled/default
The above command will disable the default Nginx profile. Now create a new profile for our WordPress installation.
sudo nano /etc/nginx/sites-available/example.com
Now paste the following configuration in the text editor.
server {
listen 80;
listen [::]:80;
server_name gaminggroup.online;
root /var/www/gaminggroup.online;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$args;
}
}
Before you save the above file, please understand what the above configuration does. After we enable this configuration, Nginx will start looking for requests for the domain name mentioned as the value of server_name in the config.
server_name => the domain name for our website
root => the root directory of our website stored on the server
index => the file to look for in the website root
location => the location block processes the request URI (It’s a little complicated and deserves a separate article on its functioning)
Replace the gaminggroup.online with your own domain name. With the root option, we have specified that our website root is located inside /var/www/gaminggroup.online
. So in the next step, we will create the website directory at /var/www
.
Now save the configuration file with ctrl + x and hit Enter.
Create Website Root directory
sudo mkdir /var/www/gaminggroup.online
Now whenever somebody visits our website example.com, based on the request, nginx will serve content to the user from this /var/www/gaminggroup.online
directory. To test our configuration, create a sample index.html inside our website root (/var/www/gaminggroup.online).
sudo nano /var/www/gaminggroup.online/index.html
Paste the following sample HTML in the file and save it.
<h2>Hello world!</h2>
<p>This is a test page for gaminggroup.online</p>
Before we test if everything is working properly, let us set correct permissions on the website root directory so that the server can access the files inside it.
sudo chown -R www-data:www-data /var/www/gaminggroup.online/index.html
Check Nginx configuration for any error –
sudo ln -s /etc/nginx/sites-available/gaminggroup.online /etc/nginx/sites-enabled/
sudo nginx -t
If there is not mistake in the configuration, we are good to go. Visit the the domain name and you should see the sample index.html page we setup.
sudo service nginx restart
The above screenshot confirms the successful working of our configurations. Now we can move on to preparing for WordPress installation.
Let’s install database server and create the database for WordPress.
Install MariaDB server
sudo apt install mariadb-server
Run mysql_secure_installation to setup server’.
sudo mysql_secure_installation
You can create the MySQL root user’s password. If you do not want to create root user’s password, just login to MySQL root user by using sudo msyql
.
If you created root user password, then login to the root user using the following command –
mysql -u root -p
Or
sudo mysql (type sudo password when prompted)
Create database –
create database wordpress;
It is highly recommended to create a new MySQL user for WordPress database.
Create MySQL user –
create user 'sandy'@'localhost' identified by 'password';
Grant database (wordpress) privileges to the newly created user –
grant all privileges on wordpress.* to 'sandy'@'localhost';
Please note that we will use this database user in WordPress so that it can connect to the database. Connecting WordPress to the database using root user is highly forbidden.
Let’s install the required packages including PHP and several PHP extensions that WordPress requires to function.
Add PHP 8 repository
sudo add-apt-repository ppa:ondrej/php
Install PHP and PHP extensions –
sudo apt install php-cli php-fpm php-mysql php-json php-mbstring php-xml php-gd php-curl
And that is it for our WordPress installation. Remove the sample index.html file from /var/www/gaminggroup.online
.
Download WordPress –
wget -O /tmp/wordpress.tar.gz https://wordpress.org/latest.tar.gz
sudo tar -xvf /tmp/wordpress.tar.gz -C /tmp/
Move WordPress files to website root directory –
sudo mv /tmp/wordpress/* /var/www/gamingroup.online
Fix permissions –
sudo chown -R www-data:www-data /var/www/gaminggroup.online
sudo find /var/www/gaminggroup.online/ -type d -exec chmod 755 {} \;
sudo find /var/www/gaminggroup.online/ -type f -exec chmod 644 {} \;
Add the following location
block in the site’s configuration after the existing location
block so that it works with WordPress.
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
}
The final configuraiton should look something like this –
server {
listen 80;
listen [::]:80;
server_name gaminggroup.online;
root /var/www/gaminggroup.online;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
}
}
And that’s all for our WordPress setup. Now further steps needed to be completed from the web browser. Visit your domain name from the web browser and it’ll redirect you to the WordPress installation page.
Click “Let’s go!” for the setting up database with WordPress. Now enter the database name, database username, database user’s password that we created above, and click “Submit”.
Click “Run the installation” and it’ll allow you to enter website title, setup WordPress administrator, etc. Do not worry, you can always modify these details later.
And surprise surprise! We’ve installed WordPress on Nginx. The next few days, you will need to spend on setting up the website, installing new themes, plugins, and publish amazing content. Do share with us what you create! 🙂
Conclusion
Although the installation is over but since this is an unmanaged server, you will need to take care of your server on your own. Make sure to keep the server up-to-date by regularly installing updates or better setup unattended upgrades to automatically install updates. Also, setup Canonical Livepatch to install Kernel updates without making the server reboot.
If you want to want to migrate WordPress from old web hosting to this server, please read this guide. Or, read this guide if you want to migrate your Weebly site to WordPress.
LinuxAndUbuntu Newsletter
Join the newsletter to receive the latest updates in your inbox.