Apache CentOS Linux MySQL PHP

LAMP STACK

What is a LAMP Stack?

LAMP stack is a popular open source web platform commonly used to run dynamic web sites and servers. It includes Linux, Apache, MySQL, and PHP/Python/Perl and is considered by many the platform of choice for development and deployment of high performance web applications which require a solid and reliable foundation.”

The reason they call it a STACK is because each level derives off it’s base layer (OS). This tutorial will assist you in setting up a base LAMP stack (Linux, Apache, MySQL, PHP) on your Centos 6/7 system.

This article will outline the following:

  • Apache installation including the configuration of a Docroot and VirtualHosts
  • MySQL installation
  • PHP installation (Covering multiple versions) with additional libraries and modules
  • Lastly, testing all of the above

Let us begin. First, I recommend updating yum.

# yum update

APACHE:

The following will install the Apache web server, as well as the additional httpd-devel package which will allow us to add additional modules if need be in the future.

# yum install httpd httpd-devel

note: The main configuration file will be located at /etc/httpd/httpd.conf

Next, let us proceed with configuring a Document root for our website to serve content as well as a Virtual Host so we can host multiple websites via our new Apache install.

Document Root:

For this example we will be using the domain www.example.com and the default /var/www/html location.

# mkdir -p /var/www/html/www.example.com

Make sure to set the proper ownership on our directory. The permissions will be set to the apache user.

# chown -R apache:apache /var/www/html/www.example.com
# chmod -R 755 /var/www/html

From here we can add a test page into our doc root as well as set permissions:

# echo "test" > /var/www/html/www.example.com/index.html
# chown apache:apache /var/www/html/www.example.com/index.html

Virtual Hosts:

We will now create the following directories, one, to store our Virtual Host configuration files, and second, the directory our sites actually being served will be stored.

# mkdir /etc/httpd/sites-available
# mkdir /etc/httpd/sites-enabled

Let us append the following to our main Apache configuration file. This will tell Apache to look for any *.conf files in the sites-enabled directory and serve the Virtual Hosts found.

note: When creating additional Virtual Hosts, ensure the configuration file ends in .conf otherwise, Apache will ignore it.

# echo "Include sites-enabled/*.conf" >> /etc/httpd/conf/httpd.conf

Now we can create and configure our first virtual host for the domain www.example.com:

# nano /etc/httpd/sites-available/example.com.conf

We can use the following for a minimum configuration:

<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/html/www.example.com
CustomLog /var/www/html/www.example.com/example.com-access.log combined
ErrorLog /var/www/html/www.example.com/example.com-error.log
</VirtualHost>

Note: There are a variety of options that can be added to our Virtual Host config. The above is enough to serve our website, and include logging for both our access logs and error logs.

Save and Exit. CTRL+o

We must create a symlink from our sites available folder to our sites-enabled folder so Apache knows what to serve.

# ln -s /etc/httpd/sites-available/example.com.conf /etc/httpd/sites-enabled/example.com.conf

Before we restart apache, let us create the log files so Apache does not error upon startup and fail to load.

# touch /var/www/html/www.example.com/example.com-access.log && chown apache:apache /var/www/html/www.example.com/example.com-access.log
# touch /var/www/html/www.example.com/example.com-error.log && chown apache:apache /var/www/html/www.example.com/example.com-error.log

Now we can restart apache,

# service httpd restart

Starting httpd: [ OK ]

Let us ensure apache is running :

# service httpd status

httpd (pid 7713) is running…

Lastly, let us open port 80 on our firewall, iptables to allow external connections to Apache.

# iptables -I INPUT -p tcp --dport 80 -j ACCEPT && iptables-save

Testing:

To test, simply goto your site in a browser. If the site is not live, configuring your local hosts file using the domain and server IP will allow you to access the newly created site that was configured.

 

MySQL:

We will now install MySQL, as well as secure our installation for production use.

# yum install mysql mysql-server mysql-devel

Once completed, let’s start the MySQL service using the following:

# service mysqld start

mysqld (pid 25356) is running…

note: The default location for databases and logs can be found /var/lib/mysql

Since this is a new installation, Let us proceed with securing our install.

# /usr/bin/mysql_secure_installation

You will be presented with the following questions. It is recommended to say ‘Yes’ to them all.

Set root password? [Y/n] y

Remove anonymous users? [Y/n]y

Disallow root login remotely? [Y/n]y

Remove test database and access to it? [Y/n] y

Reload privilege tables now? [Y/n] y

All done! If you’ve completed all of the above steps, your MySQL installation should now be secure.

Let’s ensure MySQL is running:

# service mysqld status

mysqld (pid 25356) is running...

note: Depending on usage, don’t forget to allow connections on your firewall for port 3306 (MySQL default port)

PHP:

PHP is a fairly straight forward install. This will install PHP 5.3 from the base Centos repository. (If you would like php 5.5+ please see below )

Install PHP with the necessary libraries and modules:

# yum install php php-mysql php-common php-gd php-mbstring php-mcrypt php-devel php-xml

Thats it! Let’s restart Apache for the changes to take effect. The PHP configuration file is located in /etc/php.ini

# service httpd restart

We can now run the following to verify PHP is loaded correctly.

# php -v

PHP 5.3.3 (cli) (built: Feb 9 2016 10:36:17)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies

PHP 5.5/5.6

We will need to enable 3rd party repos. We will be using both the EPEL repository and the REMI repository:
EPEL:

# yum -y install epel-release

Ensure repo is enabled:

# nano /etc/yum.repos.d/epel.repo

Under [epel] Locate enabled=0
Set to: enabled=1

save using CTRL+o

REMI:

For CentOS 7

# wget http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
# rpm -Uvh remi-release-7.rpm

For CentOS 6

# wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
# rpm -Uvh remi-release-6.rpm

Enable repository:

# nano /etc/yum.repos.d/remi.repo

under [remi] Locate enabled=0
Set to: enabled=1

save using CTRL+o

# nano /etc/yum.repos.d/remi-safe.repo

[remi-safe] Locate enabled=0
Set to: enabled=1

save using CTRL+o

INSTALLATION:
To install PHP 5.5 or 5.6 we must enable it within the remi repo.

# nano /etc/yum.repos.d/remi.repo

For php5.5

Under [remi-php55] Locate enabled=0
Set to: enabled=1

save using CTRL+o

For php 5.6

Under [remi-php56] Locate enabled=0
Set to: enabled=1

save using CTRL+o

Now we can proceed with the following to install:

# yum install php php-mysql php-common php-gd php-mbstring php-mcrypt php-devel php-xml

Restart Apache for the changes to take effect.

# service httpd restart

Verify PHP 5.5/6 is installed

# php -v

PHP 5.5.34 (cli) (built: Mar 29 2016 19:27:57)

Copyright (c) 1997-2015 The PHP Group

Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies

TESTING:

We can also test our PHP install.

In our newly created Docroot, let us create a test php file.

# cd /var/www/html/www/www.example.com
# nano info.php

paste the following:

<?php
phpinfo()
?>

CTRL+o to save

Now browse to www.example.com/info.php if all went well, you will be presented with the PHP info.

You have now successfully created a working LAMP stack.

Related Articles

  • Installing phpMyAdmin and connecting to remote database server

    phpMyAdmin is one of the many tools that allows administrators the ability to effectively and efficiently work with their databases. This can can either be a local database or on...
  • Simple Stateful Load Balancer with iptables and NAT

    NOTE: To demonstrate how iptables can perform network address translation this how-to shows how to use it to implement a over-simplified load balancer. In practice we would use a daemon...
  • Reset MySQL Password

    Resetting a MySQL Root Password is one of the easiest things to do. However, finding documentation on how to do so can be challenging.   We as system administrators, have...
  • Disk Benchmarking Tools.

    One of the main bottlenecks in server slowness is the Disk IO. If the speed of your disks are slow, then the CPUs of your servers may be wasting their CPU...