Installing and Configuring Nginx

Installing and Configuring Nginx

Nginx is a high-performance web server and reverse proxy widely used for serving websites and web applications.

Install Nginx

Ubuntu/Debian:

apt update
apt install nginx -y
systemctl enable nginx
systemctl start nginx

CentOS/AlmaLinux/Rocky:

dnf install nginx -y
systemctl enable nginx
systemctl start nginx

Test the Installation

Open your browser and navigate to http://YOUR_SERVER_IP. You should see the Nginx welcome page.

Configuration Structure (Ubuntu)

  • /etc/nginx/nginx.conf — main config
  • /etc/nginx/sites-available/ — site configs (inactive until enabled)
  • /etc/nginx/sites-enabled/ — symlinks to active site configs

Create a Server Block for Your Domain

nano /etc/nginx/sites-available/yourdomain.com
server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;
    root /var/www/yourdomain.com;
    index index.html index.php;

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

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

Enable the site and reload:

ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
nginx -t
systemctl reload nginx

Useful Commands

nginx -t               # Test configuration for errors
systemctl reload nginx # Apply changes without downtime
systemctl restart nginx
  • 0 Utenti hanno trovato utile questa risposta
Hai trovato utile questa risposta?

Articoli Correlati

Managing Users and Groups

Managing Users and Groups on Linux Proper user management is essential for server security. Avoid...

Managing Services with systemctl

Managing Services with systemctl systemctl is the standard tool for managing services on modern...

Monitoring Server Resources

Monitoring Server Resources Keeping an eye on your server's resource usage helps you identify...

Scheduling Tasks with Cron

Scheduling Tasks with Cron Cron allows you to schedule commands to run automatically at set...

Installing and Configuring Apache

Installing and Configuring Apache Apache is the world's most widely deployed web server, known...