How to Install & Configure Nginx in Ubuntu 20.04

To Install & Configure Nginx in Ubuntu 20.04, do the following

Note: Do the following steps only on a fresh server.

 

First, connect to the server using SSH root and update the server using the following command

sudo apt update -y

 

Now, Install Nginx using the following command

sudo apt install nginx -y

To check if Nginx is installed and enabled or not run the following commands

sudo systemctl status nginx
sudo systemctl is-enabled nginx
 
 
Now, Install MySQL using the following command
 
sudo apt install mysql-server -y
 
After installation of MySQL, run the following command to secure and set a password for MySQL. This command will ask to set the password and configurations to secure the MySQL
 
sudo mysql_secure_installation
 
 
Now, Install PHP FPM and extensions using the following command
 
sudo apt install php-fpm php-mysql -y
 
After the installation of PHP is completed, check it by running the following command
 
php --version
 
 
Now, we need to configure the Nginx directive to setup the domain
 
Create a directory for the domain name using the following commands. (Replace "domain_here" with your domain name)
 
cd /var/www/
sudo mkdir domain_here
 
Create a new config file in Nginx for your domain using the following commands (Replace "your_domain" with your domain name)
 
sudo nano /etc/nginx/sites-available/your_domain
 
Then paste the following code in the file and change the domain name, and location path, and save the file.

server {
listen 80;
server_name your_domain www.your_domain;
root /var/www/your_domain;

index index.html index.htm index.php;

location / {
try_files $uri $uri/ =404;
}

location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}

location ~ /\.ht {
deny all;
}

}

After that, save the file by doing CTRL+X and then y and ENTER to confirm.
 
Now we need to link the configuration file with Nginx’s sites-enabled directory, to run the following command
 
sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
 

After that, unlink the default configuration file from the /sites-enabled/ directory:

sudo unlink /etc/nginx/sites-enabled/default

Now after these changes test the Nginx configurations by running the following command

sudo nginx -t

Now reload the Nginx configurations

sudo systemctl reload nginx

sudo systemctl restart nginx

 

That's it. Now you can create a test index.html file in /var/www/your_domain and open your domain in the browser to check it.

Do the above steps for setting up the second site.

Click Here for How to Install SSL on Nginx

  • VPS, virtual private servers, vps hosting, vps servers, Nginx
  • 0 Користувачі, які знайшли це корисним
Ця відповідь Вам допомогла?

Схожі статті

Backup restoration

Since backups are saved at various nodes to avoid data loss, we manage the backup and...

How To allow SQL Express traffic in windows firewall

Please follow the below steps to allow SQL express communication in the firewall. Open SQL...

How to change SQL authentication in SQL Express

Please follow the below steps Login to SQL Express using SQL management studio. Right-click...

Set Up Sending of Email in Plesk

To Set Up Sending of Email in Plesk, You need to follow the below Points   1. Go to Tools...

How to check Port status of VPS

It may possible that the firewall has blocked a few ports and it is not accessible.  To check...

Powered by WHMCompleteSolution