How To Set Up Apache Virtual Hosts on CentOS 7

install apache

yum -y install httpd

Create the Directory Structure

mkdir -p /var/www/example.com/public_html
mkdir -p /var/www/example2.com/public_html

Grant Permissions

chown -R $USER:$USER /var/www/example.com/public_html
chown -R $USER:$USER /var/www/example2.com/public_htm

 

ensure that read access is permitted to the general web directory

chmod -R 755 /var/www

Create New Virtual Host Files

mkdir /etc/httpd/sites-available
mkdir /etc/httpd/sites-enabled


include the vhosts directory to the main httpd config file

nano /etc/httpd/conf/httpd.conf

Add this line to the end of the file:

IncludeOptional sites-enabled/*.conf

Create the First Virtual Host File

nano /etc/httpd/sites-available/example.com.conf

 

add the below content to the vhost file

<VirtualHost *:80>

    ServerName www.example.com
    ServerAlias example.com
    DocumentRoot /var/www/example.com/public_html
    ErrorLog /var/www/example.com/error.log
    CustomLog /var/www/example.com/requests.log combined
</VirtualHost>

Copy First Virtual Host and Customize for Additional Domains

cp /etc/httpd/sites-available/example.com.conf /etc/httpd/sites-available/example2.com.conf

edit the second vhost file

nano /etc/httpd/sites-available/example2.com.conf

edit the second vhost file as below

<VirtualHost *:80>
    ServerName www.example2.com
    DocumentRoot /var/www/example2.com/public_html
    ServerAlias example2.com
    ErrorLog /var/www/example2.com/error.log
    CustomLog /var/www/example2.com/requests.log combined
</VirtualHost>

Enable the New Virtual Host Files

ln -s /etc/httpd/sites-available/example.com.conf /etc/httpd/sites-enabled/example.com.conf
ln -s /etc/httpd/sites-available/example2.com.conf /etc/httpd/sites-enabled/example2.com.conf

restart apache, and here u go 🙂

 

shenno