Normally, there's an ad running in this spot, but you're using an ad blocker. To help support our blog, which provides free tutorials for everybody, we just ask that you whitelist or follow us on facebook, twitter, or subscribe using the form to the leftabove. Thank you!

    How to Install and Configure an NGiNX Web Server

    NGiNX (pronounced engine-x) is an efficient, high-performance reverse proxy and web server for websites and applications. Unlike Apache, NGiNX is asynchronous, requiring very few or one thread to handle requests and doesn't require as much overhead. We'll make sure to spend a few articles going over all of its features, but for now we'll just focus on the basic setup.

    Read on if you're a Web Developer, Software Engineer, or other IT professional in need of a web server to serve a web site or application either from your local network or over the internet. NGiNX is also useful for serving sites locally, allowing you to preview your websites on your laptop, as if it were running on the internet. This will come in handy when you have websites that you can't just load by opening the index.html in your browser. PHP sites, for example, require NGiNX, Apache, or another web server to run..

    Installation

    Install Homebrew from https://brew.sh/.

          
          brew install nginx
          
        
          
          sudo apt-get install nginx
          
        

    Configuration

        
        # remove the default symbolic link
        sudo rm /etc/nginx/sites-enabled/default
        # create and edit a new nginx configuration file
        sudo nano /etc/nginx/sites-available/domain.com
        
      
        
        # remove the default symbolic link
        sudo rm /usr/local/etc/nginx/sites-enabled/default
        # create and edit a new nginx configuration file
        sudo nano /usr/local/etc/nginx/sites-available/domain.com
        
      

    Add the following content to your new domain.com file. Replace domain.com with the url of your website or application.

    server {
        # listen on port 80
        listen 80;  
      
        # for requests to these domains
        server_name domain.com www.domain.com;  
      
        # keep logs in these files
        access_log /var/log/nginx/domain.com.access.log;
        error_log /var/log/nginx/domain.com.error.log;
    
        # Allow users to upload large files
        # See http://wiki.nginx.org/HttpCoreModule#client_max_body_size
        client_max_body_size 0;
    
        # example alias
        location / {
            alias /var/www/domain.com; # or wherever your site files are located
            try_files $uri $uri/ =404;
        }
        # example proxy pass
        #location / {
        #    proxy_pass http://localhost:9000;
        #    proxy_redirect off;
        #    proxy_read_timeout 5m;
        #    # make sure these HTTP headers are set properly
        #    proxy_set_header Host            $host;
        #    proxy_set_header X-Real-IP       $remote_addr;
        #    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        #}
    }
    
          
    # Make a symlink to your config file
    sudo ln -s /etc/nginx/sites-available/domain.com /etc/nginx/sites-enabled/domain.com
    # Restart NGiNX
    sudo service nginx restart
    # if you get errors run this
    nginx -c /etc/nginx/nginx.conf -t
          
      
          
    # Make a symlink to your config file
    sudo ln -s /usr/local/etc/nginx/sites-available/domain.com /etc/nginx/sites-enabled/domain.com
    # Restart NGiNX
    sudo service nginx restart
    # if you get errors run this
    nginx -c /usr/local/etc/nginx/nginx.conf -t
          
      

    Local DNS

    If you're going to be running your web site or application locally, and you aren't ready to deploy it to server, add the following lines to your /etc/hosts file.

          
      127.0.0.1 domain.com
      127.0.0.1 www.domain.com
          
      

    Now anytime you navigate or make a request to domain.com on your local computer, it will point to localhost and serve the website that's being served locally by our NGiNX webserver. This is a local DNS, basically. Don't forget to remove these lines from your /etc/hosts file after deploying your site to production.

    Did you like this tutorial? Help us pay for server costs by following us on Facebook, Twitter, and subscribing below, where you'll get post notifications, training webinar invites, and free bundles.