NGINX Basics For Debian Server

NGINX basics for Debian server.

Experience full control over your server environment and deploy the best operating and management systems that fit your needs with our reliable Cloud VPS Hosting!

NGINX is not only a popular reverse proxy option, it’s a complete standalone web server. In this article, you’ll learn some NGINX basics to get up and running on your Debian server right away.

NGINX Basics For Debian

You can use NGINX in a number of different ways. This article presupposes that you want to use NGINX as a replacement for the default Apache web server that comes with your InMotion Hosting cloud VPS account. If you have VPS hosting with cPanel you can still use NGINX. If you have any questions about using NGINX alongside (or instead of) Apache, feel free to get advice from our managed hosting advisors.

Uninstalling Apache

As you may know, your cloud server comes with the Apache web server pre-installed. In order to avoid conflicts, you should consider stopping the Apache service or uninstalling it entirely.

While logged in as the root user, run this command to stop the Apache web service:

service apache2 stop

And to remove Apache entirely, run this command:

apt remove apache2

Please note: if you are not logged in as the root user, you must make sure that your user has sudo privileges and append “sudo” to each command.

Now you can install NGINX, if you haven’t already, by running this command:

apt install nginx

Editing Your Configuration Files (Symlink or No?)

You have a lot of options when it comes to editing your NGINX configuration files. But first things first, take note that these files are located in your /etc/nginx/ directory. You will further notice two directories inside the /nginx/ directory: /sites-available/ and /sites-enabled/. Many NGINX users will create a configuration file for a website or virtual host by adding it to the /sites-available/ directory and create a symbolic link (symlink) to a file in the /sites-enabled/ directory. If you don’t know how to create a symlink, or would rather not bother, you can simply place your configuration files in the /sites-enabled/ directory and they will be active when you restart NGINX.

Sample NGINX Configuration For a Basic Website

Here is a sample configuration for a basic website:

server {
        listen 80 default_server;
        root /var/www/html;
        index index.html index.htm index.nginx-debian.html;

        server_name example.com;
}

Make sure to restart NGINX whenever you make changes to your configuration:

service nginx restart

Sample Advanced Configuration

As you find more use cases for NGINX you can add more advanced configurations. For example, this configuration removes the .html extension from files using the “location” directive:

server {
        listen 80 default_server;
        root /var/www/html;
        index index.html index.htm index.nginx-debian.html;

        server_name example.com;

        location / {
        if ($request_uri ~ ^/(.*)\.html) {
        return 302 /$1;
        }
        try_files $uri $uri.html $uri/ =404;
       }

This demonstrates how you can use a combination of different directives to get different effects.

For more examples check out the NGINX example configurations page.

Setting Up a Basic Page Cache

When it comes to optimizing your site speed, NGINX can play a big role.  At the very least, you should use NGINX to set up a basic cache for static resources like images, CSS files, and font files.  You can do with a directive like the one below:

 location ~* \.(jpg|jpeg|png|ttf|gif|css|js)$ {
expires 7d;
}

Now you should have your NGINX server up and running. Feel free to mix and match and try different configurations to get the perfect recipe. And do consider sharing your results in the comments.


Check out some more resources from the Support Center:

CM
Christopher Maiorana Content Writer II

Christopher Maiorana joined the InMotion community team in 2015 and regularly dispenses tips and tricks in the Support Center, Community Q&A, and the InMotion Hosting Blog.

More Articles by Christopher

Was this article helpful? Join the conversation!