Setting Up Nginx for Your Domain on Ubuntu
Towhidul Islam
AI & Web Innovator | Helping You Grow with Smart MVPs, Chatbots & CRMs | Scalable AI Agents & Custom Websites for Ambitious Brands | Business-Focused Solutions That Drive Success | Software Engineer Book a free meeting.
Setting Up Nginx for Your Domain on Ubuntu
This guide will walk you through the process of installing Nginx on an Ubuntu server, setting up a new website directory, configuring permissions, and enabling your site.
Prerequisites
- A server running Ubuntu (20.04 or newer recommended)
- Root or sudo user access
Steps
1. Install Nginx
First, ensure your package list is up-to-date and install Nginx:
sudo apt update
sudo apt install nginx
2. Create a Directory for Your Domain
Create a directory to hold your website files. Replace meow-domain with your actual domain name or project name:
sudo mkdir /var/www/meow-domain
3. Set Directory Permissions
Set the appropriate permissions for the directory. This ensures that the web server has the necessary read and execute permissions:
sudo chmod 755 -R /var/www/meow-domain
4. Set Ownership
Change the ownership of the directory to the www-data group, which is the default user group used by Nginx:
sudo chown -R root:www-data /var/www/meow-domain
5. Create Nginx Server Block Configuration
Create a new server block file for your domain. This file will define how Nginx handles requests for your domain. Open a new configuration file in a text editor (e.g., nano):
sudo nano /etc/nginx/sites-available/meow-domain
Add the following configuration to the file, replacing meow-domain with your actual domain name:
server {
listen 80;
server_name meow-domain;
root /var/www/meow-domain;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
Save and close the file (in nano, press Ctrl+X, then Y, then Enter).
领英推荐
6. Test Nginx Configuration
Before enabling the new site, test the Nginx configuration to ensure there are no syntax errors:
sudo nginx -t
7. Disable the Default Site
To avoid conflicts with the default Nginx configuration, unlink the default site configuration:
sudo unlink /etc/nginx/sites-enabled/default
8. Enable Your New Site
Create a symbolic link from the sites-available directory to the sites-enabled directory to enable your new site configuration:
sudo ln -s /etc/nginx/sites-available/meow-domain /etc/nginx/sites-enabled
9. Test Nginx Configuration Again
Test the Nginx configuration once more to ensure everything is set up correctly:
sudo nginx -t
10. Restart Nginx
Finally, restart Nginx to apply the changes:
sudo systemctl restart nginx
11. Verify Your Setup
Open a web browser and navigate to your domain (e.g., https://meow-domain). You should see the default Nginx welcome page if everything is set up correctly.
Additional Considerations
- Firewall Configuration: Ensure your firewall allows HTTP traffic (port 80). If using ufw, you can enable it with:
sudo ufw allow 'Nginx Full'
- SSL/TLS: For a production environment, it is recommended to secure your site with SSL/TLS. You can use Let's Encrypt to obtain a free SSL certificate and configure it with Nginx.
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d meow-domain
By following these steps, you will have successfully installed and configured Nginx to serve your website.