Install Linux Apache MySQL PHP (LAMP) On CentOS 7

I am writing this How To Install Linux Apache MySQL PHP (LAMP) On CentOS 7 post for my knowledge update about CentOS 7 and also for the internet users who reached here for an easy tutorial to install this. I assume that you have already Install CentOS 7 with minimal or GUI and now we are going to Install followings one by one;
  1. Apache Web Server (HTTP) Installation.
  2. MySQL Server (MariaDB) Installation.
  3. PHP Installation.

Install Linux Apache MySQL PHP (LAMP) On CentOS 7

As we know that LAMP is an abbreviated form of Linux, Apache, MySQL and PHP. We will start installation of Apache2, often called httpd on Linux box and here is the list of most professional ethics to go with installation of LAMP on CentOS VPS or standalone server as under;

Primary Note:

In this post, I am going to use the hostname server1.rizwanranjha.com with IP address 192.168.10.52. You are requested to please change your hostname and your IP address as per your IP scheme and Host Name policy of the company.
We need to add the latest EPEL repo here for better professional practices:
  1. rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY*
  2. yum -y install epel-release

Apache Web Server (HTTP) Installation

If your CentOS is not minimal, then you might have it already installed but we will assume that we need to Install it from the scratch. We will be using yum package manager to install Apache web server. For Apache web server installation on CentOS 7, we will simple execute this command to install Apache web server on CentOS 7.
sudo yum –y install httpd
You might notice that we are using sudo for Apache installation. This (sudo) will execute the above highlighted command with root privileges. It might ask you to type user's password for authenticity verification. Once, it give your a successful message for Apache installation. You need to start it on your CentOS VPS or CentOS dedicated server.
sudo systemctl start httpd.service
Afterword, you will need to check that either your Apache Server Installation let you to access it or not. Please use Facing IP (Public IP / Private IP) in your desired browser for confirmation that Apache Server is up and running after installation.

If Apache installation on CentOS 7 leads you to see above page in your browser, it mean you have Apache installation successful. Let's move to some behind the scene configurations which mostly professionals forget to add. Now for Apache installation, we need to take the last bite on this and have to start Apache on boot. We will be using the following command:
sudo systemctl enable httpd.service

MySQL Server (MariaDB) Installation

We have done successful installation of apache server and now, we need to install MariaDB, a MySQL drop-in replacement. MariaDB is a community driven development of MySQL, relational database management system (RDBMS). This is required, when we need to save our website data into a database.
We will be using yum package manager to install our software. With MySQL Server (MariaDB) Installation, we will be installing some of the “helper” packages which required for communication of some components between each other. Let’s run this command to Install MariaDB;
sudo yum -y install mariadb-server mariadb
Our MySQL database server is running perfectly now and we will make sure that some testing purpose access which is off course dangerous require to be remove which includes anonymous access, test database etc. We will use below mentioned command to create initial level database system security. Use this command to invoke already available script as;
sudo mysql_secure_installation
You have the option to simply Press Enter, which will be considered equal to Yes (Y).
[root@server1 ~]# mysql_secure_installation
/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none): <--ENTER
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorization.
Set root password? [Y/n]
New password: <-- YourOwn&RememberablePassword
Re-enter new password: <--YourOwn&RememberablePassword
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] <--ENTER
... Success!
Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] <--ENTER
... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] <--ENTER
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] <--ENTER
... Success!
Cleaning up...
All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
[root@server1 ~]#
Secure installation will remove test database, sample users and also will disable remotely root logins. As you notice that this script also reload all the MySQL privileges to accept the changes immediately. Now, we need to enable MySQL (MariaDB) to start on boot, same like Apache. Just run the below command for this;
sudo systemctl enable mariadb.service

PHP Installation

Now, we need PHP installation on our CentOS 7 VPS or dedicated server. We will also install some packages like php-mysql for php and MySQL connection or communication with each other.
sudo yum install php php-mysql
After successful PHP installation on CentOS 7, we need to restart our Apache web server to start using PHP with it. We will be using below command to restart apache server on CentOS 7 VPS or dedicated Server.
sudo systemctl restart httpd.service

PHP Installation - Testing

I am adding these lines for your testing and to make sure that you have PHP installation is successful and working fine with your Apache Server. We need to create a small info.php file in our /var/www/html/ directory, which is default document root on CentOS. Let’s do this as under and run this command;
vi /var/www/html/info.php
Above mentioned command will create a file name info.php in the /var/www/html/ directory on your CentOS 7. Now, simply paste below code to test PHP installation.
<?php phpinfo(); ?>
Now, you can use http://192.168.10.52/info.php to test PHP installation.
 

Install PHP Modules

We have already installed php-mysql package during PHP installation. However, you might need to install some other packages for Wordpress, Drupal, Joomla type CMS application. You can Install PHP Module with this command,
sudo yum -y install mod_php
and you can also install multiple PHP modules on your CentOS VPS or dedicated server as per below example;
yum -y install php-odbc php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap
If you need to know about the specific package / module. You can use yum info package_name
command, for example yum info php-xml and after all these installatin, do not forget to restart your Apache Server.
 systemctl restart httpd.service
We have install Apache MySQL PHP on CentOS7 and its working fine, please show up on comments or do share us with your friends to encourge for more posts.

Comments