The Apache web server (apache2 in debian or httpd in red hat) is one of the most popular and powerful web servers used by most people in the world because of its ease of administration and flexibility. The following steps are involved for installation and setting up the Apache server:
- Installig httpd server
In this step we will install httpd server in centos in the server.
sudo yum -y install httpd - for red hat based systems like centos sudo apt-get install apache2 - for debian based systems like ubuntu
- Configure the httpd as service and starting httpd server
We should enable apache server to start at boot once the system restarts and we can start apache server.
For Debian based systems:
sudo systemctl enable apache2 - To enable as a service sudo systemctl start apache2 - To start the service, up and running
For redhat based systems:
sudo systemctl enable httpd - To enable as service sudo systemctl start httpd - To start and running the service
- Change the root directory of Apache
In this step, we are going to change the root directory of apache which “/var/www/html” to any custom directory configured in the system under “/var/www” folder itself in the steps below.
- Creating sub directory called “myapplication” under “www” folder
cd /var/www/ sudo mkdir myapplication
2. Creating backup of configuration file before making any changes to configuration.
For RHEL systems sudo cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.backup For Debian based systems sudo cp /etc/apache/apache2.conf /etc/apache/apache2.conf.backup
3. Editing the configuration file – Open the configuration file any editor of your choice, find like called DocumentRoot “/var/www/html” as default and change that line as mentioned below.
For RHEL systems vi /etc/httpd/conf/httpd.conf DocumentRoot "/var/www/yum/" change to “/var/www/myapplication” For Debian based systems vi /etc/apache2/apache2.conf DocumentRoot "/var/www/html/" change to “/var/www/myapplication/”
- Once you make any changes to the configuration file we need to restart the server to effect the changes.
For RHEL systems sudo service httpd restart For Debian based systems sudo systemctl restart apache2.service
- Some other httpd or apache2 commands list
- httpd -v or apache2 -V : Outputs the build date and version of the Apache server.
- httpd -l or apache2ctl -M or ls /etc/apache2/mods-enabled/ : Lists compiled in Apache modules
- httpd status : Only works if mod_status is enabled and shows a page of active connections
- To restart apache server
- sudo systemctl restart httpd
- sudo systemctl restart apache2.service
- To know the status of apache server
- sudo systemctl status httpd or sudo service httpd status
- sudo systemctl status apache2.service
Note: When you are making any changes to configuration, make sure that you do not make any typos and make sure to always restart Apache server after making changes to its configuration file.