Photo by Taylor Vick on Unsplash
Create a New Apache Site
A Step-by-Step Guide to Creating a New Site in Apache2
Install Apache: If you haven't already done so, install Apache using the following command
sudo apt-get update
sudo apt-get install apache2
Create a new directory for your site under the /var/www
directory. For example, if you want to create a site called example.com
, you can create a directory called /var/www/example.com
sudo mkdir /var/www/example.com
#Create an index.html file inside the new directory with the following command and the below contnet
sudo vim /var/www/example.com/index.html
<!DOCTYPE html>
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
<p>This is a test page.</p>
</body>
</html>
Set the correct file permissions for the new directory and file
sudo chown -R www-data:www-data /var/www/example.com
sudo chmod -R 755 /var/www/example.com
Create a new virtual host configuration file for your site
sudo vim /etc/apache2/sites-available/example.com.conf
Inside the file, type the following configuration:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Enable the new virtual host configuration and restart Apache
sudo a2ensite example.com.conf
sudo systemctl restart apache2
Finally, add an entry to your /etc/hosts
file to map the domain name (example.com)
to the IP address of your local machine,
# on the machine you have browser access ,
sudo vim /etc/hosts
# add the below line by replacing with your local server IP address
your-IP example.com www.example.com
Note: If you have a real server and Domain name you don't need this, you just have to Add a DNS record to your domain name provider
Finally, Test the Application from your Browser