How to Host a Website in Apache Web Server Ubuntu

In this tutorial you are going to learn How to host a website in Apache Web Server Ubuntu.
Prerequisite
How to install Apache Web Server in Ububtu
1. Create a folder for the website that you want to host in var/www/html
You can run the following command to go to html folder in ubuntu
cd var/www/html
2. Create site directory in html folder
for example i created a directory example.com in html folder. To create a directory in html directory run the following command
sudo mkdir example.com
Output

Now create a sample page in var/www/html/example.com run the following command
sudo nano index.html
Copy and Pase the following code in index.html and press CTRL + X and then type Y and press Enter to save the file
<html> <head> <title>Example Site</title> </head> <body> <h1>Congratulations, Example.com site successfully created</h1> </body> </html>
3. Host a website in apache
To host a website navigate to apache installation directory. The default installation directory is etc/apache2. Run the following command to go in the apache directory
cd etc/apache2
now navigate to the sites-avalable directory which is located in etc/apache2. Run the following command
//Run this command to go in the sites-available directory and press enter cd sites-available //Run this command it will list all the sites available in apache server ls //Now copy default site, in my case i want to host example.com domain sudo cp 000-default.conf example.com.conf
Output

4. Edit example.com.conf and make changes as mention below
To edit the example.com.conf site run the following command
sudo nano example.com.conf
Now replace the content of example.com.conf with following code
<VirtualHost *:80> # The ServerName directive sets the request scheme, hostname and port that # the server uses to identify itself. This is used when creating # redirection URLs. In the context of virtual hosts, the ServerName # specifies what hostname must appear in the request's Host: header to # match this virtual host. For the default virtual host (this file) this # value is not decisive as it is used as a last resort host regardless. # However, you must set it for any further virtual host explicitly. ServerName www.example.com ServerAdmin webmaster@localhost DocumentRoot /var/www/html/example.com # Available loglevels: trace8, ..., trace1, debug, info, notice, warn, # error, crit, alert, emerg. # It is also possible to configure the loglevel for particular # modules, e.g. #LogLevel info ssl:warn ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined # For most configuration files from conf-available/, which are # enabled or disabled at a global level, it is possible to # include a line for only one particular virtual host. For example the # following line enables the CGI configuration for this host only # after it has been globally disabled with "a2disconf". #Include conf-available/serve-cgi-bin.conf </VirtualHost> # vim: syntax=apache ts=4 sw=4 sts=4 sr noet
5. Now enable the site in apache server
To enable the site in apache run the following command
sudo a2ensite example.com.conf
6. Restart apache server
Run the following command to restart the apache server
sudo service apache2 restart
Add your server ip address in your domain and wait for few minutes then enter domain name in your browser
i.e example.com