Complete: HTTPD+ PHP +MYSQL tutorial CENT OS 7

This how to will focus on the steps required to hosting multiple full domains or hosts on a web-server. This could be a virtual domain implementation for your corporate or home network. Maybe you’ve wanted to run web-packages and apply extra security on the applications and its sub-directories

Objectives:

  • Define packages required
  • Explain the steps of installation
  • Explain mode of security aspects.

Overview:

Apache: an open source implementation distributed freely under the GPL that will serve our web-pages

PHP: An opensource ,powerful, yet easy to use scripting language tool, that is extremely easy to setup

MySQL: An open source relational Database management system based on SQL (Structured Query Language) available on subscription under GPL. It provides rich Application programming interface (API) for many languages including C++, Java,

we will start by installing Apache and PHP with appropriate PHP, MySQL dependencies.

probably the first step if you have not already created a new directory, is to do it now;

sudo mkdir -p /var/www/serv.com/public_html

next is to grant permissions to the folder created; the -p flag suggests that the hierarchy of the parent directory is created automatically by this command

sudo chown -R apache:apache /var/www/serv/public_html

USE your favorite yum utility to  setup a virtual host, with username serv1. As our example access the file /etc/hosts using an editor maybe vi and add the following line:

172.32.0.1 serv1.com 

to make your website functional at this point, find at least the following data and insert your domain configuration:  enter into the apache configuration file itself.

sudo vi /etc/httpd/conf/httpd.conf
<VirtualHost *:80>
 ServerAdmin webmaster@example.com
 DocumentRoot /var/www/serv.com/public_html
 ServerName www.serv.com
 ServerAlias serv.com
 ErrorLog /var/www/serv.com/error.log
 CustomLog /var/www/serv.com/requests.log
</VirtualHost>

If you need encryption for your domain (s) or if you have enabled ssl then you’ll have to check how ssl is running on your system

you can list virtual hosts available  with the command

# httpd -t -D DUMP_VHOSTS

reload configuration of your web server:

systemctl restart httpd

If you are running a firewall, add the following commands permanently to allow HTTP and HTTPS traffic. This will make apache work seamlessly work with PHP and its components:

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

You can repeat these steps if you need to add other websites, though this may depend on where you’ve stored the website files. If it’s outside the web tree  or /usr/local, or …/htdocs, the owners of the folders should have read/write permissions. Anonymous apache users like wheel, www-data, nobody, (depending on the web-server release) should not be allowed to own the process for security reasons well documented.  it’s also strongly advised, that root should not own the webserver process. If it is in a production environment, it in-turn gives room for the webserver process to be accessible by its users.

Its a good idea now to create new users and give permissions for example: create two users to maintain the virtual domain and further give them read/write access to the folders.

Create two users under a group called SERV

First create group:

 groupadd SERV

Then create users that will maintain the website if any

usermod -a -G serv charles

usermod -a -G serv bob

chgrp -R SERV serv.com

chown -R charles serv.com

chown -R bob serv.com

chmod -R 755 serv.com

chmod g+s serv.com

Install PHP and MySQL

Use the yum command to install the MySQL libraries required to provide for separate environments and languages

$ yum install mysql mysql-server mysql-libs mysql-server

You may need to edit  the /etc/my.cnf

To start out the MySQL server use service:

$ service mysqld start

use chkconfig:

$ chkconfig --levels 235 mysqld on

Set the root user password for all local/virtual domains

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('new-password');

SET PASSWORD FOR 'root'@'serv1.com' = PASSWORD('new-password');

SET PASSWORD FOR 'root'@'127.0.0.1' = PASSWORD('new-password');

Use yum system to install our PHP and its components. We’re going to include the php-mysql package as well:

sudo yum install php php-mysql 

restart the Apache web server in order for PHP to work without problems. You can do this by typing this:

sudo systemctl restart httpd.service

Install PHP Modules

To get advanced functionality of PHP, optionally we can install some additional modules. but that is beyond the discussion for this how to;

yum search php-*

The results are all optional components that you can install.

sudo yum install php-fpm

Testing PHP

open a blank file and put this valid php code.
sudo vi /var/www/html/info.php

<?php phpinfo(); ?>

Once you are done , via your browser’s URI type the below command
http://
your_server_IP_address/info.php

The page should look something like this:

Now you are ready to unpack your website into your virtual domain and apache is ready to serve its pages.

Advertisements