Set up nginx on Debian

Nginx is a popular high-performance webserver. Many users prefer it over apache for its quick page render time and low memory utilization, making it a perfect fit for both small and large VPSes. Nginx is compatible with HTML, PHP, and many other media.

Step 1 - Installing nginx

apt-get is a program that manages all of Debian’s repositories for you. Its primary function is to install and uninstall packages from your system.

To install nginx, start an SSH session on your VPS and run the command shown here:

$ sudo apt-get update && sudo apt-get install nginx -y

Step 2 - Change working directory

By default, nginx will serve content located in /usr/share/nginx/www. To make updating the site easier, we will configure nginx to serve content from a system-wide location. To do this, we run:

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

Look for this line:

      root /usr/share/nginx/www;

and change it to the path shown here:

      root /var/www/public_html;

Press control+x , y, and then enter. Now, we will make the new working directory and then copy the contents of the old working directory into our new working directory:

$ sudo mkdir -p /var/www/public_html

$ sudo cp /usr/share/nginx/www/* /var/www/public_html/

We need to make sure that nginx can read and write to the working directory.

$ sudo chown -R www-data:www-data /var/www

Finally, we restart nginx.

$ sudo service nginx restart

Step 3 - Verify nginx is running

Navigate to your VPS in a web browser, and if you see “Welcome to nginx!”, nginx is working correctly! Now you can modify your website by changing the files in the web root we’ve set.
Was this answer helpful? 39 Users Found This Useful (66 Votes)