LAMP on CentOS – Apache

A LAMP (Linux, Apache, MySQL, PHP) stack is a common web stack used for hosting web content. Let’s install Apache on a CentOS 7 server.

APACHE

Apache is a free, open source and popular HTTP Server that runs on Unix-like operating systems including Linux and also Windows OS. Since its release 20 years ago, it has been the most popular web server powering several sites on the Internet. It is easy to install and configure to host single or multiple websites on a same Linux or Windows server.

In this article, we will explain how to install, configure and manage Apache HTTP web server on a CentOS 7 or RHEL 7 server using command line.

Prerequisites:

Centos / RHEL 7 server install
Centos / RHEL 7 system with static IP address

Before You Begin

To check your hostname run:

# hostname
# hostname -f

The first command should show your short hostname,
while the second should show your fully qualified domain name (FQDN).

1. Update your system

1. First update the system software packages to the latest version.

# yum -y update

2. Install Apache

Next, install Apache HTTP server from the default software repositories using the YUM package manager as follows.

# yum install httpd

3. Manage Apache HTTP Server on RHEL/CentOS 7

Once Apache web server installed, you can start it first time and enable it to start automatically at system boot.

# systemctl start httpd
# systemctl enable httpd
# systemctl status httpd

Now configure your system to start Apache at boot time…

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

4. Configure firewalld to Allow Apache Traffic

By default, CentOS 7 built-in firewall is set to block Apache traffic. To allow web traffic on Apache, update the system firewall rules to permit inbound packets on HTTP and HTTPS using the commands below.

To be able to access the web server from outside, we have to open the HTTP (80) and HTTPS (443) ports in the firewall. The default firewall on CentOS is firewalld which can be configured with the firewalld-cmd command.

# firewall-cmd --permanent --zone=public --add-service=http
# firewall-cmd --permanent --zone=public --add-service=https
# firewall-cmd --reload

5. Test Apache HTTP Server

Now you can verify Apache server by going to the following URL
http://localhost
or
http://192.168.1.100
Note: change numbers to match your own servers IP
a default Apache page will be shown.

6. Configure Name-based Virtual Hosts

This section only useful, if you want to host more than one domain (virtual host) on the same Apache web server. There are many ways to setup a virtual host, but we will explain one of the simplest methods here.
1. First create a vhost.conf file under /etc/httpd/conf.d/ directory to store multiple virtual host configurations.
# vi /etc/httpd/conf.d/vhost.conf

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

 

NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin webmaster@mytecmint.com
ServerName mytecmint.com
ServerAlias www.mytecmint.com
DocumentRoot /var/www/html/mytecmint.com/
ErrorLog /var/log/httpd/mytecmint.com/error.log
CustomLog /var/log/httpd/mytecmint.com/access.log combined
</VirtualHost>

 

Important: You can add as many as domains to vhost.conf file, just copy the VirtualHost block above and change the values for each domain you add.

3. Now create the directories for mytecmint.com website as referenced in the VirtualHost block above.

# mkdir -p /var/www/html/mytecmint.com [Document Root - Add Files]
# mkdir -p /var/log/httpd/mytecmint.com [Log Directory]

Step 4: Create a dummy index.html page under /var/www/html/mytecmint.com.
# echo "Welcome to My TecMint Website" > /var/www/html/mytecmint.com/index.html

Step 5: Finally, restart Apache service for the above changes to take effect.
# systemctl restart httpd.service

Step 6: Now you can visit mytecmint.com to test the index page created above.

Apache Important Files and Directoires

  • The default server root directory (top level directory containing configuration files): /etc/httpd
  • The main Apache configuration file: /etc/httpd/conf/httpd.conf
  • Additional configurations can be added in: /etc/httpd/conf.d/
  • Apache virtual host configuration file: /etc/httpd/conf.d/vhost.conf
  • Configurations for modules: /etc/httpd/conf.modules.d/
  • Apache default server document root directory (stores web files): /var/www/html

Step by step, installing Apache, MariaDB, PHP, and phpMyAdmin

  1. Part 1: Install Apache on RHEL/CentOS 7
  2. Part 2: Install MariaDB on RHEL/CentOS 7
  3. Part 3: Install PHP on RHEL/CentOS 7
  4. Part 4: Install phpMyAdmin on RHEL/CentOS 7

Thats all for now!

You may also like: