If you’re looking to set up a web server on Ubuntu, Nginx is a popular choice due to its speed and reliability. In this guide, we’ll go through the steps How to set up a web server on Ubuntu using Nginx
Step 1: Install Nginx
The first step in setting up Nginx on Ubuntu is to install it. This can be done using the following command in the terminal:
sudo apt-get update
sudo apt-get install nginx
This will download and install the latest version of Nginx onto your Ubuntu machine.
Step 2: Start Nginx
Once Nginx is installed, you can start it by running the following command:
sudo systemctl start nginx
You can also check the status of Nginx using the following command:
sudo systemctl status nginx
If Nginx is running correctly, you should see a message indicating that it is active and running.
Step 3: Configure Firewall
By default, Ubuntu comes with a firewall called UFW (Uncomplicated Firewall). If you have UFW enabled, you’ll need to configure it to allow traffic to your web server. The following commands will allow HTTP and HTTPS traffic:
sudo ufw allow 'Nginx HTTP'
sudo ufw allow 'Nginx HTTPS'
Step 4: Create a website directory
Now that Nginx is installed and running, you can create a directory to store your website files. The default directory for Nginx is /var/www/html/
, so we’ll create a directory there:
sudo mkdir /var/www/html/mywebsite
Step 5: Create a test page
Next, we’ll create a test page to ensure that Nginx is serving web pages correctly. You can create a file called index.html
in your website directory with the following command:
sudo nano /var/www/html/mywebsite/index.html
This will open the Nano text editor. Enter the following HTML code:
<!DOCTYPE html>
<html>
<head>
<title>My Website</title>
</head>
<body>
<h1>Welcome to my website!</h1>
<p>This is a test page.</p>
</body>
</html>
Once you’ve entered the code, save the file by pressing CTRL+O
and then exit Nano by pressing CTRL+X
.
Step 6: Configure Nginx to serve your website
Now that you’ve created a test page, you’ll need to configure Nginx to serve it. Nginx configuration files are located in the /etc/nginx/
directory. The default configuration file is nginx.conf
, but we’ll create a new configuration file for our website.
Create a new configuration file by running the following command:
sudo nano /etc/nginx/sites-available/mywebsite
This will open the Nano text editor. Enter the following code:
server {
listen 80;
listen [::]:80;
root /var/www/html/mywebsite;
index index.html;
server_name example.com www.example.com;
location / {
try_files $uri $uri/ =404;
}
}
Replace example.com
and www.example.com
with your actual domain name. If you don’t have a domain name, you can use your server’s IP address instead.
Save the file by pressing CTRL+O
and then exit Nano by pressing CTRL+X
.
Step 7: Enable the website
Once you’ve created the configuration file, you’ll need to enable it by creating a symbolic link from the sites-available
directory to the sites-enabled
directory