Configuring Nginx on a VPS for Websites and Web Applications

1. Why Nginx is Used

Nginx is a modern web server commonly used for hosting websites and server-side applications.

It is often used for:

  • hosting websites
  • working as a reverse proxy
  • handling HTTPS connections
  • load balancing
  • running Docker and API services

Due to its high performance and low resource usage, Nginx is suitable for both small websites and large-scale web projects.

2. Requirements Before Installation

Before starting, prepare the following:

  • a VPS running Ubuntu or Debian
  • SSH access to the server
  • a domain name if SSL will be configured

3. Installing Nginx

Connect to the VPS via SSH and update the package list:

apt update

Install Nginx:

apt install nginx -y

After installation, start the service:

systemctl start nginx

Enable automatic startup after reboot:

systemctl enable nginx

Check the service status:

systemctl status nginx

4. Checking if Nginx Works

Open your VPS IP address in a browser:

http://SERVER_IP

If the default Nginx page appears, the web server is working correctly.

5. Creating a Website Configuration

Create a directory for the website files:

mkdir -p /var/www/mysite

Create a simple test page:

echo "Nginx works" > /var/www/mysite/index.html

Create the configuration file:

nano /etc/nginx/sites-available/mysite

Example configuration:

server {
listen 80;
server_name example.com www.example.com;

root /var/www/mysite;
index index.html;

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

Enable the website:

ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/

Check the configuration:

nginx -t

Restart Nginx after validation:

systemctl restart nginx

6. Configuring HTTPS with SSL

You can use Certbot to enable HTTPS.

Installation:

apt install certbot python3-certbot-nginx -y

Run the automatic setup:

certbot --nginx

During the setup process, you will need to:

  • select the domain
  • confirm SSL certificate issuance

After completion, HTTPS will be configured automatically.

7. Common Problems

Nginx Does Not Start

Check for configuration errors:

nginx -t

or:

journalctl -xe

Website Does Not Open

Common reasons include:

  • ports 80 or 443 are blocked
  • DNS records have not updated yet
  • configuration errors in Nginx

SSL Certificate Cannot Be Issued

Check:

  • whether the domain points to the VPS IP address
  • whether port 80 is open
  • whether there are configuration errors

8. Where Nginx is Commonly Used

Nginx is suitable for:

  • websites
  • WordPress
  • APIs
  • Docker services
  • reverse proxy setups
  • web applications

9. Conclusion

Nginx is a fast and flexible web server suitable for hosting websites, APIs, and server applications on a VPS. It is easy to scale and adapt for different infrastructure tasks.

Leave a Reply 0

Your email address will not be published. Required fields are marked *