1. Create A Public Directory For Your Websites

Make sure you are in your top-level directory by typing:

$ cd /home/username

Permanently Delete A Directory

If you need to delete a directory and all its sub-folders, navigate one level above the directory_to_delete and type: rm -rf directory_to_delete

2. Create the Structure for Your Web Directory

$ mkdir -p public_html/domain1.com/{public,private,log,cgi-bin,backup}

Now, create a “Hello World” HTML5 page:

$ nano public_html/domain1.com/public/index.htm

Below is some sample HTML. We will customize this page later in the course with your LAMP Stack 101 badge

<!DOCTYPE html>
<html>
  <head>
    <title>domain1.com</title>
  </head>
 <body>

   <h1>Hello World</h1>
  </body>
</html>

Exit and save this file as index.htm

Create Your Virtual Host (V-Host) File

A virtual host file allows you to associate multiple websites to a single IP address. The number of sites you can reasonably host is only limited by the amount of RAM on your VPS.

$ sudo nano /etc/apache2/sites-available/domain1.com.conf
# Place any notes or comments you have here
# It will make any customization easier to understand in the weeks to come

# domain: domain1.com
# public: /home/demo/public_html/domain1.com/

<VirtualHost *:80>

  #Optional: Restrict access to site
  <Directory /home/username/public_html/domain1.com/>
    Order allow,deny
    Allow from 152.55.454.25
    Allow from 127
  </Directory>

  # Admin email, Server Name (domain name) and any aliases
  ServerAdmin webmaster@email-address-i-always-check.com
  ServerName  domain1.com
  ServerAlias www.domain1.com

  # Index file and Document Root (where the public files are located)
  DirectoryIndex index.htm index.php
  DocumentRoot /home/username/public_html/domain1.com/public

  # Custom log file locations
  LogLevel warn
  ErrorLog  /home/username/public_html/domain1.com/log/error.log
  CustomLog /home/username/public_html/domain1.com/log/access.log combined

 # Error Docs: be sure these files exists before uncommenting
 # ErrorDocument 404 /errors/404.html
 # ErrorDocument 403 /errors/403.html

</VirtualHost>

Remember: You can’t just make up names and place them in ServerName or ServerAlias. You must first have your DNS zone properly configured to map those names to an IP address associated with your server. For example, for the “www” alias shown above is created by mapping its CNAME entry to either @.

CNAME ALIASCNAME VALUE
www@

3. Enable Your Site

To enable your new site, simple type:

$ sudo a2ensite domain1.com.conf

Disabling a Site

To disable a site type sudo a2dissite domain1.com.conf and reload apache.

Show Which Sites Are Enabled

Just type cd /etc/apache2/sites-enabled && ls -al and you’re done!

Reload Apache after enabling/disabling site:

$ sudo service apache2 reload