Installing and Configuring Nginx
Installing Nginx on Ubuntu 22.04
To install Nginx on an Ubuntu 22.04 system, you can use the following command:
sudo apt-get update
sudo apt-get install nginx
This will install the latest version of Nginx on your system.
Configuring Nginx
After installing Nginx, you can start configuring it to suit your needs. Nginx's configuration files are located in the /etc/nginx/
directory.
Configuring Server Blocks
Nginx uses server blocks (also known as virtual hosts) to handle different domains or websites. You can create a new server block by creating a new file in the /etc/nginx/sites-available/
directory. For example, to create a server block for the domain example.com
, you can create a file named example.com
with the following content:
server {
listen 80;
server_name example.com;
root /var/www/example.com;
index index.html index.htm index.nginx-debian.html;
location / {
try_files $uri $uri/ =404;
}
}
This configuration tells Nginx to listen for incoming HTTP requests on port 80 and serve content from the /var/www/example.com
directory.
Enabling the Server Block
To enable the server block, you need to create a symbolic link from the /etc/nginx/sites-available/
directory to the /etc/nginx/sites-enabled/
directory:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
Reloading Nginx Configuration
After making changes to the Nginx configuration, you need to reload the configuration for the changes to take effect. You can do this using the following command:
sudo systemctl reload nginx
This will reload the Nginx configuration without interrupting the running Nginx process.
Verifying Nginx Installation and Configuration
You can verify the Nginx installation and configuration by opening a web browser and navigating to http://example.com
. If the server block is configured correctly, you should see the default Nginx welcome page.