How to Enable a Site in Nginx: Step-by-Step Guide
To enable a site in
nginx, create a configuration file in /etc/nginx/sites-available/ and then create a symbolic link to it in /etc/nginx/sites-enabled/. Finally, reload nginx with sudo systemctl reload nginx to apply the changes.Syntax
The process to enable a site in nginx involves two main directories: sites-available and sites-enabled. You place your site configuration file in sites-available and then create a symbolic link to it inside sites-enabled. This tells nginx which sites to serve.
/etc/nginx/sites-available/: Stores all site configuration files./etc/nginx/sites-enabled/: Contains symbolic links to enabled site configs.ln -s: Command to create symbolic links.sudo systemctl reload nginx: Reloadsnginxto apply changes without downtime.
bash
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/ sudo systemctl reload nginx
Example
This example shows how to enable a site called example.com. First, create the configuration file in sites-available. Then, link it to sites-enabled and reload nginx to activate the site.
bash
sudo nano /etc/nginx/sites-available/example.com
# Inside the file, add:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
# Save and exit the editor
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginxOutput
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Common Pitfalls
Common mistakes when enabling a site in nginx include:
- Not creating the symbolic link in
sites-enabled. - Forgetting to reload or restart
nginxafter changes. - Incorrect file permissions on the site root directory.
- Syntax errors in the configuration file causing
nginxto fail.
Always test your configuration with sudo nginx -t before reloading.
bash
## Wrong: Missing symbolic link # sudo systemctl reload nginx ## Right: Create symbolic link and reload sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/ sudo nginx -t sudo systemctl reload nginx
Quick Reference
| Step | Command or Action | Description |
|---|---|---|
| 1 | Create config in /etc/nginx/sites-available/ | Write your site configuration file here |
| 2 | sudo ln -s /etc/nginx/sites-available/site /etc/nginx/sites-enabled/ | Enable site by linking config |
| 3 | sudo nginx -t | Test configuration syntax |
| 4 | sudo systemctl reload nginx | Reload nginx to apply changes |
Key Takeaways
Enable a site by linking its config from sites-available to sites-enabled.
Always test your nginx config with 'sudo nginx -t' before reloading.
Reload nginx after enabling a site to apply changes without downtime.
Check file permissions and syntax to avoid common errors.
Use symbolic links to manage multiple site configurations easily.