Install and Configure PHP on Ubuntu 14.04

Install PHP

sudo apt-get -y install libapache2-mod-fastcgi php5 php5-fpm php5-cli php5-curl php5-gd php5-imagick php5-mcrypt php5-mysql php-apc php-pear

Configure PHP

  • Enable the PHP Mcrypt library interface.
    sudo php5enmod mcrypt
  • Enable the Apache HTTP Server actions, fastcgi and alias modules.
    sudo a2enmod actions fastcgi alias
  • Backup the PHP-FPM configuration file.
    sudo cp /etc/php5/fpm/php.ini /etc/php5/fpm/php.ini.orig
  • Edit the PHP-FPM configuration file.
    sudo nano /etc/php5/fpm/php.ini
    • Increase the maximum execution time to 120 seconds.
      • Replace
        max_execution_time = 30
      • With
        max_execution_time = 120
    • Increate the maximum uploadable file size to 64M.
      • Replace
        post_max_size = 8M
      • With
        post_max_size = 64M
      • Replace
        upload_max_filesize = 2M
      • With
        upload_max_filesize = 64M
  • Backup the CLI configuration file.
    sudo cp /etc/php5/cli/php.ini /etc/php5/cli/php.ini.orig
  • Edit the CLI configuration file.
    sudo nano /etc/php5/cli/php.ini
    • Increase the maximum execution time to 120 seconds.
      • Replace
        max_execution_time = 30
      • With
        max_execution_time = 120
    • Increate the maximum uploadable file size to 64M.
      • Replace
        post_max_size = 8M
      • With
        post_max_size = 64M
      • Replace
        upload_max_filesize = 2M
      • With
        upload_max_filesize = 64M
  • Backup the PHP-FPM pool www configuration file.
    sudo cp /etc/php5/fpm/pool.d/www.conf /etc/php5/fpm/pool.d/www.conf.orig
  • Edit the PHP-FPM pool www configuration file.
    sudo nano /etc/php5/fpm/pool.d/www.conf
    • Limit the number of requests each child process can execute before respawning.
      • Replace
        ;pm.max_requests = 500
      • With
        pm.max_requests = 500
    • Enable the PHP-FPM status page.
      • Replace
        ;pm.status_path = /status
      • With
        pm.status_path = /fpm-status
    • Enable the PHP-FPM ping page.
      • Replace
        ;ping.path = /ping
      • With
        ping.path = /fpm-ping
  • Create an Apache HTTP Server PHP-FPM configuration file.
    sudo nano /etc/apache2/conf-available/php-fpm.conf
    • Add
      <IfModule mod_fastcgi.c>
        # Configure all that stuff needed for using PHP-FPM as FastCGI.

        # Set handlers for PHP files.
        # application/x-httpd-php                        phtml pht php
        # application/x-httpd-php3                       php3
        # application/x-httpd-php4                       php4
        # application/x-httpd-php5                       php
        <FilesMatch ".+\.ph(p[345]?|t|tml)$">
          SetHandler application/x-httpd-php
        </FilesMatch>
        # application/x-httpd-php-source                 phps
        <FilesMatch ".+\.phps$">
          SetHandler application/x-httpd-php-source
          # Deny access to raw php sources by default
          # To re-enable it's recommended to enable access to the files only in
          # specific virtual host or directory
          #@todo Determine why 'Require all denied' fails (http://httpd.apache.org/docs/2.4/upgrading.html#run-time)
          Require all granted
        </FilesMatch>

        # Set handler for php5-fpm URLs.
        <LocationMatch "/(fpm-ping|fpm-status)">
          SetHandler application/x-httpd-php
        </LocationMatch>

        # Deny access to files without filename (e.g. '.php')
        <FilesMatch "^\.ph(p[345]?|t|tml|ps)$">
          #@todo Determine why 'Require all denied' fails (http://httpd.apache.org/docs/2.4/upgrading.html#run-time).
          Require all granted
        </FilesMatch>

        # Define Action and Alias needed for FastCGI external server.
        Action application/x-httpd-php /fcgi-bin/php5-fpm virtual
        Alias /fcgi-bin/php5-fpm /fcgi-bin-php5-fpm
        <Location /fcgi-bin/php5-fpm>
          # Here we prevent direct access to this Location URL, env=REDIRECT_STATUS
          # will let us use this fcgi-bin URL only after an internal redirect (by
          # Action upper).
          #@todo Determine why 'Require all denied' fails (http://httpd.apache.org/docs/2.4/upgrading.html#run-time).
          #Order Deny,Allow
          #Deny from all
          Require all granted
          Allow from env=REDIRECT_STATUS
        </Location>

        FastCgiExternalServer /fcgi-bin-php5-fpm -socket /var/run/php5-fpm.sock -pass-header Authorization -idle-timeout 120
      </IfModule>
  • Enable the Apache HTTP Server PHP-FPM configuration file.
    sudo a2enconf php-fpm
  • Backup the PHP APCU configuration file.
    sudo cp /etc/php5/mods-available/apcu.ini /etc/php5/mods-available/apcu.ini.orig
  • Edit the PHP APCU configuration file.
    sudo nano /etc/php5/mods-available/apcu.ini
    • Set the APC shm_size to {MEMORY_APC_CACHE}.
      • Add
        apc.shm_size = {MEMORY_APC_CACHE}
  • Restart the php5-fpm and apache2 services.
    sudo service php5-fpm restart
    sudo service apache2 restart

Open Source Software:

Operating System: