Education Series Part 05 – Setup and Configure Apache Web Server (CentOS 7)

Install Apache (httpd)

# yum -y install httpd

Enable the apache service so it starts when CentOS boots.

# systemctl enable httpd.service

Create the directory structure – I am going to configure this for virtual hosts from the get-go since that is the most common.

# mkdir -p /var/www/sample.com/public_html
# mkdir -p /var/www/sample2.com/public_html

Change permission so a regular user can modify the files:

# chown -R $USER:$USER /var/www/sample.com/public_html
# chown -R $USER:$USER /var/www/sample2.com/public_html

Modify permissions so that it has full read access:

# chmod -R 755 /var/www/

Add a quick demo page for each site:

# vi /var/www/sample.com/public_html/index.html
# cp /var/www/sample.com/public_html/index.html /var/www/sample2.com/public_html

Create two directories firsts sites-available which will hold all the virtual files, and then a sites-enabled to hold all of the symbolic links.

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

 Now to tell Apache to look for the sites-enabled by editing the httpd.conf

# vi /etc/httpd/conf/httpd.conf

Scroll to the end of the file (page down) and at the very end of the file add the following:

IncludeOptional sites-enabled/*.conf

Create the virtual hosts file.

# vi /etc/httpd/sites-available/sample.com.conf

Inside of the file fill it out appropriately:

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

Copy the file to the other domains.

# cp /etc/httpd/sites-available/sample.com.conf /etc/httpd/sites-available/sample2.com.conf

Modify the sample2.com.conf file to reflect the second domain.

# vi /etc/httpd/sites-available/sample2.com.conf
<VirtualHost *:80>
ServerName www.sample2.com
ServerAlias sample2.com
DocumentRoot /var/www/sample2.com/public_html
ErrorLog /var/www/sample2.com/error.log
CustomLog /var/www/sample2.com/requests.log combined
</VirtualHost>

Create symbolic links for the virtual hosts:

# ln -s /etc/httpd/sites-available/sample.com.conf /etc/httpd/sites-enabled/sample.com.conf
# ln -s /etc/httpd/sites-available/sample2.com.conf /etc/httpd/sites-enabled/sample2.com.conf

Since it’s a test on a local machine I modified my hosts file in order to test the site out:

#  vi /etc/hosts
10.0.0.15    sample.com
10.0.0.15    sample2.com

After this restart apache:

# systemctl restart httpd.service

Of course it failed.  I added the firewall rules.

# firewall-cmd --add-service=http 
# firewall-cmd --state
# firewall-cmd --list-all
# firewall-cmd --list-interfaces
# firewall-cmd --get-service
# firewall-cmd --query-service service_name
# firewall-cmd --add-port=8080/tcp
# systemctl restart firewalld

Still errored out ran apache config.

#  apachectl configtest

There was a problem with copying the sample2.com.conf file, I typoed it.  Changed the name, still failed.

Added exception to selinux:

# chcon --reference /var/log/httpd/error_log /var/www/sample.com/error.log
# chcon --reference /var/log/httpd/error_log /var/www/sample2.com/error.log
# chcon --reference /var/log/httpd/access_log /var/www/sample.com/request.log
# chcon --reference /var/log/httpd/access_log /var/www/sample2.com/request.log

Once again restart httpd.service

# systemctl restart httpd.service

This time it loaded up without any errors.  I opened up Firefox and went to sample.com and voila it worked.

Always and I mean always triple-check selinux!

Education Series Part 05 – Setup and Configure Apache Web Server (CentOS 7)

Leave a comment