Install and Configure Apache in Centos 7

Description:  Here I have explained how to install and configure Apache in Centos 7

Procedure:

  • First you need to install httpd using yum

# yum -y install httpd

  • After installation you need to start and enable service 

# systemctl  enable httpd.service
# systemctl start httpd.service

  • 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

Configure Name Based Virtual Host: 

  • If you have more than one domain need to host on same server then you need to configure Name based virtual host. 

Procedure: 

  • 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

ServerAdmin master@testdomain.com
ServerName testdomain.com
ServerAlias http://www.testdomain.com
DocumentRoot /var/www/html/testdomain.com/
ErrorLog /var/log/httpd/testdomain.com/error.log
CustomLog /var/log/httpd/testdomain.com/access.log

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

ServerAdmin master@testdomain2.com
ServerName testdomain2.com
ServerAlias http://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

  • Save file after make changes 
  • 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 testdomain.com 

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 . 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

Leave a comment