Grid Guides

Explore How System On Grid Can Work For You

Grid Guide Topics

How to Install Nginx on Debian 9

Table Of Contents


    ## Table of Contents

    – [Introduction](#introduction
    – [Prerequisites](#prerequisites
    – [Install Nginx on Debian 9](#install-nginx-on-debian-9
    – [Install Nginx](#install-nginx
    – [Adjust the Firewall](#adjust-the-firewall
    – [Check Your Web Server](#check-your-web-server
    – [Manage the Nginx Services and Units](#manage-the-nginx-services-and-units
    – [Conclusion](#conclusion

    ## Introduction

    In this guide, we will explain to you how to install Nginx on Debian 9.

    [Nginx](https://www.nginx.com/ was created by Igor Sysoev as an answer to the challenge of handling 10 thousand client connections all at the same time. It was first publicly released in 2004. It is a web server which can also be used as an HTTP cache, load balancer, reverse proxy, and mail proxy.

    Nginx has become popular for its lightweight utilization and ability to scale quickly on minimal hardware. It uses non-synchronized and event-driven architecture to cope with huge loads. It is often chosen by administrators for its responsiveness under huge loads and resource efficiency.

    ## Prerequisites

    Before installing Nginx, you should have access to Debian 9 and created a non-root user account with sudo privileges by following our guide, [Initial server setup with Debian 9](https://systemongrid.com/support/guides/initial-server-setup-with-debian-9.

    ## Install Nginx on Debian 9

    ## Install Nginx

    Before installing Nginx, update the available packages using the apt command.

    “`
    $ sudo apt update
    “`

    By default, Debian has Nginx in its default repositories. You can download and install Nginx from the default repositories using the apt command.

    “`
    $ sudo apt install nginx
    “`

    You will be asked if you want to continue the Nginx installation. Enter “Y” and then hit the ENTER button to continue the installation.

    ## Adjust the Firewall

    Get the list of applications that UFW knows using the below command.

    “`
    $ sudo ufw app list
    “`

    Output:

    ![nginxufwavailableapplications](https://grid.media/assets/images/nginx-ufw-available-applications-02132019.png

    In the above output, you can see that there are three available applications for Nginx.

    ### The “Nginx Full” application opens both port 80 (which is normal and unencrypted web traffic and port 443 (which is a TLS/SSL encrypted traffic.
    ### The “Nginx HTTP” application opens only port 80 (which is normal and unencrypted web traffic.
    ### The “Nginx HTTPS” application opens only port 443 (which is a TLS/SSL encrypted traffic.

    In this guide, you need to open only port 80. To do so, use the below command. ‘

    “`
    $ sudo ufw allow ‘Nginx HTTP’
    “`

    Then, enable the firewall.

    “`
    $ sudo ufw enable
    “`

    Output:

    ![nginxfirewallisactive](https://grid.media/assets/images/nginx-firewall-is-active-02132019.png

    Enter “Y” and hit the ENTER button.

    Now, check the status of the firewall.

    “`
    $ sudo ufw status
    “`

    Output:

    ![nginxufwstatus](https://grid.media/assets/images/ufw-status-http-traffic-allowed-02132019.png

    The firewall is currently allowing in only HTTP traffic. If you install and configure some additional services, you will need to alter the firewall settings to allow the acceptable traffic in.

    ## Check Your Web Server

    Check the status of the Nginx service you have installed using the below command.

    “`
    $ systemctl status nginx
    “`

    Output:

    ![nginxdebianstatus](https://grid.media/assets/images/nginx-debian-status-02132019.png

    Alternatively, you can also check if your server is up and running by accessing your public IP address or server’s domain name.

    Find your public IP address using the below command if you don’t know the IP address and don’t have a domain name pointed at your server.

    “`
    $ ip addr show ens3 | grep inet | awk ‘{ print $2; }’ | sed ‘s//.*$//’
    “`

    As an alternative, you can also use the below command to know your public IP address.

    “`
    $ curl -4 icanhazip.com
    “`

    You will get your public IP address as an output. Type the server domain or IP address in a web browser. It should direct to the Nginx’s default landing page.

    “`
    http://server_domain_or_IP_address
    “`

    Output:

    ![welcometonginx](https://grid.media/assets/images/welcome-to-nginx-debian-02132019.png

    ## Manage the Nginx Services and Units

    Debian installs Nginx as a systemd service. For all the service management tasks, the target unit is service units with .service suffix. But, you can leave .service suffix for most of the service management commands as systemd can know that you want to operate on a service.

    Use the below command to stop the Nginx service.

    “`
    $ sudo systemctl stop nginx
    “`

    Use the below command to start the Nginx service.

    “`
    $ sudo systemctl start nginx
    “`

    Use the below command to restart the Nginx service.

    “`
    $ sudo systemctl restart nginx
    “`

    Use the below command to reload the Nginx service.

    “`
    $ sudo systemctl reload nginx
    “`

    Use the below command to disable the Nginx service.

    “`
    $ sudo systemctl disable nginx
    “`

    Use the below command to enable the Nginx service.

    “`
    $ sudo systemctl enable nginx
    “`

    ## Conclusion

    In this guide, you have learned how to install Nginx on Debian 9 and how to manage the Nginx services and units.