Create a New Site with Apache on ubuntu

Suppose you want to serve a new site under the domain example.com. In this article , we see how to configure apache to serve the new site.

Solution

step 1 - Create a Directory

Create a new directory site and create the following subdirectories inside:

  • public - This directory will contians the actual content of the site the should be served by apache.
  • logs/apache - this directory will contians the generated logs related the site
Directory Structure
 /sites/example            
 ├──────────────public ├ The site files. For example : index.html, index.php ...
 └──────────────apache ├ apache logs related to the site : error.log , access.log

step 2 - Create VirtualHost

Create a new config file /etc/apache2/sites-available/example.conf

Configuration for example.com
 <VirtualHost example.com:80>
     ServerName   example.com
     ServerAdmin  webmaster@example.com

     DocumentRoot "/sites/example/public"
     ErrorLog     "/sites/example/apache/error.log"
     CustomLog    "/sites/example/apache/access.log" combined
     
     <Directory "/sites/example/public">
         Options Indexes FollowSymLinks Includes ExecCGI
         AllowOverride All
         Require all granted
     </Directory>
 </VirtualHost>

step 3 - Enabling Site and Resatrt Apache

Enable the site using a2ensite command.

Enabling Site
$ a2ensite example.conf
Enabling site example.
To activate the new configuration, you need to run:
  systemctl reload apache2

As suggested, you should restart apache to activate the new site. Let’s do it:

Restart apache
$ systemctl reload apache2
Kobi
Kobi
Software Developer

Kobi helps entrepreneurs to translate their wishes to a working product.