Featured
- Get link
- X
- Other Apps
Create a Virtual Host in Nginx on Ubuntu 24.04
Nginx uses server blocks (similar to Virtual Hosts in Apache) to host multiple websites on the same server. Each server block can have its own domain name, root directory, and configuration, making it possible to run several projects independently. In this tutorial, you’ll learn how to create a Virtual Host in Nginx on Ubuntu 24.04 step by step.
Prerequisite: Nginx Installation
Before creating a Virtual Host, make sure you have already installed and configured Nginx on Ubuntu 24.04. You can follow the installation guide here: Install Nginx on Ubuntu 24.04.
Create the Website Directory
First, create a new folder for your site inside /var/www/html
and add an index.html
file:
cd /var/www/html
mkdir mysite
cd mysite
nano index.html
Inside the file, paste this simple HTML content:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Hello, Nginx!</title>
</head>
<body>
<h1>Hello, Nginx!</h1>
<p>We have just configured our Nginx web server on Ubuntu Server!</p>
</body>
</html>
Save and close the file. This will serve as your test page.
Create the Server Block Configuration
Now create a new configuration file for your site in the sites-enabled
directory:
cd /etc/nginx/sites-enabled
sudo nano mysite
Add the following configuration:
server {
listen 80;
listen [::]:80;
server_name mysite.localhost;
root /var/www/html/mysite;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
This configuration tells Nginx to listen on port 80 for requests to mysite.localhost
, serving files from /var/www/html/mysite
with index.html
as the default page.
Restart Nginx
After saving the configuration, restart Nginx so the changes take effect:
sudo service nginx restart
Access Your Virtual Host
Open your browser and go to:
http://mysite.localhost
You should see the custom Hello, Nginx!
page you created earlier.
Conclusion
You have successfully created a Virtual Host (server block) in Nginx on Ubuntu 24.04. By organizing your websites into different server blocks, you can manage multiple domains or projects on the same server more efficiently and professionally.
- Get link
- X
- Other Apps
Popular Posts
Install PHP 8.1 with Apache2 on Ubuntu 24.04
- Get link
- X
- Other Apps
Comments
Post a Comment