May 22, 2015

Your First LAMP Server on CentOS/RedHat 7

^ Ad space to help offset hosting costs :D

Get up and running with a LAMP server for your PHP applications.

  • Use yum search and yum install commands to find and install packages
  • Install Apache and MariaDB (MySQL)
  • Install PHP and all the modules you'll need to get startedWe use a CentOS 7 server for this demonstration.

Setup

First, check if SELinux is enabled and enforcing. It is, we'll get around any issues there if we run into them.

sudo sestatus

Next we'll search for the available Apache and PHP packages:

yum search httpd
yum search php
yum search mysql # get mariadb

Apache

Then we install Apache:

sudo yum install httpd
sudo systemctl enable httpd # ensure starts on boot!
sudo systemctl start httpd  # Start the service
sudo systemctl status httpd

We can test this is working using curl: curl localhost or by viewing the server's IP address in the browser.

Apache configuration is found in /etc/httpd. Note that we can add extra conf files, such as virtual hosts, into the /etc/httpd/conf.d directory. One such virtual host might be something like this, which assumes an IP address of "104.236.197.115", a domain being used (using the xip.io service) of http://104.236.197.115.xip.io, and a web root of /var/www/my_app/public.

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName my-site.com
    ServerAlias 104.236.197.115.xip.io

    DocumentRoot /var/www/my_app/public
    <Directory /var/www/my_app/public>
        # Don't show directory index
        Options -Indexes +FollowSymLinks +MultiViews

        # Allow .htaccess files
        AllowOverride All

        # Allow web access to this directory
        Require all granted
    </Directory>

    # Error and access logs
    ErrorLog ${APACHE_LOG_DIR}/my-site.error.log
    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/my-site.access.log combined
</VirtualHost>

MariaDB:

sudo yum install mariadb-server
sudo systemctl enable mariadb # ensure starts on boot!
sudo systemctl start mariadb
sudo systemctl status mariadb

# Secure mysql installation
sudo mysql_secure_installation

You can log into MySQL and do the usual SQL commands.

PHP:

Add the Fedora EPEL and webtactic repositories to get the latest PHP (5.6).

sudo rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
sudo rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

# New packages and PHP versions!
yum search php

# Install php (just a few basic ones)
sudo yum install php56w php56w-mysql php56w-mcrypt php56w-gd php56w-curl

PHP configuration is found in /etc/php.ini.

Create /var/www/index.html to see that this is the default web root location, Apache will automatically pick up any new files there.

Then we can make a info.php file at that location: sudo vi /var/www/info.php:

<?php
phpinfo();
?>

Check that in browser, we see it works!

Let's test all of this!

Log into mysql using mysql -u root -p and run the following to create a database and user:

create database some_db;
create user 'my_app_user'@'%' identified by 'my_password';
grant all privileges on some_db.* to 'my_app_user'@'%';
flush privileges;

Edit and tests connection with that user:

mysql -u my_app_user -p
> show databases; # see database "some_db"

Let's next create a php file at /var/www/html/test.php:

<?php
$dsn = 'mysql:dbname=some_db;host=localhost';
$user = 'my_app_user';
$password = 'my_password';

try {
    $dbh = new PDO($dsn, $user, $password);
} catch (\PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

echo "Connected!";
?>

Running that test.php file in CLI and browser, we'll see that it works. We also can use 127.0.0.1 instead of localhost in the connection string to connect - that won't change or interfere with any stock rules SELinux has in place.

All Topics