Last Updated:

CentOs how to make a LAMP Server with Virtual Hosts

There are many control panels that can configure a generic LAMP server with any linux distribution or almost any. 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 split them up to help you identify any errors. This guide has been tested step by step and assumes that you have a clean centos 7 system installed with the network configured.

If you have the sudo command installed and are logged in as a normal user, you can simply enter the word ‘sudo’ before the commands. But now let's start configuring our ‘test server’.

Configuring the firewall

First of all, you must be sure that your server accepts certain requests on 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 router or a more complex network, I recommend reading an article on network theory or an article on how to configure virtual servers on a router to route traffic on certain ports. Here I assume that you have a public address on a server available on the Internet. So, you should type these commands at 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 have opened the ports to accept tcp connections on apache's default http (80) and httpd (443) ports. We now 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

Starting and Enabling Apache2

After installing apache, you must start and enable the service (daemon) with this command

  • systemctl start httpd
  • systemctl enable httpd

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

Configuring virtual hosts

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

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

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

SELINUX=disabled

Now we just need to create the configuration file for the virtual host, which we will place in the folder

/etc/httpd/conf.d/ directory.

In this article, we will use the domain example.com as an example. You will have to replace the domain name example.com with your own 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
AllowOverride All
Order allow,deny
allow from all

Obviously you must also create the destination folder for the site and logs if it does not exist:

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

At this point you must 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 concerning the configuration of virtual hosts with CentOS that they recommend creating a specific folder containing available sites and another containing enabled sites. This method is used in debian-based distributions and allows a site to be disabled and enabled simply by acting on the symbolic links. But this article is about CentOS, so we will follow the route of RedHat-based distributions.

Installation and configuration of Certbot (letsencrypt) for https support (optional but recommended)

If the site is dynamic, it may normally carry sensitive information, so it is a good idea to encrypt it. Purchasing a valid certificate used to be an expensive affair but now there is a system to acquire one completely free of charge.

Make sure that your DNS points to your server.

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

  • yum -y install mod_ssl
  • yum -y install openssl
  • yum -y install python-certbot-apache

In order to work, the https protocol must have a valid certificate during start-up. 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

/etc/ssl/certs/apache-selfsigned.crt

For this time, you can leave all the answers blank and will have to wait for the end of the certificate generation. Since certbot checks whether the apache configuration is congruent, we have to add the instructions for the virtual host for the https protocol to the end of the file as well

/etc/httpd/conf.d/example.com.conf

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

We can now create the certificate with certbot. Remember that if you use a service such as Cloudflare you will have to pause it temporarily.

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

Your domain should now be visible from http://www.example.com . If you want users to be automatically redirected to https, you will have to modify your virtual host's instructions regarding the http protocol by adding the line

Redirect ‘/’ ‘https://www.example.com’

to the configuration file

/etc/httpd/conf.d/example.com.conf

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
AllowOverride All
Order allow,deny
allow from all

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

Installing MariaDB (for Mysql)

To install the mysql MariaDB server you need to write this command

  • yum -y install mariadb-server

Then you need to start and enable the server

  • systemctl start mariadb
  • systemctl enable mariadb

Now mariadb is installed but you should do the basic auto-securing because as it is, the service is configured with as few limitations as possible to make it more convenient for developers. You can do this with the command

  • mysql_secure_installation

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

Considerations: CentOS has chosen to use MariaDB as the default mysql server. However, I must tell you that this is a fork of the classic mysql server and there may be some incompatibility problems when importing data. If you do not know the difference between mariadb and mysql community edition, I recommend reading 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 PHP and the modules used by the most common CMSs

To use mysql, apache requires an intermediate programme, which in our case is php. To install php with mysql support and the relevant modules used by the most popular CMSs, 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 modules with the word pecl inside the name, I just type

  • yum search php- | grep pecl

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