CentOs Installing and Configuring an L.A.M.P. Server (Linux Apache Mysql Server) with Multiple Virtual Hosts

There are plenty of control panels that can configure a generic LAMP server with any linux or almost linux distribution. Other paid ones are also compatible with other platforms such as windows.

Many commands in this guide can be aggregated into one, however I prefer to divide them to help you identify any errors. This guide has been tested step by step and assumes that you have installed a clean centos 7 system with the network configured.

If you have the sudo command installed and you are logged in as a regular user, you can simply enter the word “sudo” before the commands. But now we’re starting to set up our “test server.”

Configure the firewall

First, you need to make sure that your server accepts certain requests at certain ports. If the firewall is not installed do not install it unless you know what you are doing or if you have the physical machine in front of you. If you have a more complex router or network, I recommend that you read an article about network theory or an article about how to configure virtual servers on a router to route traffic to certain ports. Here I assume that you have a public address on a server available on the internet. So, you should write these commands on your prompt (shell).

firewall-cmd --permanent --zone=public --add-port=80/tcp
firewall-cmd --permanent --zone=public --add-port=443/tcp
firewall-cmd --runtime-to-permanent
firewall-cmd --reload

Install Apache2

We opened ports to accept tcp connections in apache’s default http (80) and httpd (443) ports. Now we need to install apache2 as a simple web server by writing this command:

rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch-rpm
yum -y install epel-release
yum -y install httpd

Start-up and Enable Apache2

After installing apache, you need to use and enable the service (demon) with this command

systemctl start httpd
systemctl enable httpd

If you put the ip address of the server on a browser you should see the welcome page of apache and centos.

Configure virtual hosts

To configure virtual hosts on CentOS you should disable the selinux or you will receive an error. To temporarily disable it, thus avoiding having to restart the system, you can write

setenforce 0
echo 0 > /sys/fs/selinux/enforce

To make sure that this change persists even after the reboot you should edit the /etc/sysconfig/selinux file and be sure that the line regarding selinux is disabled as shown in this example:


Now we just have to create the configuration file for the virtual host that we’ll put inside the folder


In this article, we’ll use the example.com domain as an example. You will need to replace the example.com domain name with your domain name.

Then create the configuration file and enter the following lines:

ServerName www.example.com
DocumentRoot /var/www/example.com
ServerAlias example.com
ErrorLog /var/log/httpd/www.example.com.error.log
CustomLog /var/log/httpd/www.example.com.requests.log combined

Options FollowSymLinks MultiViews
AllowOvverride All
Order allow,deny
allow from all

Of course, you also need to create the site and log destination folder if it doesn’t exist:

mkdir -p /var/www/example.com
mkdir -p /var/log/httpd

At this point you need to restart apache

apachectl restart

If your domain points to the server correctly, you should see the Welcome page.

Considerations: I have read in many articles about configuring virtual hosts with CentOS that recommend creating a specific folder containing the available sites and another containing the enabled sites. This method is used in debian-based deployments and allows you to disable and enable a site simply by acting on symbolic links. But this article is about CentOS and so we will follow the path of RedHat-based distributions.

Installing and configuring Certbot (letsencrypt) for https support (optional but recommended)

If the site is dynamic, it could normally carry sensitive information and so it’s a good idea to encrypt it. Buying a valid certificate was once an expensive operation but now there is a system to acquire one completely for free.

Make sure your DNS points to your server.

The first thing you need to do is install apache modules for ssl support

yum -y install mod_ssl
yum -y install openssl

And install the certbot program

yum -y install python-certbot-apache

To work, at startup, the https protocol must have a valid certificate. We can create this certificate as follows

mkdir -p /etc/ssl/private
chmod 700 /etc/ssl/private
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/apache-selfsigned.key -out

For this time, you can leave all the answers blank and you will have to wait until the end of the certificate generation. Since certbot checks if the apache configuration is consistent, we need to add instructions for the virtual host also for the https protocol at the end of the file


SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/example.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem
ServerName www.example.com
DocumentRoot /var/www/example.com
ServerAlias example.com
ErrorLog /var/log/httpd/www.example.com.error.ssl.log
CustomLog /var/log/httpd/www.example.com.requests.ssl.log combined

Options FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all

Now we can create the certificate with certbot. Keep in me that if you use a service such as Cloudflare, you’ll need to temporarily pause it.

apachectl stop
certbot certonly --standalone -d example.com -d
apachectl start

Your domain should now http://www.example.com be visible from . If you want users to be automatically redirected to https, you’ll need to change your virtual host’s instructions for the http protocol by adding the line

Redirect "/" "https://www.example.com"

configuration file


as shown in this example

ServerName www.example.com
DocumentRoot /var/www/example.com
ServerAlias example.com
Redirect "/" "https://www.example.com"
ErrorLog /var/log/httpd/www.example.com.error.log
CustomLog /var/log/httpd/www.example.com.requests.log combined

Options FollowSymLinks MultiViews
AllowOvverride All
Order allow,deny
allow from all

If you use services such as cloudflare or other redirect methods it will not insert this line or it will generate an endless loop of addressing that will eventually return an error on the browser.

Installation MariaDB (for Mysql)

To install the mysql MariaDB server, you must write this command

yum -y install mariadb-server mariadb

So you’ll need to start and enable the server

systemctl start mariadb
systemctl enable mariadb

Now mariadb is installed but you should proceed to basic automatic security because as it is, the service is configured with as few limitations as possible so that it is more convenient for developers. You can do this with the command


You will need to choose all the recommended options and you will need to enter a password for your mysql root account.

Considerations: CentOS chose to use MariaDB as the default mysql server. However I have to tell you that this is a fork of the classic mysql server and there may be incompatibility issues when importing data. If you don’t know the difference between mariadb and mysql community edition I recommend you read an article on how to migrate data from a mysql community release server to a mariadb server or how to migrate data from a mariadb server to a mysql community release server.

Installing PHPs and modules used by common CMSs

To use mysql, apache needs an intermediate program that in our case is php. To install php with mysql support and its most commonly used modules from popular CMS, type these commands

yum -y install php
yum -y install php-opcache
yum -y install php-common
yum -y install php-fpm
yum -y install php-pear
yum -y install php-mysql
yum -y install php-cli
yum -y install php-gd
yum -y install php-odbc
yum -y install php-xml
yum -y install php-xmlrpc
yum -y install php-mbstring
yum -y install php-snmp
yum -y install php-soap
yum -y install php-mcrypt

If you need to install other modules you can search for them using the command

yum search php-

You can also filter the results with the grep command. If for example, i want to display the modules with pecl written inside the name I just have to write

yum search php- | grep pecl

Considerations: CenOS is a stable and conservative linux distribution. Packages that you can install with traditional repositories are tested. To install the latest versions, you should import other repositories. If you don’t need to install the latest version of php, I recommend that you respect centOS’s choice. If you need to install version 7 of php, I recommend you read an article about installing and configuring a LAMP (Linux Apache Mysql Php 7) server with multiple virtual hosts on CentOS 7.

It's possible to leave a comment as registered users to the site, accessing through social, wordpress account or as anonymous users. If you want to leave a comment as an anonymous user you will be notified by email of a possible response only if you enter the email address (optional). The insertion of any data in the comment fields is totally optional. Whoever decides to insert any data accepts the treatment of these last ones for the inherent purposes of the service that is the answer to the comment and the strictly necessary communications.

Leave a Reply