LAMP stack on your CentOS 7

--

This article is provided as a courtesy and is intended as a general guide. Installing and configuring the LAMP stack on your CentOS 7 server from scratch.

Prerequisites— Connect through SSH (putty)

As a first step, you should configure your operating system CentOS 7, so that it is optimally prepared for the subsequent installation of LAMP Stack. This implies: 1. Update your server. 2. Connect with your SSH client for a more comfortable interface.

Login as root user:

su root

1. Get your operating system up to date. This is important:

yum update -y

2. Install net-tools. Will allow you to know your IP, and you will be able to connect with your SSH client (putty):

yum install net-tools

3. Open the settings of your VM, head over Network section. There you will see your configuration and it should look like this.

Once done, run:

network restartip a

Your IP should look like this:

4. Open putty and submit your server’s IP.

You should have no issues connecting to the server:

If you are interested in setting up a SAMBA client in your centOS, for a realtime file sharing on Windows, head over my other tutorial:

Install the LAMP stack

In order to install the LAMP stack, some packages are needed as dependencies. Some others are useful tools for your CentOS, as the dnf package manager, nano for editing documents, packages needed for mounting the shared folder and so on:

yum install nano dnf zip unzip wget yum-utils gcc glibc-headers glibc-devel kernel-headers kernel-devel patch libgomp libgcc dkms bzip2 perl make binutils rsync

Step 1. Install MariaDB & Apache

For installing mariaDB, we need to create a repo by the following way:

nano /etc/yum.repos.d/MariaDB.repo

You need to paste the following content in the file (CRTL+O = save):

[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.2/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

Now you can install the packages:

dnf install MariaDB-server MariaDB-client httpd -y

Verification

Check the status of the installation by running:

systemctl start mariadb httpdsystemctl status mariadb httpd

Run MySQL secure installation

This will let you set a root password for your MySQL user.

mysql_secure_installation

Check the versions of your installed MariaDB and Apache

httpd -vmysql -u root -p

Configurations

There are some configurations that need to be applied in order to the MariaDB and Apache services work properly.

1. Firewall:

To be able to access the web server from outside, we have to open the HTTP (80) and HTTPS (443) ports in the firewall.

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

2. SELinux:

Configure SELinux to Allow HTTP. SELinux default setting is to restrict Apache’s access to directories until explicit permissions are granted.

nano /etc/sysconfig/selinux

3. Apache Module_rewrite

Edit the following file with nano:

nano /etc/httpd/conf/httpd.conf

Find the line Include conf.modules.d/*.conf and add the following line just below:

LoadModule rewrite_module modules/mod_rewrite.so

Find the public html folder and change the value of AllowOverride None to → AllowOverride All.

Run the following command to check that rewrite_module is enabled.

apachectl -M

4. Verification: Now you can test that apache is working properly by entering your server’s IP in the browser.

Step 2. Install PHP

For installing PHP, I will be using the UIS repositories.

cd /var/tmprpm -Uvh https://centos7.iuscommunity.org/ius-release.rpm

Search for your desired PHP version.

dnf search php72u

Once you find it, install. Also add the packages that you will need.

dnf install php72u php72u-devel php72u-mysqlnd mod_php72u php72u-fpm -y

Check the installed version:

php -v

Test the PHP installation

For testing the PHP installation, create a new file in the html folder, named info.php in which contains all the php information.

nano /var/www/html/info.php

Paste the following content, just as you did with mariaDB repo:

<?php
phpinfo();

Open in your browser: domain/info.php

Recommended PHP options

Some PHP options are “recommended” for running several web applications as Magento and others. We change directly the php.ini file by “sed” command:

sed -i "s/memory_limit = .*/memory_limit = 1024M/" /etc/php.ini
sed -i "s/upload_max_filesize = .*/upload_max_filesize = 400M/" /etc/php.ini
sed -i "s/post_max_size = .*/post_max_size = 800M/" /etc/php.ini
sed -i "s/max_execution_time = .*/max_execution_time = 1800/" /etc/php.ini
sed -i "s/max_input_time = .*/max_input_time = 1800/" /etc/php.ini
sed -i "s/zlib.output_compression = .*/zlib.output_compression = on/" /etc/php.ini
sed -i "s/;date.timezone.*/date.timezone = UTC/" /etc/php.ini
sed -i "s/;opcache.save_comments.*/opcache.save_comments = 1/" /etc/php.ini

Check for installed PHP extensions

php -m

Step 3. phpMyAdmin

phpMyAdmin will let you create and modify though a visual interface, all your databases. In order to install it, run:

dnf install phpMyAdmin -y

Enable external connections

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

Add the line “Require all granted” in the marked position:

Restart the MariaDB and apace services, in order to the changes take effect:

systemctl restart httpd

Verify the installation:

Verify the installation by accessing your phpMyAdmin menu through the browser: domain/phpmyadmin

Now you can create databases and are accessible from anywhere.

Database user

This responds to good practices, to have user different from root for working with the database.

Login phpMyAdmin and head over “users”, “add new user”.

extra: Composer

Composer is a dependency manager in projects, for programming in PHP. That means that it allows us to manage (declare, download and keep updated) the software packages on which our PHP project is based.

curl -sS https://getcomposer.org/installer | php
echo $PATH
mv composer.phar /usr/local/bin/composer
chmod +x /usr/local/bin/composer

Check the composer version

composer -V

In case you got bash: curl: command not found, do the following→

sudo yum install curl
ln -s /opt/lampp/bin/php /usr/local/bin/php

Don’t miss my other tutorial:

--

--