How to: Installing LAMP on CentOS 7,Fedora & SUSE

This guide will help you installing LAMP on your CentOS 7 system. I’ve searched around the net and I added some ‘bonus’ content which might

Introduction

A LAMP (Linux, Apache, MySQL, PHP) stack is a group of open source software that is installed for hosting websites or web applications. Whereas Linux is the ‘operating system’, Apache is the webserver, MySQL is your database and last but not least PHP is for processing your content.

Preparation

To start you will need the following:

  • A CentOS (7+), Fedora or openSUSE operating system
  • Having a text editor (nano or vi) installed
  • Network connection
  • 30 minutes of time

Getting your OS up-to-date

Before we start we want to make sure your system is up to date.Run the following command to update:

sudo yum -y update

If you don’t want to update or think this is not needed, you can skip this step.

Installing LAMP

Linux

You’ve probably already installed Linux. If you didn’t you can follow the links above to download an image and install the operating system. Most of the Linux distributions are free so I can only recommend to give them a try!

Apache

To install Apache run the following command:

sudo yum install httpd

Linux Install Httpd

After installing, use the following commands to start the httpd (Apache) service and make it automatically launch on start-up:

sudo systemctl start httpd

sudo systemctl enable httpd

(Optional) To save CPU usage, you might want to turn of the KeepAlive function in httpd.conf. Below is explained what the KeepAlive function does. If you don’t mind or don’t want to change the option, you can skip this part.


Quote by Abdussamad.com:

Advantages of KeepAlive

  • Improves website speed: It reduces latency associated with HTTP transfers and delivers a better user experience.
  • Reduces CPU usage: On the server side enabling KeepAlive reduces CPU usage. Consider that a typical web page has dozens of different files such as images, stylesheets, javascript files etc. If KeepAlive is disabled a separate connection must be made for each of those files. Creating and closing connections has an overhead and doing it for every single file wastes CPU time.

Disadvantages of Keepalive

  • Increases memory usage: Enabling KeepAlive  increases memory usage on the server. Apache processes have to keep connections open waiting for new requests from established connections. While they are waiting they are occupying RAM that could be used to service other clients. If you turn off KeepAlive fewer apache processes will remain active. This will lower memory usage and allow Apache to serve more users.

To edit the config file, use command:

sudo nano /etc/httpd/conf/httpd.conf

Optionally you can create virtual hosts, this means you can have ‘different sites’ on ‘different port numbers’. You can skip this step if you only want to create a single website or application. Otherwise use the following command to create and edit the virtual host configuration file:

sudo nano /etc/httpd/conf.d/vhost.conf

Add the following text to the file to create a host:

NameVirtualHost *:80

<VirtualHost *:80>
ServerAdmin webmaster@example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html/
ErrorLog /var/www/example.com/logs/error.log
CustomLog /var/www/example.com/logs/access.log combined
</VirtualHost>

Edit the file to your own desire. An example would be to change the folder in the www from /var/www/example.com/public_html/ to /var/www/yourwebsite.com/public_html/.

Make sure to create the directories stated in your configuration file by using the mkdir command:

sudo mkdir -p /var/www/example.com/public_html
sudo mkdir /var/www/example.com/logs

Please make sure you edit the directories in the command above. Otherwise your virtual host might not work.

MariaDB (MySQL)

Now we’re going to install your database server, I prefer to use MariaDB. To install MariaDB use the following command:

sudo yum install mariadb-server

Linux Install MariaDB1

To launch MariaDB now and on system start-up use the following commands:

sudo systemctl start mariadb.service

sudo systemctl enable mariadb.service

Because we want to harden the security, run the secure installation script. To do so run the following command:

mysql_secure_installation

If it asks you for a password, just press enter as there is no password set yet. It is STRONGLY to set a password for all of your MariaDB or MySQL server. Also remove any anonymous user accounts. Other options asked are optional, but please think twice when choosing your options.

(Optional) We’re going to create a database now, if you want to do this the easy way you can wait until we’ve installed PHP. After you’ve installed PHP you can optionally install phpMyAdmin which helps you to maintain your database server through a webinterface. To create a database using the terminal, log-in with your root account and use the password you’ve set while hardening the security:

mysql -u root -p

In this case ‘root‘ is the user, if you create an extra account in the future, replace ‘root’ with your new account name.

Now use the following command to create a database:

CREATE DATABASE IF NOT EXISTS TEST;

Linux Install MariaDB Database

Replace ‘TEST‘ with the database name you want to use, do not forget the ‘;‘ or your command will not work. The ‘IF NOT EXISTS‘ part checks if the database you want to create already exists or not, if it exists it will not create the database and an error will show up.

To leave the MariaDB instance, use:

quit

And press enter.

PHP

Now we’ve got Apache and a database server running, we want to install PHP. Run the following command to install PHP and the module to support your database server:

sudo yum install php php-mysql

Use the search funtion to find any modules and libraries you might find useful. This is optional but really useful:

yum search php-

(Optional) Installing phpMyAdmin

If you want to maintain your database(server) with ease, I can really recommend phpMyAdmin. This part of the tutorial is copied from the Xibo (Digital Signage / Narrowcast) tutorial I created a few days ago.

sudo yum install epel-release

sudo yum -y install phpmyadmin

phpMyAdmin is now installed on your webserver, let’s configure it so you can actually access it!

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

You’re now editing the configuration file of phpMyAdmin. Change the following code:

<IfModule mod_authz_core.c>
# Apache 2.4
<RequireAny>
Require ip 127.0.0.1
Require ip ::1
</RequireAny>
</IfModule>

To:

<IfModule mod_authz_core.c>
# Apache 2.4
<RequireAny>
Require all granted
Require ip 127.0.0.1
Require ip ::1
</RequireAny>
</IfModule>

And:

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

To:

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

Press CTRL-O to save your changes, then close by using CTRL-X.

After restarting your Operating System you can now access the phpMyAdmin environment by heading to http://<Your_Servers_IP_Address/phpmyadmin.


Now you’ve installed LAMP (and maybe some additional add-ons like phpMyAdmin or other PHP modules/libraries) you can start uploading your webserver / web-application files.