Friday 18 September 2015

Install nginx PHP5 FastCGI Webserver On RedHat

Step # 1: Enable EPEL repo

ngnix is not included in the base system. Turn on EPEL repo to install nginx stable release:
# rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/$(uname -m)/epel-release-5-3.noarch.rpm

Step # 2: Install ngnix

Type the following command at a shell prompt:
# yum install nginx
Sample output:
Loaded plugins: downloadonly, fastestmirror, priorities, protectbase
Loading mirror speeds from cached hostfile
 * epel: archive.linux.duke.edu
 * base: ftp.linux.ncsu.edu
 * updates: centos.mirror.nac.net
 * addons: mirror.cs.vt.edu
 * extras: centos.mirror.nac.net
0 packages excluded due to repository protections
Setting up Install Process
Parsing package install arguments
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 0:0.6.34-1.el5 set to be updated
--> Finished Dependency Resolution
Dependencies Resolved
==============================================================================================================================================================
 Package                             Arch                                 Version                                    Repository                          Size
==============================================================================================================================================================
Installing:
 nginx                               x86_64                               0.6.34-1.el5                               epel                               319 k
Transaction Summary
==============================================================================================================================================================
Install      1 Package(s)
Update       0 Package(s)
Remove       0 Package(s)
Total size: 319 k
Is this ok [y/N]: y
Downloading Packages:
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing     : nginx                                             [1/1]
Installed: nginx.x86_64 0:0.6.34-1.el5
Complete!

nginx configuration file

  • Default config file: /etc/nginx/nginx.conf
  • Default SSL config file: /etc/nginx/conf.d/ssl.conf
  • Default virtual hosting config file: /etc/nginx/conf.d/virtual.conf
  • Default documentroot: /usr/share/nginx/html

Configure PHP As FastCGI

Type the following to install php5 with other modules:
# yum install php-pear-Net-Socket php-pear php-common php-gd php-devel php php-mbstring php-pear-Mail php-cli php-imap php-snmp php-pdo php-xml php-pear-Auth-SASL php-ldap php-pear-Net-SMTP php-mysql

Install spawn-fcgi simple program for spawning FastCGI processes

Type the following command:
# yum install spawn-fcgi
Next, download spawn-fcgi init.d shell script:
# wget http://bash.cyberciti.biz/dl/419.sh.zip
# unzip 419.sh.zip
# mv 419.sh /etc/init.d/php_cgi
# chmod +x /etc/init.d/php_cgi

Start php app server, enter:
# /etc/init.d/php_cgi start
# netstat -tulpn | grep :9000

Sample output:
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      14294/php-cgi
By default php server listens on 127.0.0.1:9000 port. Finally, update /etc/nginx/nginx.conf as follows:
# vi /etc/nginx/nginx.conf
Modify / append as follows:
 
    location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }
 
Save and close the file. Restart nginx:
# service nginx restart
Create /usr/share/nginx/html/test.php as follows:
 
<?php
     phpinfo();
?>
 

How To install PHP7 MariaDB nginx Apache on Debian 7 (Wheezy)


install mariadb

sudo apt-get update
sudo apt-get install python-software-properties
sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 0xcbcb082a1bb943db
sudo add-apt-repository 'deb http://sfo1.mirrors.digitalocean.com/mariadb/repo/10.0/debian wheezy main'
sudo apt-get update; sudo apt-get install mariadb-server

uninstall php5 and apache2, we will be building it from source.

sudo apt-get remove php5
sudo apt-get remove php5-common
sudo apt-get remove apache2-common
sudo apt-get remove apache2
sudo apt-get remove apache2.2-bin 

download and prepare apache2 for building

mkdir apache
cd apache
wget -nd http://ftp.wayne.edu/apache//httpd/httpd-2.4.16.tar.gz
tar xzf httpd-2.4.16.tar.gz

wget -nd http://www.interior-dsgn.com/apache//apr/apr-1.5.2.tar.gz
wget -nd http://www.interior-dsgn.com/apache//apr/apr-util-1.5.4.tar.gz
tar xzf apr-util-1.5.4.tar.gz
tar xzf apr-1.5.2.tar.gz
cd httpd-2.4.16/srclib/    
mv ../../apr-util-1.5.4 apr-util
mv ../../apr/apr-1.5.2/ apr
cd ../../
should have worked now but we need pcre. wget -ndftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre2-10.20.zip unzip pcre2-10.20.zip cd pcre2-10.20 ./configure --prefix=/usr/local/pcre make sudo make install
lets configure and build and install apache2 cd httpd-2.4.16
./configure --with-included-apr --enable-so 
make

sudo make install
now time for php7!
./configure \
--prefix=$HOME/tmp/usr \
--with-config-file-path=$HOME/tmp/usr/etc \
--enable-mbstring \
--enable-zip \
--enable-bcmath \
--enable-pcntl \
--enable-ftp \
--enable-exif \
--enable-calendar \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-wddx \
--with-curl \
--with-mcrypt \
--with-iconv \
--with-gmp \
--with-pspell \
--with-gd \
--with-jpeg-dir=/usr \
--with-png-dir=/usr \
--with-zlib-dir=/usr \
--with-xpm-dir=/usr \
--with-freetype-dir=/usr \
--enable-gd-native-ttf \
--enable-gd-jis-conv \
--with-openssl \
--with-pdo-mysql \
--with-gettext=/usr \
--with-zlib=/usr \
--with-bz2=/usr \
--with-recode=/usr \
I got:
+--------------------------------------------------------------------+
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE.  By continuing this installation |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+

Thank you for using PHP.
Woohoo!
make
I see:
Build complete.
Don't forget to run 'make test'.
Okay lets test it!
make test
Now lets really do it. (cross fingers)
sudo make install

Install PHP on Debian without Apache Tips

When you apt-get install php5 on a Debian/Ubuntu server, you’ll notice that APT will automatically install a bunch of apache2 packages as well. This can be pretty annoying if you’re planning on using another web server (or no web server at all).
If you take a look at the package dependencies (Debian/Ubuntu) you’ll see why this happens - php5 needs one of either libapache2-mod-php5libapache2-mod-php5filter,php5-cgi, or php5-fpm. APT doesn’t care which package it installs; it just picks the first package that satisfies the dependency, which is why you get the apache2packages.
You can get around this by installing one of the other dependencies before php5. For example, apt-get install php5-fpm php5 or apt-get install php5-cgi php5.

Install HHVM Nginx/Apache on Ubuntu/Debian/Mint

HHVM, or the HipHop Virtual Machine, is a virtual machine for PHP developed by Facebook to improve the performance of PHP applications. Unlike the regular PHP runtime, HHVM uses a just-in-time compiler to convert scripts into native machine code. As a result, third-party benchmarks have shown as much as a 3x load time reduction over PHP-FPM 5.4 for tasks like loading a regular Drupal website.

A Word of Warning

While very fast, HHVM is also still under development and may not run some software properly, or may not support some necessary extensions. Proceed with caution. For a list of supported, integrated PHP extensions, follow this link.

Supported Distributions

  • Ubuntu
    • 10.04 (lucid)
    • 12.04 (precise)
    • 14.04 (trusty)
  • Debian
    • 8 (jessie)
    • 7 (wheezy)
  • Mint
    • 16 (petra)
These are the distributions that Facebook and the HHVM maintainers will support, and the distributions that are still actively maintained for servers. While it is possible to install HHVM on an Ubuntu 14.10 server, doing so is not supported (at the time of writing) by HHVM and may result in bad things happening.

Requirements

  1. One of the distributions above.
  2. Root access for configuration installing packages.

Installing HHVM

Installing HHVM itself is quick and painless, involving not much more than configuring repositories and installing.

Ubuntu

For lucid (10.04) and precise (12.04) users only: Both versions of Ubuntu require the addition of repositories. To streamline the process, we need to make sure that the add-apt-repository command is ready. If you are not using lucid or precise, skip over this set of commands. Otherwise:
sudo apt-get update
sudo apt-get install python-software-properties
For lucid (10.04) users only: HHVM has a few more dependencies that are not included in the base system or repositories.
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install gcc-4.8 g++-4.8 gcc-4.8-base
For precise (12.04) users only: You'll also need to add a repository to obtain libraries needed to run HHVM.
sudo add-apt-repository ppa:mapnik/boost

HHVM also requires installing a GPG key for its repository.
wget -O - http://dl.hhvm.com/conf/hhvm.gpg.key | sudo apt-key add -
Once that's done, we can add HHVM's repository to a sources.list file.
echo deb http://dl.hhvm.com/ubuntu DISTRIBUTION_VERSION main | sudo tee /etc/apt/sources.list.d/hhvm.list
Make sure to replace DISTRIBUTION_VERSION with your Ubuntu version's codename: lucid, precise, or trusty.
Now we can install.
sudo apt-get update
sudo apt-get install hhvm
And we're done!

Debian

HHVM installation on Debian is similar to Ubuntu, but less fragmented across distributions. You'll only need one set of commands for jessie or wheezy.
wget -O - http://dl.hhvm.com/conf/hhvm.gpg.key | sudo apt-key add -
echo deb http://dl.hhvm.com/debian DISTRIBUTION_VERSION main | sudo tee /etc/apt/sources.list.d/hhvm.list
sudo apt-get update
sudo apt-get install hhvm
Make sure to replace DISTRIBUTION_VERSION on the second line with your Debian version's codename, jessie or wheezy. HHVM should now be installed.

Mint

Mint installation is also very similar to Debian in that it's simplified.
wget -O - http://dl.hhvm.com/conf/hhvm.gpg.key | sudo apt-key add -
echo deb http://dl.hhvm.com/mint petra main | sudo tee /etc/apt/sources.list.d/hhvm.list
sudo apt-get update
sudo apt-get install hhvm
Since petra is the only supported Mint distribution at the moment, that's it!

Configuring with Apache/Nginx

With HHVM comes a nifty configuration script that automatically sets up a CGI handler for either server.
If you are using Nginx, make sure to edit your server's configuration file (by default /etc/nginx/sites-available/default) to disable FastCGI processing. Look for a section like the following and make sure it either does not exist or is entirely commented out (by adding # to the beginning of each line in the section):
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
#       fastcgi_split_path_info ^(.+\.php)(/.+)$;
#       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#
#       # With php5-cgi alone:
#       fastcgi_pass 127.0.0.1:9000;
#       # With php5-fpm:
#       fastcgi_pass unix:/var/run/php5-fpm.sock;
#       fastcgi_index index.php;
#       include fastcgi_params;
#}
If you are using Apache, there's nothing specific for you to do.
After that, simply run the following script.
sudo /usr/share/hhvm/install_fastcgi.sh

Testing HHVM

Apache or Nginx should automatically be configured and restarted, and HHVM should now be running on your server. To test it, you can either make a file like this:
<?php phpinfo();>
And look for "HipHop" or "HHVM," or you can run a script like this:
<?php if(defined('HHVM_VERSION')) { echo 'HHVM works!'; }
If "HHVM works!" appears, then you're all set!

Install mod_fastcgi and PHP-FPM on Debian 7 (Wheezy) with Apache

This article explains how to configure and install mod_fastcgi and PHP-FPM on a Debian 7 instance using Apache. Apache’s default configuration, which uses mod_php instead of mod_fastcgi, uses a significant amount of system resources.
The main reason mod_php uses more resources is because it is loaded even for non-PHP files (like plain HTML and JavaScript files). The FastCGI Process Manager (PHP-FPM) helps to reduce the amount of system resources used by forcing the web server to act as a proxy and passing only files ending with the php file extension to PHP-FPM.
Additionally, using PHP-FPM allows each virtual host to be configured to run PHP code as individual users. Previously, this was only possible by using suPHP.
This guide assumes that you are familiar and comfortable with setting up LAMP stacks on Debian 7. If you are new to Linux server administration, you may be interested in reading our Linux System Administration Basics documentation series.

Installing mod_fastcgi and PHP-FPM

Both mod_fastcgi and PHP-FPM are part of repositories for aptitude supported by Debian 7. The following are necessary steps to install mod_fastcgi and PHP-FPM.
  1. Update the apt-get repositories
    1
    sudo apt-get update && sudo apt-get upgrade --show-upgraded
    
  2. See if mod_fastcgi is available. By default, the Debian 7 does not include the necessary repositories to install mod_fastcgi because it is a contrib module and is non-free (in terms of Debian’s licensing restrictions).
    1
    sudo apt-cache search libapache2-mod-fastcgi
    
  3. If it is not available, you will need to edit your /etc/apt/sources.list file to allow for contrib and non-free software to be loaded in the repository list. Your sources file should look like:
    a) If you are using Linode’s mirrors:
    /etc/apt/sources.list
    1
    2
    3
    4
    5
    6
    7
    8
    9
    deb http://mirrors.linode.com/debian/ wheezy main contrib non-free
    deb-src http://mirrors.linode.com/debian/ wheezy main contrib non-free
    
    deb http://mirrors.linode.com/debian-security/ wheezy/updates main contrib non-free
    deb-src http://mirrors.linode.com/debian-security/ wheezy/updates main contrib non-free
    
    # wheezy-updates, previously known as 'volatile'
    deb http://mirrors.linode.com/debian/ wheezy-updates main
    deb-src http://mirrors.linode.com/debian/ wheezy-updates main
    
    b) If you are using Debian’s mirrors:
    /etc/apt/sources.list
    1
    2
    3
    4
    5
    6
    7
    8
    deb http://ftp.es.debian.org/debian stable main contrib non-free
    deb-src http://ftp.es.debian.org/debian stable main contrib non-free
    
    deb http://ftp.debian.org/debian/ wheezy-updates main contrib non-free
    deb-src http://ftp.debian.org/debian/ wheezy-updates main contrib non-free
    
    deb http://security.debian.org/ wheezy/updates main contrib non-free
    deb-src http://security.debian.org/ wheezy/updates main contrib non-free
    
  4. Update the apt-get repositories.
    1
    sudo apt-get update && sudo apt-get upgrade --show-upgraded
    
  5. Install mod_fastcgi and PHP-FPM.
    1
    sudo apt-get install libapache2-mod-fastcgi php5-fpm
    

Configuring Apache with PHP-FPM

We will now configure Apache to pass all requests for PHP files, with the php file extension, to the PHP wrapper through FastCGI.
  1. Enable the mod_actions module with the following command:
    1
    sudo a2enmod actions
    
  2. Configure PHP-FPM to use UNIX sockets instead of TCP. In this command, we will use grepto determine if the sockets are already being used. In a standard installation, they will be.
    1
    sudo grep -E '^\s*listen\s*=\s*[a-zA-Z/]+' /etc/php5/fpm/pool.d/www.conf
    
    You should see the following output:
    1
    listen = /var/run/php5-fpm.sock
    
    If you see the above output, skip to step 6.
  3. If no output is returned, you will need to edit the following file and add this line:
    etc/php5/fpm/pool.d/www.conf
    1
    listen = /var/run/php5-fpm.sock
    
  4. Find the following line and remove it.
    /etc/php5/fpm/pool.d/www.conf
    1
    listen = 127.0.0.1:9000
    
  5. Restart the php5-fpm daemon for these changes to take effect.
    1
    sudo service php5-fpm restart
    
  6. Check for the version of Apache with the following command.
    1
    apache2 -v
    
  7. Depending on your Apache version, edit the following file accordingly.
    Apache 2.2 or earlier
    /etc/apache2/mods-enabled/fastcgi.conf
    1
    2
    3
    4
    5
    6
    <IfModule mod_fastcgi.c>
     AddType application/x-httpd-fastphp5 .php
     Action application/x-httpd-fastphp5 /php5-fcgi
     Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi
     FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -socket /var/run/php5-fpm.sock -pass-header Authorization
    </IfModule>
    
    Apache 2.4 or later
    /etc/apache2/mods-enabled/fastcgi.conf
    1
    2
    3
    4
    5
    6
    7
    8
    9
    <IfModule mod_fastcgi.c>
     AddType application/x-httpd-fastphp5 .php
     Action application/x-httpd-fastphp5 /php5-fcgi
     Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi
     FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -socket /var/run/php5-fpm.sock -pass-header Authorization
     <Directory /usr/lib/cgi-bin>
      Require all granted
     </Directory>
    </IfModule>
    
  8. Save the file and check for configuration errors.
    1
    sudo apache2ctl configtest
    
  9. As long as you received Syntax OK as a result of that command, restart the Apache service:
    1
    sudo service apache2 restart
    
    If you did not get the Syntax OK result, check your configuration for errors.
  10. Check if the PHP is working by creating and accessing a page with phpinfo() displayed. The following command will create info.php in /var/www (default directory for websites in Apache):
    1
    sudo echo "<?php phpinfo(); ?>" > /var/www/info.php
    

Configuring PHP Pools (Optional)

PHP-FPM brings in the concept of pools. Using pools you can control the amount of resources dedicated to each virtual host, and also run PHP scripts as different users.
In this section we will create a pool for the domain example.com which is owned by the user bob.
  1. Create a copy of the original pool file to make changes to using the following command.
    1
    sudo cp /etc/php5/fpm/pool.d/www.conf /etc/php5/fpm/pool.d/example.com.conf
    
  2. Edit the file to change the site name, socket name, and user/group.
    /etc/php5/fpm/pool.d/example.com.conf
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    ; Start a new pool named 'www'.
    ; the variable $pool can we used in any directive and will be replaced by the
    ; pool name ('www' here)
    [example.com]
    
    ...
    
    ; Unix user/group of processes
    ; Note: The user is mandatory. If the group is not set, the default user's group
    ;       will be used.
    user = bob
    group = bob
    
    ...
    
    listen = /var/run/php5-fpm_example.com.sock
    
  3. Restart the php5-fpm process for the new pool to be created.
    1
    sudo service php5-fpm restart
    
  4. Edit the virtual host file of example.com to use this PHP-FPM pool
    /etc/apache2/sites-available/example.com.conf
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    <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/error.log
        CustomLog /var/www/example.com/access.log combined
    
        <IfModule mod_fastcgi.c>
            AddType application/x-httpd-fastphp5 .php
            Action application/x-httpd-fastphp5 /php5-fcgi
            Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi_example.com
            FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi_example.com -socket /var/run/php5-fpm_example.com.sock -pass-header Authorization
        </IfModule>
    
    </VirtualHost>
    
  5. Check the configuration file for errors.
    1
    sudo apache2ctl configtest
    
  6. If there were no errors, restart Apache.
    1
    sudo apache2 restart
    
  7. Create a PHP file inside the DocumentRoot of this domain to check the owner of this PHP-FPM pool.
    /var/www/example.com/public_html/user.php
    1
    2
    3
    4
    <?php
    $processUser = posix_getpwuid( posix_geteuid() );
    print $processUser('name');
    ?>
    
  8. Access the following URL in a web browser, replacing example.com with your domain or IP address.
    1
    http://example.com/user.php
    
The page should say bob.

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.