Install and configure Apache, PHP with suPHP and Virtual Hosts for users on CentOS 6

In this article I will cover the installation and configuration of:

1) Apache
3) PHP and basic packages
4) suPHP

Also, I’ll explain how to:

1) Configure Apache to run Virtual Hosts for chosen existing users
2) Configure PHP to run as the Virtual Host’s user (suPHP)

All the configuration files mentioned in this topic can be browsed here.

Let’s get started!

Open the shell of your CentOS server and login as root.

Apache

1) Installing Apache web server (httpd)

yum install httpd -y

The -y parameter skips the yes/no question of the yum install.

2) Start the httpd service (the web server):

service httpd start

3) Test if the HTTP server is running (choose one method)

#method1: curl https://localhost/ 2>/dev/null
#method2: wget -qO- https://localhost/
#method3: echo -e "GET / HTTP/1.0\r\n\r\n" | nc localhost 80

Apache Configuration

I’ve used a default configuration located in /etc/httpd/conf/httpd.conf

You can view or download the entire httpd.conf here: 
https://invision-web.net/web/sources/etc/httpd/conf/httpd.conf

PHP and Packages

4) Here are some basic PHP packages you might want to install:

yum install php-common php-gd php-mcrypt php-pear -y
yum install php-pecl-memcache php-mysql php-xml php -y
yum install php-mcrypt php-mbstring php-devel gcc pcre-devel -y

5) Next, install the mod_suphp to be able to run PHP on a per user basis

rpm -Uvh https://invision-web.net/web/sources/rpmforge-release-0.5.2-2.el6.rf.i686.rpm
yum install mod_suphp

6) Edit the /etc/suphp.conf, be sure the bolded lines are exact:

[global]
logfile=/var/log/httpd/suphp_log
loglevel=info
webserver_user=apache
docroot=/
env_path=/bin:/usr/bin
umask=0077
min_uid=500
min_gid=500

; Security options
allow_file_group_writeable=false
allow_file_others_writeable=false
allow_directory_group_writeable=false
allow_directory_others_writeable=false

;Check wheter script is within DOCUMENT_ROOT
check_vhost_docroot=true

;Send minor error messages to browser
errors_to_browser=true

[handlers]
;Handler for php-scripts
x-httpd-php="php:/usr/bin/php-cgi"

;Handler for CGI-scripts
x-suphp-cgi="execute:!self"
View entire file here: 
https://invision-web.net/web/sources/etc/suphp.conf

7) Edit the /etc/httpd/conf.d/php.conf apache configuration file for PHP , add following lines :

# Cause the PHP interpreter to handle files with a .php extension.
AddHandler x-httpd-php .php .php4 .php3 .phtml
AddType text/html .php

# SuPHP configuration for PHP5
LoadModule suphp_module modules/mod_suphp.so
suPHP_Engine on
View entire file here:
https://invision-web.net/web/sources/etc/httpd/conf.d/php.conf

8) Edit values in /etc/php.ini to match these:

short_open_tag = On
error_reporting = E_ALL & ~E_DEPRECATED & ~E_NOTICE
display_errors = On
log_errors = On
session.save_path = "/tmp/"
View entire file here: 
https://invision-web.net/web/sources/etc/php.ini

Apache Virtual Hosts

This configuration creates a virtual host for user invision on domains n1.invision-web.net and www.invision-web.net. Adjust your username and domain names accordingly.
You can copy-paste the invision virtual host to add another host/user just by changing the domain and user keywords.

9) Create and Edit the file /etc/httpd/conf.d/user.conf

########################################################
### Apache Configuration for Virtual Hosts for Users ###
########################################################

# Include conf/extra/httpd-userdir.conf
# Allow user public_html directories
UserDir public_html
UserDir disabled root

# Precedence of directory index file to be served (Source: CPanel)
DirectoryIndex index.html.var index.htm index.html index.shtml index.xhtml index.wml index.perl index.pl index.plx index.ppl index.cgi index.jsp index.js index.jp index.php4 index.php3 index.php index.phtml default.htm default.html home.htm index.php5 Default.html Default.htm home.html

# How will apache handle user directories?
<Directory /home/*/public_html>
AllowOverride All
Options MultiViews Indexes SymLinksIfOwnerMatch
<Limit GET POST OPTIONS>
Order allow,deny
Allow from all
</Limit>
<LimitExcept GET POST OPTIONS>
Order deny,allow
Deny from all
</LimitExcept>
</Directory>

#########################
### The virtual hosts ###
#########################

# All virtual hosts will be running according to the domain name
NameVirtualHost *:80
# The main virtual host :: the /var/www directory
<VirtualHost *:80>
ServerName node1
DocumentRoot /var/www
<IfModule mod_suphp.c>
suPHP_UserGroup nobody nobody
</IfModule>
</VirtualHost>

#######################

# Your user's virtual host and domain
<VirtualHost *:80>
# The virtual host server name
ServerName invision-web.net

# Enter any subdomains you want to serve as users public_html directory
ServerAlias www.invision-web.net n1.invision-web.net

# The public e-mail for the administrator (I.E. 404 pages can show this)
ServerAdmin info@invision-web.net

# enable the user directory
UserDir enabled invision

# Where is the user directory?
DocumentRoot /home/invision/public_html

# CGI scripts if any are served as this
ScriptAlias /cgi-bin/ /home/invision/public_html/cgi-bin/
ScriptAlias /~invision/ /home/invision/public_html/

# configure the suphp to run as user
suPHP_AddHandler x-httpd-php
suPHP_AddHandler application/x-httpd-php5
suPHP_UserGroup invision invision

</VirtualHost>
You can view or download the entire file here:
https://invision-web.net/web/sources/etc/httpd/conf.d/user.conf

Test PHP with suPHP

10) Edit file located in your user directory, in this example, the user is invision:

nano /home/invision/public_html/index.php

11) Copy paste this code to /home/invision/public_html/index.php :

<? echo `/usr/bin/whoami`; ?>

12) Next, give the test index.php file the right permissions and owner:

chmod 0755 /home/invision/public_html/index.php
chown invision:invision /home/invision/public_html/index.php

13) Lastly, test your page by visiting your domain you have set up earlier in the Apache virtual hosts:

https://n1.invision-web.net/

The page should output your user’s username:

invision

If it outputs apache then something is wrong with suPHP configuration.

Error Handling and Log Files

You can view details of the Apache error log:

cat /var/log/httpd/error_log | tail -n 20

Also, you can view the suPHP log:

cat /var/log/httpd/suphp_log | tail -n 20

Normally, the suPHP log would output similar to:

[Mon Jul 01 23:17:46 2013] [info] Executing "/home/invision/public_html/index.php" as UID 502, GID 502

Files and Locations Summary

You can explore the source files following this location:
https://invision-web.net/web/sources/
# Configuration files
/etc/php.ini
/etc/suphp.conf
/etc/httpd/conf/httpd.conf
/etc/httpd/conf.d/user.conf

# Test PHP file
/home/invision/public_html/index.php

# Log files
/var/log/httpd/error_log
/var/log/httpd/suphp_log

Useful Links on topic

Tutorials | Posted on July 1, 2013 by .

About Kristijan Burnik

Kristijan Burnik is a Programmer and Web Developer specializing in Server-side and Client-side Technologies and Application Development on Linux Servers and Windows Desktop . Also has experience in Networking, Desktop application development as well as Android mobile application development . Experienced in Programming Languages like Java, C, C++, C#, PHP, Javascript, MySQL and somewhat in other languages like Bash, Perl & Python. Sometimes he works as a Graphical Designer for digital production as well as for printing and advertising. He dedicates his spare time writing Tech Articles on his blog in order to share his work with others, as well as to document his projects for his own use. He's also an Educator & Mentor in field of Algorithms and Programming to young programmers in Zagreb, Croatia.

Leave a Reply