Installing Nginx On Debian 9

Nginx is one of the best free, opensource and High-performance web server available today. It is used as a reverse proxy web server for Apache and other web servers. Nginx can handle the bigger amount of connection than apache consuming low memory power. So it can be used for high traffic websites. In this tutorial, you are going to learn how to install Nginx on Debian 9.

Prerequisites

Before you start to install Nginx on Debian 9. You must have the non-root user account on your server with sudo privileges.

1. Install Nginx on Debian

Here first you will need to update the package manager index and then you will install Nginx.

Update package manager index by typing

sudo apt update

Now install Nginx by typing

sudo apt install nginx

Nginx web server automatically starts after installation complete. To check the status of Nginx server and confirm installation type following in the terminal

sudo systemctl status nginx

The output should be:

● nginx.service - A high performance web server and a reverse proxy server
    Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
    Active: active (running) since Sat 2018-10-22 11:44:12 CDT; 4min 10s ago
    Docs: man:nginx(8)
    Process: 6412 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 6409 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Main PID: 6413 (nginx)
    CGroup: /system.slice/nginx.service
    ├─6413 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
    ├─6414 nginx: worker process
    └─6415 nginx: worker process

2. Managing Nginx web server

There are some useful commands using **systemctl** you can manage your Nginx web server easily.

Make it sure Nginx started on system reboot by typing

sudo systemctl enable nginx

If you want to disable automatic startup on system reboot enter following command

sudo systemctl disable nginx

To check the status on Nginx enter the following command

sudo systemctl status nginx

You can start server by typing

sudo systemctl start nginx

You can stop Nginx server using the following command

sudo systemctl stop nginx

In case, If you want to restart Nginx server type following command

sudo systemctl restart nginx

3. Set up Nginx configuration file

Now you should create a directory inside var/www named example.com (you can also use your domain name)

sudo mkdir -p /var/www/example.com

Now you should remove the default configuration file provided. Delete default Nginx configuration file by typing.

sudo rm -f /etc/nginx/sites-enabled/default

Configuration files for the website are stored inside /etc/nginx/sites-available directory so you need to create configuration file inside this directory named example.com.conf. Then enter following code inside the file by replacing example.com with your domain name.

/etc/nginx/sites-available/example.com.conf
    server {
        listen         80 default_server;
        listen         [::]:80 default_server;
        server_name    example.com www.example.com;
        root           /var/www/html/example.com;
        index          index.html;

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

Create a symbolic link of the above configuration file inside /etc/nginx/sites-enabled/ directory by entering

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

Now you will need to reload Nginx configuration file as you have made changes in Nginx configuration directory. Type following command to reload Nginx

sudo nginx -s reload

You can check the status of Nginx by typing following

sudo nginx -t

Create an index.html file inside /var/www/example.com directory and enter following code inside the file

<html>
    <head>
        <title>Index Page</title>
    </head>
    <body>
        <h1>Success!</h1>
    </body>
</html>

Now visit [http://example.com](http://example.com). Replacing example with your domain name where you can see the output as given: ==Success!==

Conclusion

You have successfully learned how to install Nginx on Debian 9. If you have any queries regarding this please don’t forget to comment below.

Leave a Reply

Your email address will not be published. Required fields are marked *
You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>