Nginx Ubuntu Setup
Configuring Nginx on Ubuntu
-
Create a new server block configuration file:
Terminal window
sudo vim /etc/nginx/sites-available/defaultThis command opens the default configuration file for editing.
Replace the file content with the following content:
server { listen 80; # Listen on port 80, the default HTTP port server_name localhost; # The server name, here it is set to localhost root /var/www/html; # The root directory where files are served from index index.html index.htm; # The default files to serve location / { try_files $uri $uri/ =404; # Try to serve the requested URI, if not found return a 404 }}Create the document root directory if it doesn’t exist:
Terminal window
sudo mkdir -p /var/www/htmlTerminal window
sudo chown -R $USER:$USER /var/www/htmlTerminal window
sudo chmod -R 755 /var/www/htmlCreate the directory for the web root. Change ownership of the directory to the current user Set the permissions for the directory
Create a sample index.html file:
Terminal window
cd /var/www/htmlNavigate to the web root directory
Terminal window
touch index.htmlCreate an empty index.html file
Terminal window
sudo vim index.htmlOpen the index.html file for editing
Add the following content to the index.html file:
<html> <head> <title>Welcome to Nginx</title> </head> <body> <h1>Hello, world!</h1> </body></html>Test the Nginx configuration
Terminal window
sudo nginx -tTest the Nginx configuration for syntax errors
Reload Nginx to apply the changes:
Terminal window
sudo systemctl reload nginxReload the Nginx service to apply the changes