How to Install Nginx on Debian 9


how to install nginx on debian9

Nginx is one of the most popular web server used by millions of websites around the world. Let us take a look at how to install Nginx on Debian 9 system.

 

How to Install Nginx on Debian 9

Here are the step to install Nginx on Debian 9.

 

1. Install Nginx Web Server

Update system packages so that Nginx is installed smoothly.


$ sudo apt-get update
$ sudo apt-get upgrade

 

Install Nginx with the following command


$ sudo apt-get install nginx

 

After installation, Nginx will start automatically, and listen to port 80. You can verify if Nginx is running.


$ ps aux | grep nginx
$ pgrep nginx

 

Also, use the following command to test if port 80 is open


$ netstat -tulpn | grep :80

 

Now Nginx is installed and running. However, it won’t start automatically when you restart your Debian system. So run the following command to start Nginx automatically on system reboot


$ sudo systemctl enable nginx

 

 

2. Some Common Commands to manage Nginx

Here are some common commands to manage your Nginx installation

 

Start Nginx server command


$ sudo systemctl start nginx

 

Stop Nginx server command


$ sudo systemctl stop nginx

 

Restart Nginx server command


$ sudo systemctl restart nginx

 

Get Status of Nginx server command


$ sudo systemctl status nginx

 

3. Test the installation

Open your web browser and go to


http://your_domain.com

or


http://your_server_ip

 

You should see the default Nginx page in the browser

nginx default page

4. Configure Nginx Server

Your website files are located in /var/www/html. By default, Nginx is configured to read files from that location.

If you want to edit this location, you can do so by modifying Nginx config file in a text editor


$ sudo vi /etc/nginx/sites-enabled/default

 

Please note, Nginx’s main config file is located at /etc/nginx


$ sudo vi /etc/nginx/nginx.conf

 

5. (Optional) Create your own config file

Let us configure Nginx for a sample website www.example.com

Open the config file


$ sudo vi /etc/nginx/sites-enabled/default

 

Add the following lines. You can replace example.com with your domain name


server {
 listen 80;
 listen [::]:80;

 server_name www.example.com;

 root /var/www/html;
 index index.html;

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

 

Now create an HTML file index.html at /var/www/html (root location mentioned above)


$sudo vim /var/www/html/index.html

 

Add the sample HTML code, save and close it.


<!DOCTYPE html>
<html>
 <head>
  <title>Welcome to Ubiq!</title>
<style>
 body {
  width: 35em;
  margin: 0 auto;
  font-family: Tahoma, Verdana, Arial, sans-serif;
 }
</style>
</head>
<body>
 <h1>Welcome to Ubiq</h1>
 <p>This is a test for Nginx virtual host.</p>

 <p><em>Thank you for using Ubiq tutorials.</em></p>
</body>
</html>

 

Update the file permission of your html file


$ sudo chmod 0444 /var/www/html/index.html

 

Test it by opening and web browser and visiting http://www.example.com

You’ll see the HTML page that we just created

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!