Why Nginx?
Nginx is the most used web server in the world. It is faster and more efficient than Apache for high-traffic sites, consumes less memory, and is ideal for serving static files and acting as a reverse proxy.
Install Nginx
On Ubuntu/Debian:
apt update
apt install nginx -y
systemctl start nginx
systemctl enable nginx
apt install nginx -y
systemctl start nginx
systemctl enable nginx
On AlmaLinux/CentOS:
yum install nginx -y
systemctl start nginx
systemctl enable nginx
systemctl start nginx
systemctl enable nginx
Configure a Website (Virtual Host)
Create a configuration file for your domain:
nano /etc/nginx/conf.d/yourdomain.conf
Paste this basic configuration:
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
root /var/www/yourdomain;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
}
listen 80;
server_name yourdomain.com www.yourdomain.com;
root /var/www/yourdomain;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
}
Basic Nginx Commands
systemctl start nginx # Start
systemctl stop nginx # Stop
systemctl restart nginx # Restart
nginx -t # Test configuration
systemctl stop nginx # Stop
systemctl restart nginx # Restart
nginx -t # Test configuration
⚠️ Always test the configuration with
nginx -t before restarting. A syntax error can bring down your web server.