DEV Community

Faisal Rehman
Faisal Rehman

Posted on

Apache Server Configuration [Basics]

Apache is a world's leading web server that can easily be configured and maintained. Let's talk about some of its features:

 Apache is a web (HTTP) server that is written in C and it runs on the Default port ‘80’, it
handles mainly static pages.
 The default port of Apache Web server can be changed as:

cd /etc/httpd/conf

vim httpd.conf  There will be an option for Listen which is set to 80 by default, here you

can add the port of your own choice and after adding it Restart the httpd service using the
following command:

service httpd restart…………… (i)

 Apache can be started and stopped using the ‘start’ & ‘stop’ keywords instead of restart in
command (i).
 There is another way to start & stop the apache service as:

cd /usr/sbin  This directory contains a script for apache shown as:

apachectl start (or stop)

 The main difference between Apache & Tomcat is basically Apache is a web server
whereas tomcat is an application server. Apache is written in C & Tomcat is written in Java.
Apache handles mainly the static pages & Tomcat is dealt with dynamic pages (JSPs) & it
is formally known as Servlet Container.
 Apache can run on multiple ports, if you want to add more ports on which apache should
listen, go as:

cd /etc/httpd/conf

vim httpd.conf  Now here you can add number of ports under the “Listen” option and restart

the apache service.
Listen 91  Now apache will also listen on port 91.
 Httpd.conf is the mother file for Apache web server and all the configurations regarding
apache are done in it.
Apache Logs:
There are two types of Apache logs:
 Access logs
 Error logs
In order to view the apache logs go to the following direcotory:

/etc/httpd/logs

The tail –f command can be used to view the logs. In case of any error or issue, the error logs will
help you to check where the problem is. There is a best feature of apache that it saves the logs in
archive with their specific dates, for example error_log-20100519, now this archive contains all the
logs till 19 th of May 2010.
Apache Document Root:
It is the directory from where Apache web server serves the files and pages. The default Document
Root is located as:

/var/www/html

We can confirm the location of Apache Document Root with the help of following command:

/grep –i ‘DocumentRoot’ httpd.conf

If you are not present in the directory where the httpd.conf is placed then you will have to write the
above command as:

/grep –i ‘DocumentRoot’ /etc/httpd/conf/httpd.conf

Any how the Document Root can also be changed as we want, by editing the configuration file of
httpd, there will be the option available for Document Root which is set to /var/www/html by
default, change here the Document Root here like /opt/faisal and restart the httpd service and
check the document root again.
But it will not show the contents and files of that document root unless the path of Directory which is
also set to /var/www/html by default in httpd.conf, hence change it also and restart the httpd
service and check in the browser, hopefully it will be showing the contents of that document root.
Virtual Hosting:
Virtual Hosting is a method to host more than one domain names for a single IP. For example let
say we want to host two domain names like:
(i) www.example1.com
(ii) https://www.softwarespice.com
Now both of the above domains can be hosted easily using the method of Virtual Hosting over IP,
let say locally at 127.0.0.1 and in the web browser both URLs will have the same output.

In order to make a virtual host, go to /etc/httpd/conf and in the httpd.conf file, there will be a tag
for Virtual Host(if not then you will have to add it yourself).
Before creating a virtual host, let us discuss the types of Virtual Hosts. There are three major types
of virtual hosts:
i. Name-based Hosts
ii. Port-based Hosts
iii. IP-based Hosts
The most commonly used hosts are name and port based. It is strongly recommended that a
separate file should be made and all the necessary configurations are done in it and then finally
include that file in the httpd.conf file. For example we have made a file named http123.conf and
made some configurations in it and that file is saved in /etc/httpd/conf/http123.conf. After that
include that file in httpd.conf as:
Include /etc/httpd/conf/http123.conf (and then restart httpd service).
In order to do port-based virtual hosting, firstly include that port in httpd.conf file across Listen
option.
Example:
An example of name and port based virtual hosting is given as:
<VirtualHost *:91>
ServerAdmin webmaster@www.example1.com
DocumentRoot /www/docs/ www.example1.com
ServerName www.example1.com
ErrorLog logs/ www.example1.com -error_log
CustomLog logs/ www.example1.com -access_log common
</VirtualHost>
In the above virtual host the domain name www.example1.com is running on port 91,also define that
host in /etc/hosts file.

Alias:
An Alias is a name that Web Browsers use to access that directory as it is shorter than the original
path. We can create an alias for a command or for a very long path. For example, if we want to
create an alias for the path /etc/httpd/conf then:

alias apache=‘cd /etc/httpd/conf’

Whenever we will type apache in the shell it will move us to the conf directory as apache is the
alias name for the above conf directory.
Now we are going to create alias for a long URL, for example for the below URL;
http://www.example1.com/httpd/conf/abc/xyz/mmm/yyy/uuuu
Instead of writing the whole path we will create an alias as:
As we have already created a file http.conf and included it in the mother file of apache i.e.,
httpd.conf so we will write the alias in http.conf as:
Alias /uuuu /etc/httpd/conf/abc/xyz/mmm/yyy/uuuu (and restart the web server).
Now we will move directly to the contents of uuuu if we just write the alias name after the Domain
name as:
http://www.example1.com/uuuu


Top comments (0)