LAMP- Installation and Configuration in CentOS 7

LAMP- Installation and Configuration in CentOS 7

Hello! Today, let’s install and configure a web server (Linux + Apache + MySQL + PHP = LAMP) in CentOS7 platform. I think this subject can be very helpful to web developers, students and system administrators. Without any adieu, let’s get started with the installation and configuration.




Installation of CentOS 7

I’ve already written blogs on installation and initial configuration of CentOS 7 operating system in my previous posts. You can refer to these blog posts:

Creating VM in VMware

Installation of CentOS

Initial Configuration in CentOS

Installation of LAMP

Once the CentOS server is ready, we can proceed to install the required packages for LAMP. I personally prefer to use EPEL repository for getting software packages.


[root@lamp ~]# yum update
[root@lamp ~]# yum install epel-release
[root@lamp ~]# yum install httpd mysql mariadb mariadb-server php php-mysql phpmyadmin

After the installation of above packages is completed, we can begin the configuration of these components. First, let’s┬ástart these services and enable them to start at startup.

[root@lamp ~]# systemctl start httpd
[root@lamp ~]# systemctl start mariadb
[root@lamp ~]# systemctl enable httpd.service
[root@lamp ~]# systemctl enable mariadb.service

Then, let’s configure the MySQL server. To do so, we can simply run the command mysql_secure_installation, which will launch a simple wizard asking us to set root password and other security parameters. After preparing MySQL server, we can go ahead and create a database and a non-root user to access our database from web application.

[root@lamp ~]# mysql -u root -p
MariaDB [(none)]> CREATE DATABASE 'test-db';
MariaDB [(none)]> CREATE USER 'sajjan'@'localhost' IDENTIFIED BY 'sajjan@123!';
MariaDB [(none)]> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER ON `test-db`.* TO 'sajjan'@'localhost'
MariaDB [(none)]> SHOW GRANTS FOR 'sajjan'@'localhost';

We can then setup PHPMyAdmin to easily manage our databases with its graphical interface. If you’ve noticed above, we’ve already installed phpmyadmin package, so don’t need to install it again. Now, we need to make a little change to its config file so that it can be accessed from other hosts in the network. We just need to add our IP address or network with ‘Require ip’ and ‘Allow from’ statements like below:

[root@lamp ~]# vi /etc/httpd/conf.d/phpMyAdmin.conf
Allow from 192.168.1.0/24
Require ip 192.168.1.0/24

Then, we can restart the Apache server to implement the changes. Up to now, even though our services are configured and running properly, we cannot yet access them from network. I let you guess the reason for this… Well, if you guessed it because of the CentOS’s Iptables or firewalld, give yourself a pat on the back for being correct. So, let’s configure our firewall to allow HTTP service.

[root@lamp ~]# firewall-cmd --permanent --zone=public --add-service=http
[root@lamp ~]# firewall-cmd --reload

Now, if we access this server’s ip address followed by /phpmyadmin, the corresponding page should be opened in our web browser. This is the basic setup of LAMP. We can then proceed with our web development. By default, Apache’s root directory is set to /var/www/html. So, we need to put our web projects inside this directory. If we need to place our projects somewhere else, we should change the root directory parameter accordingly in httpd.conf file.

SAMBA Share For Easy Development

If you’re wondering why we need Samba share for this purpose…, well, the reason is ┬ásimple. It’s because developing an entire web application in command line mode isn’t that easy and convenient. So, with Samba, we create a share folder in our CentOS server, which is then accessed and managed from our chosen OS and application. Doing this, makes web development process a whole lot easier and handier, mostly for Windows users. For Linux users, you can also use NFS.

Let’s now install and configure the Samba server.

[root@lamp ~]# yum install samba samba-client samba-common
[root@lamp ~]# mv /etc/samba/smb.conf /etc/samba/smb.conf.backup
[root@lamp ~]# vi /etc/samba/smb.conf
[global]
workgroup = WORKGROUP
server string = Samba Server %v
netbios name = lamp
security = user
map to guest = bad user
dns proxy = no
# ===================Share Definitions ============================
[Test]
path = /var/www/html/test
valid users = @smbgrp
browsable = yes
writable = yes
guest ok = yes
read only = no

What I’ve done here is create a share called Test and map it to a directory called /var/www/html/test. I’ll create this directory later and put my web project inside it. I’ve also configured this share to be accessed by users belonging to group called smbgrp only. I’ll create this group and its members below. Once done, we can access this shared folder from our Windows PC using the user credentials that we’ll create below.

[root@lamp ~]# systemctl start smb.service
[root@lamp ~]# systemctl start nmb.service
[root@lamp ~]# systemctl enable smb.service
[root@lamp ~]# systemctl enable nmb.service

[root@lamp ~]# mkdir /var/www/html/test
[root@lamp ~]# groupadd smbgrp
[root@lamp ~]# useradd sajjan -G smbgrp
[root@lamp ~]# smbpasswd -a sajjan

This completes our configuration of Samba server, but we may still face some issues while accessing the shared folder from our PC. The reason again being firewall, along with permission issues and SElinux.

[root@lamp ~]# firewall-cmd --permanent --zone=public --add-service=samba
[root@lamp ~]# firewall-cmd --reload

[root@lamp ~]# sestatus
[root@lamp ~]# chcon -t samba_share_t /var/www/html/test   # you may also disable SElinux if you prefer

[root@lamp ~]# chmod -R 0777 /var/www/html/test
[root@lamp ~]# chown -R sajjan:smbgrp /var/www/html/test

And here’s the video tutorial on it:

This completes this blog post. I hope you’ve learned something useful from it. I hope to hear from you in the Comments Section below. Keep learning! Thank you!