24.1 C
Texas
Mel K
LInux Guru and Technical Writer

Install LAMP in CentOS 7 and RHEL7

Let’s keep it easy. Linux is one of the best OS for building a server. To build a small Linux web server, you need the following packages – any Linux OS (here, it’s CentOS 7), Apache, MySQL, and PHP. This is why its called LAMP. Sometimes, people add phpMyAdmin to facilitate the MySQL tasks for beginners.

Let me explain step by step. Starting from my test machine, my VM is Centos 7.4 with IP= 192.168.1.100

Install Apache Webserver

CentOS 7 ships with Apache 2.4. It’s a basic package available on CentOS 7. Let’s install it the withfollowingollow command:

- Advertisement -
yum -y install httpd

You see  something like this:

Now, configure your apache to start at boot time

systemctl start httpd.service
systemctl enable httpd.service
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

If your firewall is activated, please add the following rules to make your servers to be accessible remotely.

firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload

Check if server listening on port 80

[root@osradar-test ~]# netstat -an | grep 80
tcp6 0 0 :::80 :::* LISTEN

Ow!!! Server only listening on TCP6 . It’s not available via IP address. Run this command:

edit /etc/httpd/conf/httpd.conf

Replease

Listen 80

with

Listen 192.168.1.100:80  ##  please put your own IP address

Restart Apache and check netstat

[root@osradar-test conf]# systemctl restart httpd.service
[root@osradar-test conf]# netstat -an | grep 80
tcp 0 0 192.168.1.100:80 0.0.0.0:* LISTEN

Everything looks fine. Now, visit your welcome page at http://192.168.1.100.  and see  the bellow result.

Install MySQL

CentOS is  using  MariaDB instead of MySQL package . MariaDB is open source and fully compatible with MySQL.

Install mariadb-server and client with the following command:

[root@osradar-test conf]# yum install mariadb mariadb-server -y 

Installed:
 mariadb.x86_64 1:5.5.56-2.el7 mariadb-server.x86_64 1:5.5.56-2.el7

Dependency Installed:
 perl-Compress-Raw-Bzip2.x86_64 0:2.061-3.el7 perl-Compress-Raw-Zlib.x86_64 1:2.061-4.el7 
 perl-DBD-MySQL.x86_64 0:4.023-5.el7 perl-DBI.x86_64 0:1.627-4.el7 
 perl-Data-Dumper.x86_64 0:2.145-3.el7 perl-IO-Compress.noarch 0:2.061-2.el7 
 perl-Net-Daemon.noarch 0:0.48-5.el7 perl-PlRPC.noarch 0:0.2020-14.el7

Enable MySQL service to start at boot and start it now.

systemctl start mariadb.service
systemctl enable mariadb.service

Now, set passwords for the MySQL root :

[root@osradar-test conf]# mysql_secure_installation

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): <=== just  Enter
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y  <== set new password
New password: 
Re-enter new password: 
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] y <== remove anonymous user
 ... 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] n <== allow or not to login with root remotly
 ... skipping.

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] n <== delete  test database
 ... skipping.

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y <== Reload privileges
 ... 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@osradar-test conf]#

Test you access and see the created databases

[root@osradar-test conf]# mysql -u root -p
Enter password:     <<== insert your mysql created password in the step before 
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 7
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]>

If you followed everything as said, all things should be fine.

Install PHP

Please note that PHP 7 is not yet supported  in CentOS 7. The only supported PHP version is 5.x.

To install the official PHP package just run

yum install php  -y

If you choose to install PHP 7.2, you have to  activate Remi and EPEL repository.

yum install yum-utils
yum install epel-release
rpm -Uvh http://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum-config-manager --enable remi-php72

Now, let’s install PHP.

yum --enablerepo=remi,remi-php72 install httpd php php-common
-------
Installed:
 php.x86_64 0:7.2.1-1.el7.remi php-common.x86_64 0:7.2.1-1.el7.remi Dependency Installed: php-cli.x86_64 0:7.2.1-1.el7.remi php-json.x86_64 0:7.2.1-1.el7.remi Complete!

Test your PHP page and settings

vi /var/www/html/info.php

Add these lines into the file.

<?php
phpinfo();
?>

Now, save and exit.

Restart apache and  check your PHP file on http://192.168.1.100/info.php

systemctl restart  httpd

To Install additional PHP 7.2 modules, run these commands.

yum --enablerepo=remi,remi-php72
 install -y php-pecl-apcu php-cli php-pear php-pdo php-mysqlnd php-pgsql php-pecl-mongodb php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml

Install phpMyAdmin

This chapter is optional. It’s just to help you to manager your databases via a web interface. This tool is mostly used by beginners and professionals as well.

Installing phpMyAdmin

yum install phpmyadmin

phpMyAdmin is not normally accessible remotely from another PC or servers. That’s why you have to make some changes to make this possible.

Let’s edit phpMyAdmin.conf file using command line:

vi  /etc/httpd/conf.d/phpMyAdmin.conf

and adjust the bellow settings

<Directory /usr/share/phpMyAdmin/>
 AddDefaultCharset UTF-8

<IfModule mod_authz_core.c>
 # Apache 2.4
 Require all granted
 </IfModule>
 <IfModule !mod_authz_core.c>
 # Apache 2.2
 Order Deny,Allow
 Allow from All
 Allow from 127.0.0.1
 Allow from ::1
 </IfModule>
</Directory>

Now, restart apache to load the new settings.

systemctl restart  httpd

Now, check the changes :  http://IP/phpmyadmin  ( http://192.168.1.100/phpmyadmin/)

You can login with root access already. You can easily defined and create databases and users :

Please feel free to ask questions or comment on this article!!!

- Advertisement -
Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

LEAVE A REPLY

Please enter your comment!
Please enter your name here



Latest article