When coming from Apache to Nginx, many administrators miss the convenient a2ensite
/a2dissite
commands for managing virtual hosts. While Nginx doesn't include equivalent commands by default, the nginx-common package in Ubuntu/Debian provides this functionality.
First, ensure you have the package installed:
sudo apt-get install nginx-common
The package provides three essential commands:
sudo nginx-enable example.com
sudo nginx-disable example.com
sudo nginx-list-sites
These commands follow the same principle as Apache's tools:
# Enabling creates symbolic link
ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
# Disabling removes the link
rm /etc/nginx/sites-enabled/example.com
Here's a complete workflow for a new site:
# 1. Create config in sites-available
sudo nano /etc/nginx/sites-available/myapp.conf
# 2. Enable the site
sudo nginx-enable myapp
# 3. Verify configuration
sudo nginx -t
# 4. Reload Nginx
sudo systemctl reload nginx
# 5. List active sites
sudo nginx-list-sites
If you prefer not to use the package, you can create custom scripts:
# /usr/local/bin/ngxensite
#!/bin/bash
if [ -f "/etc/nginx/sites-available/$1" ]; then
ln -s "/etc/nginx/sites-available/$1" "/etc/nginx/sites-enabled/$1"
else
echo "Site $1 doesn't exist in sites-available"
fi
Remember that after enabling/disabling sites, you need to:
sudo nginx -t # Test configuration
sudo systemctl reload nginx
While Apache users enjoy the convenience of a2ensite
/a2dissite
commands for managing virtual hosts, Nginx traditionally lacks such built-in utilities. This often forces administrators to manually create symlinks between sites-available
and sites-enabled
directories.
The package you're remembering is nginx-common, which provides two crucial scripts:
sudo apt-get install nginx-common
After installation, you'll gain access to:
nginx-enable
- Activates a site configurationnginx-disable
- Deactivates a sitenginx-list
- Shows enabled/available sites
Enable a site (creates symlink automatically):
sudo nginx-enable example.com.conf
Disable a site (removes symlink):
sudo nginx-disable example.com.conf
List all sites:
sudo nginx-list
For systems without nginx-common, here's the traditional method:
sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
Ensure your Nginx config files follow this pattern in /etc/nginx/sites-available/
:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Remember to test configurations before reloading:
sudo nginx -t