If you are reading this chances are you are looking on how to host multiple websites on a single server.
This guide is going to cover installing Apache2 and creating multiple virtual hosts, for different domains and / or subdomains. I will show examples in Ubuntu and CentOS. I started with a fresh install of both operating systems for this tutorial.
Note: I never suggest logging in as root. I would also run the service as a non-root user.
Ubuntu #
Step 1: Install Apache2 #
sudo apt-get install apache2Step 2: Create Directory Structure #
Personally I like the directory structure organized so I would do something like below.
sudo mkdir /var/www/domain1.com
sudo mkdir /var/www/domain2.comStep 3: Create/Edit Your Virtual Hosts #
For this example you can just copy and paste the code below for each of your domains.
sudo nano /etc/apache2/sites-available/domain1.comBe sure to change the information to your specific info.
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName domain1.com
ServerAlias www.domain1.com domain1.com
DocumentRoot /var/www/domain1.com
<Directory /var/www/domain1.com/>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/domain1.com-error.log
CustomLog ${APACHE_LOG_DIR}/domain1.com-access.log combined
</VirtualHost>Then repeat for additional domains.
Step 4: Enabling Your Virtual Hosts #
For each file you created above run the following command.
sudo a2ensite domain1.comOnce you have enabled all of your virtual hosts you then run.
sudo service apache2 reloadAs long as your domains are pointed to your network and port 80 is open your domains should be directed to the correct virtual host.
CentOS #
Step 1: Install httpd #
sudo yum install httpdStep 2: Have Apache Start at Boot #
sudo chkconfig -levels 235 httpd on
sudo service httpd startor with CentOS 7
sudo systemctl enable httpd
sudo systemctl start httpdStep 3: Create Your Directory Structure #
sudo mkdir /var/www/domain1.com
sudo mkdir /var/www/domain2.comRepeat for any additional sites.
Step 4: Configure Your Virtual Hosts #
Open the httpd.conf
sudo vi /etc/httpd/conf/httpd.confAdd the following lines to the bottom of httpd.conf be sure to hit “i” to insert text.
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName www.domain1.com
ServerAlias www.domain1.com domain1.com
Document Root /www/domain1.com
ErrorLog logs/domain1.com-error.log
CustomLog logs/domain1.com-access.log combined
</VirtualHost>Repeat for each additional host you want to add.
Save the file by hitting Esc and typing:
:wqand hitting enter.
Step 5: Restart the Apache HTTPD #
sudo service httpd restartor in CentOS 7
sudo systemctl restart httpd