# - - # # - Run multiple websites on Ubuntu Server - Apache2 - # http://www.unixmen.com/setup-apache-virtual-hosts-on-ubuntu-15-10/ Setup Apache Virtual Hosts 1. Create Virtual Directories Now, let us proceed to setup virtual hosts. As I mentioned earlier, I am going to host two virtual hosts called “unixmen1.local”, and “unixmen2.local”. Create a public directory to place the two virtual hosts data. First, let us create a directory for unixmen1.local site: sudo mkdir -p /var/www/html/unixmen1.local/public_html Then, create the directory for unixmen2.local site: sudo mkdir -p /var/www/html/unixmen2.local/public_html 2. Setting Up Ownership and Permissions The above directories are owned by root user now. We should change the ownership of these two directories to the regular user. sudo chown -R $USER:$USER /var/www/html/unixmen1.local/public_html/ sudo chown -R $USER:$USER /var/www/html/unixmen2.local/public_html/ The “$USER” variable indicates the currently logged in user. Set the read permissions to the Apache web root (/var/www/html/) directory, so that everyone can read files from that directory. sudo chmod -R 755 /var/www/html/ We have created the directories for holding the websites data and assigned the necessary permissions and ownership to them. 4. Create Sample pages for Virtual Hosts Now, we have to create the sample pages to be served through the websites. First, let us create a sample page to the unixmen1.local virtual host. Create a ‘index.html’ for unixmen1.local virtual host, sudo vi /var/www/html/unixmen1.local/public_html/index.html Add the following contents: www.unixmen1.local

Welcome To Unixmen1.local website

Save and close the file. Similarly, add the sample page to the second virtual host. sudo vi /var/www/html/unixmen2.local/public_html/index.html Add the following contents: www.unixmen2.local

Welcome To Unixmen2.local website

Save and close the file. 5. Create Virtual Host Files By default, Apache comes with a default virtual host file called 000-default.conf. We will copy the 000-default.conf file contents to our new virtual host files. sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/unixmen1.local.conf sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/unixmen2.local.conf Make sure the virtual host files contains .conf extension at the end. Now, modify the unximen1.local.conf file to reflect with our new own values. sudo vi /etc/apache2/sites-available/unixmen1.local.conf Make the relevant changes that reflect to the unixmen1 site. # 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@unixmen1.local ServerName unixmen1.local ServerAlias www.unixmen1.local DocumentRoot /var/www/html/unixmen1.local/public_html # 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 Like wise, modify the second virtual host file. sudo vi /etc/apache2/sites-available/unixmen2.local.conf Make the relevant changes that reflect to the unixmen2 site. # 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@unixmen2.local ServerName unixmen2.local ServerAlias www.unixmen2.local DocumentRoot /var/www/html/unixmen2.local/public_html # 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 After modifying the virtual hosts files, disable the default virtual host (000.default.conf), and enable new virtual hosts as shown below. sudo a2dissite 000-default.conf sudo a2ensite unixmen1.local.conf sudo a2ensite unixmen2.local.conf Finally, restart the apache service. In Ubuntu 15.10/15.04: sudo systemctl restart apache2 In Ubuntu 14.10 and earlier versions: sudo service apache2 restart That’s it. Now, we successfully configured the apache virtual hosts on our Ubuntu server. Testing Virtual Hosts Edit file /etc/hosts, sudo vi /etc/hosts Add the virtual domain names one by one as shown below. [...] 192.168.1.103 unixmen1.local 192.168.1.103 unixmen2.local Save and close the file. Note: In case, you want to access the above sites from a remote system, you need to add the above two lines to your remote system’s /etc/hosts/ file as well. Don’t forget to replace the IP address with your own. Open up your browser and point to the URL http://unixmen1.local or http://unixmen2.local. You should see the sample pages which we created earlier. Different document: - In addition of the above. Typically sites on Apache are handled through *.conf files in the /etc/apache2/sites-available directory. Here is an example from my server, which is hosting www.brownvpn.tk, www.browntech.tk, www.techyteen.tk, and a couple of others. # 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 browntech.tk ServerAdmin your@email.com DocumentRoot /var/www/html/tech # 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 RewriteEngine on RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [L,QSA,R=permanent] The comments are very helpful. Each file needs to be pretty much the same as this. However, you may want to remove the Rewrite lines if you don't have SSL enabled or you want to allow HTTP as well as HTTPS traffic. We see up at the top the two most important lines. ServerName and DocumentRoot. The ServerName field should be the domain that points to that DocumentRoot path. So, you will need two configuration files(four if you want www.domain1.com as well as domain1.com). cd /etc/apache2/sites-available/ touch site1.conf touch site2.conf Then, in your preferred editor, copy and paste my example configuration file, making the two edits (three if you count setting it to your email, four if you take out those rewrite lines) into each of site1.conf and site2.conf. sudo a2ensite site1 site2 sudo service apache2 restart That will enable the two sites and redirect the URLs accordingly.