Thursday, June 21, 2018

Install and Configure Apache in Centos 7

Description:  Apache is most widely usable Web Server Application in the world. It supports multiple features like compiled as a separate modules and extend its functionality. Virtual hosting is one feature which allow single server to server multiple number of different website.  
Here I have explained how to install and configure Apache in Centos 7 and setup virtual hosting using different method. 

Procedure:
  • You can install via default package manager using below command
# yum -y install httpd
  • After installation you need to start and enable service 
# systemctl  enable httpd.service
# systemctl start httpd.service

Install Apache from Source: You can also install Apache from source using below method. Download source in  /usr/local/src folder
# cd /usr/local/src
# gzip -d httpd-2.2.26.tar.gz
# tar xvf httpd-2.2.26.tar
# httpd-2.2.26
#./configure --help
#./configure –prefix=/usr/local/apache –enable-so
# make
# make install

  • In order to see all configuration option available for Apache, you can use ./configure –help option.  The most common configuration option is –prefix={install directory name}
    
      
     
     
      
      
  • Configure firewall to allow httpd traffic using below command 
# firewall-cmd --zone=public --permanent --add-service=http
# firewall-cmd --zone=public --permanent --add-service=https
# firewall-cmd --reload
  • Test your installation by browse default page in browser 
http://SERVER_DOMAIN_NAME_OR_IP


Virtual Host:  An apache server can host multiple website on Same server. You do not  need to have multiple machine and apache software in each server. This can achieved by using virtualhost or vhost

Types Of  Apache Virtual Host

  • Name-based virtual host 
  • Address-based  or IP based virtual host

Name-based virtual host: It is use to host multiple virtual sites on single address.
  • First create vhost.conf file under /etc/httpd/conf.d/ to store multiple vhost configuration 
# vi /etc/httpd/conf.d/vhost.conf

Add the following example virtual host directive template for website testdomain.com, make sure to change the necessary values for your own domain


NameVirtualHost *:80

<10.0.0.1:80>
ServerAdmin master@testdomain.com
ServerName testdomain1.com
ServerAlias www.testdomain1.com
DocumentRoot /var/www/html/testdomain1.com/
ErrorLog /var/log/httpd/testdomain1.com/error.log
CustomLog /var/log/httpd/testdomain1.com/access.log
</VirtualHost>

######## Additional Domain ################

<10.0.0.1:80>
ServerAdmin master@testdomain2.com
ServerName testdomain2.com
ServerAlias www.testdomain2.com
DocumentRoot /var/www/html/testdomain2.com/
ErrorLog /var/log/httpd/testdomain2.com/error.log
CustomLog /var/log/httpd/testdomain2.com/access.log
</VirtualHost>


  • Save file after make changes 
  • You can Check syntax of configuration file by using httpd -t
# httpd -t 
  • You can add more virtual host as you require. 
  • Make sure to create error log and custom log folder as defined in virtual host file.
  • Restart httpd service after chagnes
# systemctl restart httpd.service 

  • Now you can visit to testdomain1.com  and testdomain2.com

IP-based virtual host: In order to setup IP based virtual hosting, you need more than one IP address configured on your server.  So the number of virtual host will require number of IP address. i.e If you have 10 Virtual Host you require 10 IP Address.


# vi /etc/httpd/conf.d/vhost.conf

Add the following example virtual host directive template for website testdomain.com and testdomain2.com, make sure to change the necessary values for your own domain


Listen 10.0.0.3:80

<VirtualHost 10.0.0.1:80>
ServerAdmin master@testdomain.com
ServerName testdomain1.com
ServerAlias www.testdomain1.com
DocumentRoot /var/www/html/testdomain1.com/
ErrorLog /var/log/httpd/testdomain1.com/error.log
CustomLog /var/log/httpd/testdomain1.com/access.log
</VirtualHost>

######## Additional Domain ################

<VirtualHost 10.0.0.2:80>
ServerAdmin master@testdomain2.com
ServerName testdomain2.com
ServerAlias www.testdomain2.com
DocumentRoot /var/www/html/testdomain2.com/
ErrorLog /var/log/httpd/testdomain2.com/error.log
CustomLog /var/log/httpd/testdomain2.com/access.log
</VirtualHost>



Setup Apache Password Protected Directory with htpasswd
  • By default Apache does not allow the use of .htaccess files in CentOS 7. You will need to set up Apache to allow .htaccess based authentication. You can do this by editing the Apache config file
# vi /etc/httpd/conf/httpd.conf
Find the section that begins with <Directory "/var/www/html">. Change the line from AllowOverride none to AllowOverride AuthConfig

AllowOverride AuthConfig
  • Create a password file with htpasswd
# htpasswd -c /etc/httpd/.htpasswd user1

You will be asked to supply and confirm a password for user1.

.htpasswd file created  and it looks like as follow
 user1:$apr1$0r/2zNGG$jopiWY3DEJd2FvZxTnugJ/

  • Now, you need to allow the apache user to read the .htpasswd file.
# chown apache:apache /etc/httpd/.htpasswd
# chmod 0660 /etc/httpd/.htpasswd

Now you need to create a .htaccess file in the web directory you wish to restrict.
For this example, we will create the .htaccess file in the /var/www/html/ directory to restrict the entire document root.

vi /var/www/html/.htaccess

Add the following content:

AuthType Basic
AuthName "Restricted Content"
AuthUserFile /etc/httpd/.htpasswd
Require valid-user
  • Save file and restart service. 
  • Test it by browse URL in browser. You will prompt for username and password

No comments:

Post a Comment