Up to $140 off-Lowest  Prices of 2024(Nov.21-Dec.2)

My Computer Tips

Home | About | Categories | All Tips & Tutorials

Install Apache2 on Ubuntu and Ubuntu based distributions in 2024

ID: 307

Category: Ubuntu

Added: 26th of June 2022

Updated On: Tutorial updated and rechecked on 4th of November 2024

Views: 1,698



Click here to buy Apache2 Web Server books on Amazon
Affiliate Link ⓘ

This is a basic guide to installing Apache2 on Ubuntu and Ubuntu based distributions

Install Apache2 by entering the following command

sudo apt-get install apache2

Once installed enter localhost or 127.0.0.1 in the address of your web browser. You will now see the Apache holding page.

Create a new folder in the /var/www/ directory named mysite
sudo mkdir /var/www/mysite

Create an index.html file in the mysite folder
sudo touch /var/www/mysite/index.html

Next we need to open the index.html file in nano and add some basic html code.
sudo nano /var/www/mysite/index.html

Copy and paste the code below
<!DOCTYPE html>
<html>
<body>
<h1>Apache 2 successfully installed</h1>
<p>Welcome to mysite</p>
</body>
</html> 


To save the index.html file in nano, do the following
Press CTRL +O to write to file, press enter to confirm
Press CTRL +X to exit nano

Next we need to take ownership of the folder and files in the mysite folder.
If you run the ls -l command you will see that root has ownership of the folder and file
ls -l /var/www/mysite

drwxr-xr-x 2 root root 4096 Jun 25 14:03 mysite

Enter the following command to change ownership to your username. Replace username with your real username
sudo chown -R username:username /var/www/mysite/

Run the ls -l command again
ls -l /var/www/mysite

drwxr-xr-x 2 username username 4096 Jun 25 14:03 mysite

Next change the permissions of the folder and file to enable read, write
sudo chmod -R 755 /var/www/mysite/

So that apache2 points to your new mysite folder, we need to copy the 000-default.conf file

The main 000-default.conf file is located in the following directory /etc/apache2/sites-available/ so cd to that directory using the command below
cd /etc/apache2/sites-available/

Copy the 000-default.conf file using the cp command and create a new .conf file named mysite.conf
sudo cp 000-default.conf mysite.conf

Open and edit the mysite.conf file using nano
sudo nano mysite.conf

Locate the line
DocumentRoot /var/www/html
Change this to
DocumentRoot /var/www/mysite


To save the configuration file in nano, do the following
Press CTRL +O to write to file, press enter to confirm
Press CTRL +X to exit nano

Next we need to enable the mysite.conf file

We first need to disable the 000-default.conf using the ad2dissite command
sudo a2dissite 000-default.conf

Then we need to enable the mysite.conf file using the a2ensite command
sudo a2ensite mysite.conf

Next we need to restart apache2 using the following command
sudo systemctl reload apache2

Enter localhost or 127.0.0.1 in the address of your webrowser. You will now see your Apache holding page.