How to install LAMP (Linux Apache, MariaDB & PHP) on CentOS 7

Leave reply

lamp on centos 7 vps In the following article we are going to show you how to install LAMP (Linux Apache, MariaDB & PHP) on a CentOS 7 VPS.

What is LAMP?

LAMP is actually an acronym for a web services solution stack consisting of Linux, the Apache HTTP Server, the MySQL or MariaDB database engines, and the PHP, Perl or Python programming language. All components are free and open-source software, and the combination is suitable for building dynamic web pages.

Requirements?

We are using our SSD 1 VPS hosting plan for this set-up, which should be more than enough to run LAMP and host multiple dynamic websites using virtual host directives.

 

UPDATE THE SYSTEM

As always, make sure your CentOS 7 VPS is fully up-to-date and you’re in a screen session using the commands below:

## screen -U -S lamp-centos7
## yum update

 

INSTALL MARIA DB

MariaDB is the default database server in CentOS 7, so go ahead and install it with yum using the following command:

## yum install mariadb mariadb-server mysql

Once installed, add bind-address = 127.0.0.1 to /etc/my.cnf.d/server.cnf to bind MariaDB to localhost only:

## vim /etc/my.cnf.d/server.cnf

[mysqld]
#log-bin=mysql-bin
#binlog_format=mixed
bind-address = 127.0.0.1

Restart the MariaDB database server and enable it to start on system start-up using:

## systemctl restart mariadb
## systemctl status mariadb
## systemctl enable mariadb

Finally, you can run the mysql_secure_installation post-installation script to finish the MariaDB set-up. For example:

## mysql_secure_installation

Enter current password for root (enter for none): ENTER
Set root password? [Y/n] Y
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y

 

INSTALL APACHE

OK so far so good. Next, proceed with the installation of Apache, OpenSSL and mod_ssl using yum as in:

## yum install httpd openssl mod_ssl

Restart Apache and add it to automatically start on your system start-up using:

## systemctl restart httpd
## systemctl status httpd
## systemctl enable httpd

Test the set-up by opening, for example http://YOUR_SERVER_IP/ in your favorite browser. You should see the default Apache page, for example:

centos-default-apache-page

Great. Now let’s configure some extra Apache options, for example, hide its version number, its banner etc… by adding the following to /etc/httpd/conf.d/options.conf

## vim /etc/httpd/conf.d/options.conf

TraceEnable off

## Disable Signature
ServerSignature Off

## Disable Banner
ServerTokens Prod

Test Apache configuration file and restart the webserver for the changes to take effect:

## apachectl configtest
## systemctl restart httpd
## systemctl status httpd

 

INSTALL PHP

In order to be able to run PHP based web applications, we have to add PHP support to the Apache webserver, so let’s install some required packages with yum:

## yum install php php-mysql

also, you may want to install some other PHP extensions required by your applications. Here is the list:

php-bcmath          : A module for PHP applications for using the bcmath library
php-cli             : Command-line interface for PHP
php-common          : Common files for PHP
php-dba             : A database abstraction layer module for PHP applications
php-devel           : Files needed for building PHP extensions
php-embedded        : PHP library for embedding in applications
php-enchant         : Enchant spelling extension for PHP applications
php-fpm             : PHP FastCGI Process Manager
php-gd              : A module for PHP applications for using the gd graphics library
php-intl            : Internationalization extension for PHP applications
php-ldap            : A module for PHP applications that use LDAP
php-mbstring        : A module for PHP applications which need multi-byte string handling
php-mysql           : A module for PHP applications that use MySQL databases
php-mysqlnd         : A module for PHP applications that use MySQL databases
php-odbc            : A module for PHP applications that use ODBC databases
php-pdo             : A database access abstraction module for PHP applications
php-pear.noarch     : PHP Extension and Application Repository framework
php-pecl-memcache   : Extension to work with the Memcached caching daemon
php-pgsql           : A PostgreSQL database module for PHP
php-process         : Modules for PHP script using system process interfaces
php-pspell          : A module for PHP applications for using pspell interfaces
php-recode          : A module for PHP applications for using the recode library
php-snmp            : A module for PHP applications that query SNMP-managed devices
php-soap            : A module for PHP applications that use the SOAP protocol
php-xml             : A module for PHP applications which use XML
php-xmlrpc          : A module for PHP applications which use the XML-RPC protocol

OK, let’s continue with PHP configuration. Edit /etc/php.ini and set the following:

## vim /etc/php.ini

date.timezone = America/New_York
memory_limit = 64M
expose_php = Off

restart Apache using systemctl for the changes to take effect:

## systemctl restart httpd
## systemctl status httpd

Test if PHP module is loaded in Apache using:

## httpd -M | grep php

also

## php -v

You can create a test info.php script too using the following command:

## echo -e "<?php\n\tphpinfo();" > /var/www/html/info.php

and access it with the following command:

## curl -I $(curl -s icanhazip.com)/info.php

 

SET-UP APACHE VHOSTS

Now, you would have to set-up Apache Virtual Host Directives, to be able to host multiple domains, even SSL powered ones using one IP address. So, create /etc/httpd/conf.d/vhosts.conf and add the following:

## cat /etc/httpd/conf.d/vhosts.conf

# Load my vhosts
IncludeOptional vhosts.d/*.conf

This tells Apache, to load the configuration files ending with .conf which reside in /etc/httpd/vhosts.d. As you can guess, this is the place where we put our Virtual Hosts, so let’s set-up one for domain1.com and another for domain2.net

VHOST for domain1.com

## cat /etc/httpd/vhosts.d/domain1.com.conf

<VirtualHost YOUR_SERVER_IP:80>
    ServerAdmin webmaster@domain1.com
    DocumentRoot "/var/www/html/domain1.com"
    ServerName domain1.com
    ServerAlias www.domain1.com
    ErrorLog "/var/log/httpd/domain1.com-error_log"
    CustomLog "/var/log/httpd/domain1.com-access_log" combined

    <Directory "/var/www/html/domain1.com/">
        DirectoryIndex index.html index.php
        Options FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

VHOST for domain2.net

## cat /etc/httpd/vhosts.d/domain2.net.conf

<VirtualHost YOUR_SERVER_IP:80>
    ServerAdmin webmaster@domain2.net
    DocumentRoot "/var/www/html/domain2.net"
    ServerName domain2.net
    ServerAlias www.domain2.net
    ErrorLog "/var/log/httpd/domain2.net-error_log"
    CustomLog "/var/log/httpd/domain2.net-access_log" combined

    <Directory "/var/www/html/domain2.net/">
        DirectoryIndex index.html index.php
        Options FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

Finally, restart Apache for the changes to take effect:

## apachectl configtest
## systemctl restart httpd
## systemctl status httpd

Of course you don’t have to do any of this if you use one of our Linux VPS Hosting services, in which case you can simply ask our expert Linux admins to install LAMP for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

 

One Response to “How to install LAMP (Linux Apache, MariaDB & PHP) on CentOS 7”

  1. How to install LEMP (Linux, Nginx, MariaDB & PHP-FPM) on a CentOS 7 VPS | RoseHosting.com Linux VPS Hosting Blog

    […] instead, you are looking for how to set-up LAMP, then please refer to our guide on how to install LAMP (Linux Apache, MariaDB & PHP) on a CentOS 7 […]

    Reply

Leave a Reply