Nginx is a popular Apache web server alternative and open source as well. In this tutorial, we learn the simple commands to install Nginx on Ubuntu 20.04 Focal Fossa LTS Linux using the command terminal.
Apart from using as a Web server, it is also used as a proxy, cache, and load-balancing server. To install it, you just need a Linux server such as Ubuntu 20.04 and sudo user rights along with the Internet connection.
Steps to install Nginx on Ubuntu 20.04 LTS
1. Update Ubuntu 20.04
Start with updating the system which we’ll refresh the APT package manager index cache. It is important when we install some applications on Ubuntu using the default package manager.
sudo apt update
2. Manually Install Nginx on Ubuntu 20.04
Nginx is a popular open-source software, hence available to install using the default system repository of Ubuntu 20.04. So, in your command terminal just run the following given command.
sudo apt install nginx
3. Start, Restart or Stop the Nginx service
Once the installation is completed, nothing else we need to do. However, to confirm it is on our system running absolutely fine without generating any errors run the command given here.
For getting version details:
nginx -v
For Checking the status of the Nginx service, we can use:
sudo systemctl status nginx --no-pager -l
If the service is not running, then we can start it using:
sudo systemctl start nginx
Whereas, in the future, if you want to stop or restart the Nginx then the commands to use are:
To restart:
sudo systemctl restart nginx
To Stop:
sudo systemctl stop nginx
4. Open Firewall for Nginx Traffic on Ubuntu 20.04
If you are relying on the Ubuntu system firewall and it is active, then we have to open port 80 for the HTTP connection and 443 for the HTTPS connection. You can use the given command for that:
sudo ufw allow 'Nginx Full'
Whereas those who are using some cloud hosting service such as Amazon Lightsail, need to manually open the ports in their service provider’s firewall.
5. Check the Demo page
To confirm, the Nginx is properly delivering the test page over HTTP, open your browser and type: http://127.0.0.1
or http://your-server-ip-address
![Manually install Nginx on Ubuntu 20.04 Manually install Nginx on Ubuntu 20.04](https://www.how2shout.com/linux/wp-content/uploads/2022/11/Manually-install-Nginx-on-Ubuntu-20.04.png)
Those who can access the graphical interface to run the browser and want to access the Nginx test page; can use the Terminal as well. For that run:
sudo apt install curl
Now, use curl to fetch page details:
curl –i 127.0.0.1
or
curl -i your-server-ip
Note: Replace your-server-ip with the actual Ip-address assigned to your server.
The output you get after accessing the test page using the curl and terminal will be something as shown in the given screenshot.
![Terminal command to access Nginx test page Terminal command to access Nginx test page](https://www.how2shout.com/linux/wp-content/uploads/2022/11/Terminal-command-to-access-Nginx-test-page.png)
6. Nginx Server Block for WordPress
Once we have the Nginx Server up and ready we can start delivering our website by placing our web files. However, by default, the Nginx web server reads the files available under /var/www/html
. However, when it comes to setting up multiple domains or websites on a single web server we need to configure server blocks for each website.
Let’s say you have xyz.com domain and want to install WordPress for it. First, create a directory for it to store the content of WordPress.
Note: Replace xyz.com with the one you want to use.
sudo mkdir -p /var/www/xyz.com/html
Change the created directory permission:
sudo chown -R $USER:$USER /var/www/xyz.com
sudo chmod -R 755 /var/www/xyz.com
Place the file of the website you want to access on your domain under the created directory. For example, you want to set up WordPress. Then let’s first download it. You can put any other CMS or HTML files for the website you want.
For WordPress, first, download it:
wget https://wordpress.org/latest.zip
Extract and move its file to the directory you have created for your domain.
unzip latest.zip
mv wordpress/* -d /var/www/xyz.com/html
Create a server configuration block:
Now, let’s create a configuration block that is known as a virtual host configuration in the Apache web server.
sudo nano /etc/nginx/sites-available/xyz.com
Add the following lines:
server {
listen 80;
listen [::]:80;
root /var/www/xyz.com/html;
index index.php index.html index.htm;
server_name xyz.com www.xyz.com;
location / {
try_files $uri $uri/ =404;
}
}
Note: To run WordPress your system must have PHP installed.
To save the file press Ctrl+O, hit the Enter key, and then exit Ctrl+X.
7. Enable create site configuration
Once you have created the files, let’s create a Symbolic link for it inside the site-enabled folder of Nginx, so that the web server can read it.
sudo ln –s /etc/nginx/sites-available/xyz.com /etc/nginx/sites-enabled
Restart the Nginx:
sudo systemctl restart nginx
8. Test the configuration file
Now, let’s test the configuration file to find if there is any error in our created file.
sudo nginx -t
The output must be:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
9. Access your Web files
Now, open your browser that can access your server IP address where you have installed the Nginx webserver. Type the Ip-address or domain in the following format:
http://server-ip-adress
or
http://your-domain.com
In our case, here we have xyz.com.
![Install WordPress on Nginx Ubuntu 20.04 Install WordPress on Nginx Ubuntu 20.04](https://www.how2shout.com/linux/wp-content/uploads/2022/11/Install-Wordpress-on-Nginx-Ubuntu-20.04.png)
In this way, we can install and use the Ngnix Web server on Ubuntu 20.04 to deliver some websites.
Other Articles:
⇒ Set Up Nginx as a Reverse Proxy For Apache on Ubuntu 22.04
⇒ NGINX vs. Apache: Comparision of web servers to host your website
⇒ How to Install WordPress on Ubuntu 22.04 LTS Server
⇒ Install Microsoft SQL Server 2022 preview on Ubuntu 20.04 LTS