Full Upgrade: NginX 1.9 + PHP7 + MySQL 5.6 on Ubuntu 14.04

Summer’s winding down and my hosting year with amazon is starting over.
Decided to spin up a new server with everything on the latest versions.

At the time I wrote the first version of this article, the PHP 7 compiler was still just a beta, but as of today it’s a full release.

Nginx 1.9.3 was also released just a couple weeks ago. MySQL server hit version 5.6, which the developers claim works better and faster than the previous version.

So I felt like updating everything and spinning my WordPresses back up on the latest software. One sad thing - Ubuntu 15.04 isn’t in the OS list for EC2 on Amazon AWS. So I’ll be working with Ubuntu 14.04.

Second catch - not all of this is available in the repositories, so I’ll have to compile from source. A few years ago I was skeptical about installing software on production servers from source, but over time I’ve run into more and more issues with pre-built software. In that case, something built from source works a lot more stable.

ModSecurity:

So I can sleep better at night, I want to lock my sites down a bit, so I’ll build nginx with mod_security support. And I’m building nginx from source anyway.

Install what’s needed:

 apt-get install libxml2 libxml2-dev apache2-dev libcurl4-openssl-dev

I’ll be working in the folder:

 cd /usr/local/src

Download and unpack:

 wget https://www.modsecurity.org/tarball/2.9.0/modsecurity-2.9.0.tar.gz  
 tar xf modsecurity-2.9.0.tar.gz  
 cd modsecurity-2.9.0

Build:

 ./configure -enable-standalone-module  
 make

PHP 7

Before you start installing, note that NewRelic doesn’t support PHP 7 yet.

Install the needed packages:

 apt-get install libpng++-dev libpng12-dev libmcrypt-dev spawn-fcgi -y

Download the archive and unpack:

 cd /usr/local/src  
 wget -O php-7.0.0.tar.gz http://us1.php.net/get/php-7.0.0.tar.gz/from/this/mirror  
 tar xf php-7.0.0.tar.gz  
 cd php-7*

Build and install. The configure options aren’t much different from previous versions

 ./configure -bindir=/usr/bin -with-config-file-path=/etc -with-curl -with-mhash -enable-mysqlnd -with-mysqli -with-gd -with-pdo-mysql -with-mcrypt -enable-mbstring -with-openssl -with-pcre-regex -enable-soap -with-zlib  
 make -j 2  
 make install

There are two config files in the source folder:

  • php.ini-development
  • php.ini-production

Copy one of them into the etc folder and rename it to php.ini:

 cp php.ini-development /etc/php.ini

Create a script to start cgi:

 vim /etc/init.d/php-fastcgi

Pay attention to the group and user php should run as. I bolded them.

PHP_SCRIPT=/usr/bin/php-fastcgi
FASTCGI_USER=<strong>nginx</strong>
FASTCGI_GROUP=<strong>nginx</strong>
PID_DIR=/tmp/
PID_FILE=/tmp/php-fastcgi.pid
RET_VAL=0

case "$1" in
    start)
      if [[ ! -d $PID_DIR ]]
      then
        mkdir $PID_DIR
        chown $FASTCGI_USER:$FASTCGI_GROUP $PID_DIR
        chmod 0770 $PID_DIR
      fi
      if [[ -r $PID_FILE ]]
      then
        echo "php-fastcgi already running with PID `cat $PID_FILE`"
        RET_VAL=1
      else
        $PHP_SCRIPT
        RET_VAL=$?
      fi
  ;;
    stop)
      if [[ -r $PID_FILE ]]
      then
        kill -9 $(cat $PID_FILE)
        rm $PID_FILE
        RET_VAL=$?
      else
        echo "Could not find PID file $PID_FILE"
        RET_VAL=1
      fi
  ;;
    restart)
      if [[ -r $PID_FILE ]]
      then
        kill -9 $(cat $PID_FILE)
        rm $PID_FILE
        RET_VAL=$?
      else
        echo "Could not find PID file $PID_FILE"
      fi
      $PHP_SCRIPT
      RET_VAL=$?
  ;;
    status)
      if [[ -r $PID_FILE ]]
      then
        echo "php-fastcgi running with PID `cat $PID_FILE`"
        RET_VAL=$?
      else
        echo "Could not find PID file $PID_FILE, php-fastcgi does not appear to be running"
      fi
  ;;
    *)
      echo "Usage: php-fastcgi {start|stop|restart|status}"
      RET_VAL=1
  ;;
esac
exit $RET_VAL
<p>
  Second script:
</p>

```bash
    vim /usr/bin/php-fastcgi
FASTCGI_USER=nginx
FASTCGI_GROUP=nginx
ADDRESS=127.0.0.1
PORT=9000
PIDFILE=/tmp/php-fastcgi.pid
CHILDREN=10
PHP5=/usr/bin/php-cgi

/usr/bin/spawn-fcgi -a $ADDRESS -p $PORT -P $PIDFILE -C $CHILDREN -u $FASTCGI_USER -g $FASTCGI_GROUP -f $PHP5

<p>
  Make them executable:
</p>

```bash
    chmod +x /usr/bin/php-fastcgi<br /> chmod +x /etc/init.d/php-fastcgi

Set to start on boot:

    /usr/sbin/update-rc.d -f php-fastcgi defaults

Nginx:

I put together a separate article for installing NginX:
Building NginX v.1.9.10 from source

MySQL

Last thing left is MySQL. Good thing the MySQL team put together a package for Ubuntu 14.04. Available for download on the following page:
http://dev.mysql.com/downloads/mysql/

Getting ready:

    apt-get install man-db libaio1

Download the bundle package and unpack it:

    mkdir /usr/local/src/mysql<br /> cd /usr/local/src/mysql<br /> wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-server_5.6.26-1ubuntu14.04_amd64.deb-bundle.tar<br /> tar xf mysql-server_5.6.26-1ubuntu14.04_amd64.deb-bundle.tar

Next install them in order:

    dpkg -i mysql-common_5.6.26-1ubuntu14.04_amd64.deb

The client depends on mysql-community-client:

    dpkg -i mysql-community-client_5.6.26-1ubuntu14.04_amd64.deb<br /> dpkg -i mysql-client_5.6.26-1ubuntu14.04_amd64.deb

The server itself depends on mysql-community-server:

    dpkg -i mysql-community-server_5.6.26-1ubuntu14.04_amd64.deb<br /> dpkg -i mysql-server_5.6.26-1ubuntu14.04_amd64.deb

I'd also recommend installing the source package - just in case you need to build something in the future:

    dpkg -i mysql-community-source_5.6.26-1ubuntu14.04_amd64.deb

Set to start on boot:

    /usr/sbin/update-rc.d -f mysql defaults

That's it. I wasn't planning to cover Varnish setup in this article. You can now upload content and enjoy.

Useful articles:

Sources I peeked at:
* http://sharadchhetri.com/2015/02/21/install-nginx-source-code-ubuntu-14-04-lts/