Check whether apache is already installed and running on your server. You can do this with the following command:<\/p>\n
dpkg -l apache2<\/pre>\nIf apache is not installed, you can do this by running the following commands. First, make sure that the system repositories are up to date:<\/p>\n
apt-get update<\/pre>\nTo install the Apache web server, execute the following:<\/p>\n
apt-get install apache2<\/pre>\nAfter the installation is complete, you should enable Apache to start automatically upon server reboot with:<\/p>\n
systemctl enable apache2<\/pre>\nYou can also check the status of your Apache service with the following command:<\/p>\n
systemctl status apache2<\/pre>\nNow that we are sure that Apache is installed and running on our server we can continue with the next step and set up our first virtual host.<\/p>\n
<\/span>3. What is a virtual host?<\/span><\/h2>\nApache virtual hosts are set of configuration directives which allow you to host as many websites as you want, using a single web server.Apache web server tsupports two types of virtual hosts:<\/p>\n
Name-based virtual hosts
\nIP based virtual hosts<\/p>\n
The name-based virtual host is commonly used to host multiple websites on the same server, while in IP based virtual host we can only configure one website on one IP address. In this tutorial will show you how to create name-based virtual hosts. For this purpose, we will host two websites using the following domain names, domain1.com<\/strong> and domain2.com<\/strong>. You can also replace them with your actual domain names.<\/p>\n<\/span>4. Create the webroot directories<\/span><\/h2>\nBefore setting up the virtual hosts, we will need to create the document root directories for our websites. Let’s create them in the \/var\/www\/<\/strong>html directory with the following commands:<\/p>\nmkdir -p \/var\/www\/html\/domain1.com\r\nmkdir -p \/var\/www\/html\/domain2.com<\/pre>\nLet’s also create a test demo page for each of our domain, so we can later test our configuration.
\nNavigate to the domain1.com<\/strong> document root directory:<\/p>\ncd \/var\/www\/domain1.com<\/pre>\nCreate a new index.html page with:<\/p>\n
nano index.html<\/pre>\nAnd add the following content:<\/p>\n
<html>\r\n <body>\r\n <center><h1>This is domain1.com!<\/h1><\/center>\r\n <\/body>\r\n<\/html><\/pre>\nNow lets, do the following for the domain2.com<\/strong> domain.<\/p>\ncd \/var\/www\/domain2.com\r\nnano index.html<\/pre>\nAnd add the following content:<\/p>\n
<html>\r\n <body>\r\n <center><h1>This is domain2.com!<\/h1><\/center>\r\n <\/body>\r\n<\/html><\/pre>\nWe have now successfully created the test pages for both domains. In order for our Apache webserver to be able to access these files, we also need to give them appropriate permissions and set the user and group to the www-data<\/strong>. We update the permissions to the whole \/var\/www\/<\/strong>html directory, with the following command.<\/p>\nchown -R www-data: \/var\/www\/html<\/pre>\n<\/span>5. Create the Virtual Host Files<\/span><\/h2>\nWe can now create our virtual host files. The virtual host configuration files usually end with .conf extension.
\nRun the following command to create the virtual host configuration file for our first domain, domain1.com<\/strong>:<\/p>\nnano \/etc\/apache2\/sites-available\/domain1.com.conf<\/pre>\nAnd add the following content to the file:<\/p>\n
<VirtualHost *:80>\r\n\r\nServerAdmin admin@domain1.com\r\nServerName domain1.com\r\nServerAlias www.domain1.com\r\nDocumentRoot \/var\/www\/html\/domain1.com\r\n\r\nErrorLog ${APACHE_LOG_DIR}\/domain1.com_error.log\r\nCustomLog ${APACHE_LOG_DIR}\/domain2.com_access.log combined\r\n\r\n<\/VirtualHost><\/pre>\nNow, let’s do the same for our second domain name, domain2.com<\/strong>:<\/p>\nnano \/etc\/apache2\/sites-available\/domain2.com.conf<\/pre>\nAnd add the following code:<\/p>\n
<VirtualHost *:80>\r\n\r\nServerAdmin admin@domain2.com\r\nServerName domain2.com\r\nServerAlias www.domain2.com\r\nDocumentRoot \/var\/www\/html\/domain2.com\r\n\r\nErrorLog ${APACHE_LOG_DIR}\/domain2.com_error.log\r\nCustomLog ${APACHE_LOG_DIR}\/domain2.com_access.log combined\r\n\r\n<\/VirtualHost><\/pre>\nHere is a short explanation about each line in our virtual host files.<\/p>\n
The following lines shows that the virtual host is listening on port 80:<\/p>\n
<VirtualHost *:80><\/pre>\nThe ServerAdmin sets the contact address that the server includes in any error messages it returns to the client. You can specify your email address here, or even remove the line.<\/p>\n
ServerAdmin admin@domain1.com<\/pre>\nServerName is the domain name and the ServerAlias defines additional names that should match as if they are the original domain names.<\/p>\n
ServerName domain1.com\r\nServerAlias www.domain1.com<\/pre>\nThe DocumentRoot defines the location where Apache should look for when processing a request for the domain defined in ServerName or ServerAlias.<\/p>\n
DocumentRoot \/var\/www\/html\/domain1.com<\/pre>\nThe last two lines, define the location of the log files:<\/p>\n
ErrorLog ${APACHE_LOG_DIR}\/domain1.com_error.log\r\nCustomLog ${APACHE_LOG_DIR}\/domain1.com_access.log combined<\/pre>\n