1. Home
  2. Installing LAMP stack on CentOS 7

Installing LAMP stack on CentOS 7

What is LAMP stack?

LAMP stack is a combination of operating system and open source softwares stack which are significant in setting up a server initially. The acronym stands for Linux, Apache HTTP server, MySQL/MariaDB and PHP/Perl/Python. This article will guide you through the steps involved in installing Apache HTTP server, MySQL and PHP on your server assuming that the Operating system CentOS 7 is already installed in it.

PrerequisitePrior to following this article, you must have non root user account set up on your server.

Installing Apache HTTP server

Apache or Apache HTTP is the most commonly used web server around the world accounting for more than 50% of the market share currently. To install apache follow the steps below:

Step 1: Use the following commands in the terminal

sudo yum update

After the update process is finished, install Apache HTTP server using the command.

sudo yum install httpd

You will receive an authentication message, accept it by entering Y and continue with the installation. Then start your web server installed on the VPS by running the command

sudo systemctl start httpd.service

Step 2: Verify the installation by entering the following URL on your web browser.

http://server_ip_address/

If the web server has been installed properly, your web browser will direct you to the web page that looks similar to the following:

Installing MySQL(MariaDB)

MariaDB is an open source database management software which is quite similar in many ways to MySQL and is infact better than the latter in various performance aspects. So ideally it is a great alternative for the ever so popular MySQL.  MariaDB is a fork of MySQL which maintains high compatibility with MySQL and it is an integral part in many LEMP stacks installations worlwide.

Installing MariaDB on to a server is quite a simple process and in this article we will provide you with the steps on how to install and secure MariaDB on to a server running CentOS 7.

Step 1: Install MariaDB

Installing MariaDB is as easy as running a single command. Run the below command in the terminal to install.

sudo yum install mariadb-server

A prompt message will appear before completion of installation which resembles as follows

Is this ok [y/d/N]: 

Accept the prompt by entering y and then hit Enter.

Step 2: Start MariaDB and configure to start on boot

Now that MariaDB has been installed on your server, its time to start the service manually. This is done by running the following command in the terminal.

sudo systemctl start mariadb

Since this command won’t display any output after its execution, you need to verify if the service has started. You can verify the status of MariaDB by executing the below command in the terminal.

sudo systemctl status mariadb

When the service is up and running you will receive the following result:

Output:

mariadb.service - MariaDB database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled)
   Active: active (running) since Mon 2016-12-19 06:47:18 UTC; 2min 25s ago
  Process: 9179 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=0/SUCCESS)
  Process: 9099 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS)
 Main PID: 9178 (mysqld_safe)
   CGroup: /system.slice/mariadb.service
           ├─9178 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
           └─9336 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-fil...

Dec 19 06:47:16 Maria mariadb-prepare-db-dir[9099]: The latest information about MariaDB is available at http://mariadb.org/.
Dec 19 06:47:16 Maria mariadb-prepare-db-dir[9099]: You can find additional information about the MySQL part at:
Dec 19 06:47:16 Maria mariadb-prepare-db-dir[9099]: http://dev.mysql.com
Dec 19 06:47:16 Maria mariadb-prepare-db-dir[9099]: Support MariaDB development by buying support/new features from MariaDB
Dec 19 06:47:16 Maria mariadb-prepare-db-dir[9099]: Corporation Ab. You can contact us about this at sales@mariadb.com.
Dec 19 06:47:16 Maria mariadb-prepare-db-dir[9099]: Alternatively consider joining our community based development effort:
Dec 19 06:47:16 Maria mariadb-prepare-db-dir[9099]: http://mariadb.com/kb/en/contributing-to-the-mariadb-project/
Dec 19 06:47:16 Maria mysqld_safe[9178]: 161219 06:47:16 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
Dec 19 06:47:16 Maria mysqld_safe[9178]: 161219 06:47:16 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
Dec 19 06:47:18 Maria systemd[1]: Started MariaDB database server.

Ensure that is starts on boot up as well by running the following command.

sudo systemctl enable mariadb
Output:

Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.

Step 3: Secure MariaDB

You can improve the security of the MariaDB server by changing options like remote root logins and sample users. To implement this security step, use this command.

sudo mysql_secure_installation

A series of prompts will appear once the command is executed. First one would be to enter the root password, since there is no root password set as of now, hit Enter and continue.

Next you will be asked if you would like to set a new root password, accept it and set a new root password. Then Enter Y on all the prompts that follow which will finish the process.

Step 4: Verify the installation

To verify if the installation is working properly, run this command.

mysqladmin -u root -p version
Output:
Enter password: 
mysqladmin  Ver 9.0 Distrib 5.5.52-MariaDB, for Linux on x86_64
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Server version        5.5.52-MariaDB
Protocol version    10
Connection        Localhost via UNIX socket
UNIX socket        /var/lib/mysql/mysql.sock
Uptime:            23 min 10 sec

Threads: 1  Questions: 25  Slow queries: 0  Opens: 1  Flush tables: 2  Open tables: 27  Queries per second avg: 0.017

Installing PHP

PHP is a scripting language used in web designing to build dynamic web pages.

Step 1: Use the following command in the terminal

sudo yum install php php-mysql

You will receive a prompt message, enter y and finish the installation.

Step 2: Lots of additional PHP modules are available that can be installed easily. Even though this is an optional step, if you want to install additional modules follow the steps below.

To check the available modules, enter the command in the terminal

yum search php-
Output:

Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.fibergrid.in
 * extras: mirror.fibergrid.in
 * updates: mirror.fibergrid.in
========================================================================== N/S Matched: php- ===========================================================================
php-bcmath.x86_64 : A module for PHP applications for using the bcmath library
php-cli.x86_64 : Command-line interface for PHP
php-common.x86_64 : Common files for PHP
php-dba.x86_64 : A database abstraction layer module for PHP applications
php-devel.x86_64 : Files needed for building PHP extensions
php-embedded.x86_64 : PHP library for embedding in applications
php-enchant.x86_64 : Human Language and Character Encoding Support
php-fpm.x86_64 : PHP FastCGI Process Manager
php-gd.x86_64 : A module for PHP applications for using the gd graphics library
php-imap.x86_64 : A module for PHP applications that use IMAP
php-intl.x86_64 : Internationalization extension for PHP applications
php-ldap.x86_64 : A module for PHP applications that use LDAP
php-mbstring.x86_64 : A module for PHP applications which need multi-byte string handling
php-mysql.x86_64 : A module for PHP applications that use MySQL databases
php-odbc.x86_64 : A module for PHP applications that use ODBC databases
php-pdo.x86_64 : A database access abstraction module for PHP applications
php-pear.noarch : PHP Extension and Application Repository framework
php-pecl-apc.x86_64 : APC caches and optimizes PHP intermediate code
php-pecl-apc-devel.i686 : APC developer files (header)
php-pecl-apc-devel.x86_64 : APC developer files (header)
php-pecl-memcache.x86_64 : Extension to work with the Memcached caching daemon
php-pgsql.x86_64 : A PostgreSQL database module for PHP
php-process.x86_64 : Modules for PHP script using system process interfaces
php-pspell.x86_64 : A module for PHP applications for using pspell interfaces
php-recode.x86_64 : A module for PHP applications for using the recode library
php-snmp.x86_64 : A module for PHP applications that query SNMP-managed devices
php-soap.x86_64 : A module for PHP applications that use the SOAP protocol
php-tidy.x86_64 : Standard PHP module provides tidy library support
php-xml.x86_64 : A module for PHP applications which use XML
php-xmlrpc.x86_64 : A module for PHP applications which use the XML-RPC protocol
php-zts.x86_64 : Thread-safe PHP interpreter for use with the Apache HTTP Server

  Name and summary matches only, use "search all" for everything.

To know the details of the package or module type the following

yum info module-name

In place of module-name, enter the name of the corresponding module. For example,

yum info php-bcmath

To install that package, type the following

sudo yum install php-bcmath

You can install multiple php packages according to your requirements.

Step 3: To verify if php is installed properly into your VPS, create a file by using the command below

sudo vim /var/www/html/info.php

Press the key I to edit the file, enter the following content in the file and then save and exit by pressing ESC key followed by :wq! then hit Enter.

<?php
phpinfo();
?>

Restarting web server

The final step in installing the LAMP stack on CentOS 7 is to restart the Apacheweb server . This is done in order for the web server to recognize the changes done to the server.

sudo systemctl restart httpd

This will restart the server immediately and there you go. You have now installed LAMP stack on your server running on CentOS 7.

You can check the installation of php by entering the following URL in your web browser.

http://server_ip_address/info.php

Output:

It is highly recommend to remove this the php file that you created as it could provide important server data to unauthorized users. You can do this by running the below command in your terminal.

sudo rm /var/www/html/info.php
Updated on December 21, 2016

Was this article helpful?

Add A Comment