Recommended VPS Complete list of best VPS hosting providers.

How to Install LAMP on CentOS 7

Tutorial to guide newbie how to install complete LAMP stack with Apache, MariaDB MySQL and PHP5 on CentOS 7. Full screenshot pics inlcluded. This is the full all-in-page guide of LAMP stack installation series I wrote for CentOS 6 before. There is no significant difference except the use of new systemctl command. You can read a short introduction (information) about why and what is systemctl command in my previous post.

LAMP which originally stands for Linux, Apache, MySQL and PHP has now recently changed with the rise of MariaDB, a drop-in replacement for original MySQL. Long story short, MariaDB is a fork of MySQL and developed by MySQL developers itself. It has almost all features of what MySQL has and features library binary equivalency and exact matching with MySQL APIs and commands. It means if an app is able to run with MySQL and it also is able to on MariaDB without any glitch. I will not explain what is Apache and PHP as I’ve explained before and I believe you already knew what it is.

Let’s start with the step by step tutorial:

You may need:

  1. A server (VPS / Dedi) running CentOS 7. I recommend you to use CentOS 7 x86_64 minimal if available. As usual, I’m using a Playground Server from DigitalOcean.
  2. A knowledge on how to use Putty or Terminal to access a server via SSH.
  3. I believe you knew –at least part ofmost common Unix commands used to manage an unmanaged server.
  4. A spare time of your life and a cup of coffee.

Install Apache Web Server

Being the most popular web server, Apache is commonly included in most of recent Linux Distro so installation will be very easy.

Before you proceed to the next steps, it is better to explain that all commands in this tutorial are written without the “sudo” prefix. In this tutorial I use root but you may also login as separate user with root privilege. However if you disabled root login and you logged in using another username with root privilege, you can add the “sudo” prefix all by your self. Alternatively you can simply type su, hit Enter and type in your password twice to switch as root.

Step 1 – Login to your server via Putty or Terminal.

lamp-centos-7-1

Step 2 – Now issue command below to install Apache 2.4 on your CentOS 7 server:

yum install httpd -y

As you can see the command is still the same.

lamp-centos-7-2

And when the process finished, you’ll see something like this:

lamp-centos-7-2-b

Step 3 – Now you have Apache 2.4 installed which you can then start the service by typing command below:

systemctl start httpd.service

or,..

service httpd start

lamp-centos-7-3

Available commands:

systemctl status|start|stop|restart|reload httpd.service

# OR, old command :

service httpd status|start|stop|restart|reload

Step 4 – You can verify that Apache is really running by opening your favorite web browser and access your vps via its IP address:

http://xxx.xxx.xxx.xxx

and you’ll see default Apache welcome page.

lamp-centos-7-apache

In current example I’ve installed Apache v2.4.6.

or, you can directly issue this command:

systemctl status httpd.service

you’ll see something like this:

lamp-centos-7-4-1

Enable Apache to automatically run every time your server reboot:

systemctl enable httpd.service

Install PHP5

Step 5 – Now, it is time to install PHP5. Default command is:

yum install php -y

That’ really is a simple command but since we will install MySQL then we’ll need PHP MySQL modules plus any other PHP5 modules you website / app may need it. You can view all available modules using this command:

yum search php-

Confused? You can read my previous article or you can simply use command below that includes common PHP5 modules most websites can run with it.

yum install php php-common php-cli php-devel php-gd php-imap php-intl php-mysql php-process php-xml php-xmlrpc php-zts -y

lamp-centos-7-5

And once done, you’ll see something like this:

lamp-centos-7-5-1

You can test which version of PHP is installed by typing php -v command.

lamp-centos-7-5-php-version

In my example it is PHP v5.4.16.

Install MariaDB MySQL Server

Step 6 – Installing MariaDB mysql server on CentOS 7 is pretty easy and once again we’ll make us of yum package manager:

yum install mariadb-server mariadb -y

lamp-centos-7-6

and once done you’ll see something like this:

lamp-centos-7-6-1

Step 7 – Now you can start MariaDB server for the very first time using this simple systemctl command :

systemctl start mariadb.service

lamp-centos-7-7

You may also see the status of MariaDB by typing:

systemctl status mariadb

MariaDB Initial Configuration

Step 8 – So its service is now running but there is one thing you should do immediately: configuring MariaDB setup for the very first time like setting up your mysql root password. Issue this command:

mysql_secure_installation

Then you’ll see a series of question, just answer it accordingly. The main important part is to define your root password while everything else is just up to you or you can simply hit the “ENTER” key through each prompt to accept the default values.

lamp-centos-7-8-mysql-setup

If you need to automatically run MariaDB everytime your server boot, simply issue this command:

systemctl enable mariadb.service

lamp-centos-7-8-2

You may also need to test your newly installed MariaDB by logging in as root:

mysql -u root -p

lamp-centos-7-8-3

As you can see from the screenshot above, it is Maria DB v5.5.37.

Step 9 – Also test if Apache and PHP is running well and able to process any *.php files. Create a php info page using this command followed by restarting apache

echo "<?php phpinfo(); ?>" > /var/www/html/info.php

lamp-centos-7-9

Restart apache:

systemctl restart httpd.service

Now open up your browser and access that newly created php page:

http://xxx.xxx.xxx.xxx/info.php

You’ll see a page similar to this one:

lamp-centos-7-9-php-info

Installing PhpMyAdmin

Step 10 – Now your server has Apace, PHP and MariaDB installed. It means it should be OK now to install PhpMyAdmin, a popular web-based database management system so you can easily manage your database without having to login via SSH and issuing several command lines. Unluckily, this piece of awesome software is not available in CentOS 7.0 default repositories. In this case you have to add / enable third-party repo like EPEL or RPMForge.

Method #1: RPMForge

First, download the rpm file.

wget http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el7.rf.x86_64.rpm

lamp-centos-7-10-rpmforge

then enable the repository and delete the .rpm file as it is not needed again.

rpm -ivh rpmforge-release-*
rm rpmforge-release-*

lamp-centos-7-10-rpmforge-enable

Method #2: EPEL

Download the .rpm file:

wget http://download.fedoraproject.org/pub/epel/beta/7/x86_64/epel-release-7-0.2.noarch.rpm

The url above is still its beta version. However if the repository is out of beta status, the link most likely will be different. In case that happens, you can find out its latest download url at Fedora Project website.

enable the repository and delete the .rpm file:

rpm -ivh epel-release*
rm epel-release*

Step 11 – Next, install it using yum again:

yum install phpmyadmin -y

screenshot:

lamp-centos-7-11

Step 12 – That’s it. Now you also have phpMyAdmin (PMA) installed but you should be better if you change its default configuration before using it. First, you’ll need to backup default PMA’s config file:

cp /etc/httpd/conf.d/phpMyAdmin.conf /etc/httpd/conf.d/phpMyAdmin.conf.old

then edit file phpMyAdmin.conf file using your favorite editor. In this example I use Nano editor:

nano /etc/httpd/conf.d/phpMyAdmin.conf

Step 13 – You’ll now see the content of phpMyAdmin.conf, next you have to allow connections from remote hosts by editing few lines inside section <Directory “/usr/share/phpMyAdmin”>.

Before changes:

lamp-centos-7-13

After:

lamp-centos-7-13-4

Also you’ll need to edit few lines next:

Before:

lamp-centos-7-13-2

After:

lamp-centos-7-13-3

Shortly it should look like this:

<Directory /usr/share/phpMyAdmin/>
   <IfModule mod_authz_core.c>
     # Apache 2.4
     <RequireAny>
       Require all granted
     </RequireAny>
   </IfModule>
   <IfModule !mod_authz_core.c>
     # Apache 2.2
     #Order Deny,Allow
     #Deny from All
     AllowOverride None
     Options None
     Allow from All
     Require all granted
   </IfModule>
</Directory>

Once done, save and exit editor (In Nano it is Control+O then Control+X).

Step 14 – Restart Apache again:

systemctl restart httpd.service

Now you can test opening PMA on your browser via your server’s IP address:

http://xxx.xxx.xxx.xxx/phpmyadmin

and default login page of phpMyAdmin should be displayed:

phpmyadmin-login-page

That’s all. Now you can host your websites or blogs in that server, even WordPress.

Do not forget to follow me on twitter to get faster update or download my official Android app. Enjoy..

9 Comments

Add a Comment

Your email address will not be published. Required fields are marked *

Get more stuff like this
in your inbox

Subscribe and get interesting stuff plus faster updates to your email.