Jump to content
ilbr22

Install NginX and PHP with PHP-FPM, MySQL and APC

Recommended Posts

Posted (edited)

In this tutorial I will compile the latest NginX (1.3.6) and PHP (5.4.7) with FPM, MySQL and APC (3.1.13) on Ubuntu Precise (12.04). For the moment, Suhosin 0.9.33 is not working with PHP 5.4.x. Apart for some version numbers for some dependencies, all the steps should be the similar for Ubuntu Lucid, Maverick, Natty and Oneiric.

I will not explain how to configure NginX here. You can find a lot of resources about that on the nginx wiki.

In order to compile these programs you will need the following dependencies (most of them should already be installed):

sudo apt-get install htop vim-nox binutils cpp flex gcc libarchive-zip-perl libc6-dev m4 libpcre3 libpcre3-dev libssl-dev libpopt-dev lynx make perl perl-modules openssl unzip zip autoconf2.13 gnu-standards automake libtool bison build-essential zlib1g-dev ntp ntpdate autotools-dev g++ bc subversion psmisc re2c

And also some libraries for PHP:

sudo apt-get install libmysqlclient-dev mysql-client libcurl4-openssl-dev libgd2-xpm-dev libjpeg-dev libpng3-dev libxpm-dev libfreetype6-dev libt1-dev libmcrypt-dev libxslt1-dev bzip2 libbz2-dev libxml2-dev libevent-dev libltdl-dev libmagickwand-dev libmagickcore-dev imagemagick libreadline-dev libc-client-dev libsnmp-dev snmpd snmp libpq-dev

Now let's create a folder in which we can play:

mkdir ~/lemp && cd ~/lemp

and start downloading the sources:

wget http://nginx.org/download/nginx-1.3.6.tar.gz
wget http://us.php.net/distributions/php-5.4.7.tar.gz
wget http://pecl.php.net/get/APC-3.1.13.tgz

You can visit each site in order to find faster mirrors. Now decompress them:

tar zxvf nginx-1.3.6.tar.gz
tar xzvf php-5.4.7.tar.gz
tar xzvf APC-3.1.13.tgz

And now let's install them one by one. I like to put everything I compile by myself in one place, the /opt directory. By doing this I know where to find things like configuration files and libraries.

cd nginx-1.3.6/

./configure \
--prefix=/opt/nginx \
--conf-path=/etc/nginx/nginx.conf \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log \
--http-client-body-temp-path=/var/lib/nginx/body \
--http-proxy-temp-path=/var/lib/nginx/proxy \
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi \
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi \
--http-scgi-temp-path=/var/lib/nginx/scgi \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_gzip_static_module \
--user=www-data \
--group=www-data \
--without-mail_pop3_module \
--without-mail_imap_module \
--without-mail_smtp_module

make
sudo make install

cd ..
sudo wget -O /etc/init.d/nginx https://github.com/vladgh/VladGh.com-LEMP/raw/master/init_files/nginx
sudo chmod +x /etc/init.d/nginx
sudo update-rc.d -f nginx defaults
sudo mkdir -p /var/lib/nginx/{body,proxy,fastcgi,uwsgi,scgi}

You sould also create the file /etc/logrotate.d/nginx in order to compress the logs.

sudo sh -c "echo '/var/log/nginx/*.log {
weekly
missingok
rotate 52
compress
delaycompress
notifempty
create 640 root adm
sharedscripts
postrotate
[ ! -f /var/run/nginx.pid ] || kill -USR1 \`cat /var/run/nginx.pid\`
endscript
}' > /etc/logrotate.d/nginx"

You can also find a very simple configuration for nginx at https://github.com/vladgh/VladGh.com/blob/master/nginx.conf, but I strongly recommend to use the Nginx Configuration page

Starting with Ubuntu 11.04 the canonical team modified the path for some libraries so you might need to run the following set of commands in order to get the PHP compilation to work:

arch=$(dpkg-architecture -qDEB_HOST_MULTIARCH)
[ -f /usr/lib/${arch}/libjpeg.so ] && sudo ln -s /usr/lib/${arch}/libjpeg.so /usr/lib/
[ -f /usr/lib/${arch}/libpng.so ] && sudo ln -s /usr/lib/${arch}/libpng.so /usr/lib/
[ -f /usr/lib/${arch}/libXpm.so ] && sudo ln -s /usr/lib/${arch}/libXpm.so /usr/lib/
[ -f /usr/lib/${arch}/libmysqlclient.so ] && sudo ln -s /usr/lib/${arch}/libmysqlclient.so /usr/lib/
[ -d /usr/lib/i386-linux-gnu/mit-krb5 ] && sudo ln -s /usr/lib/${arch}/mit-krb5/lib*.so /usr/lib/

Now let's compile the PHP packages:

sudo mkdir /var/www
sudo chown -R www-data:www-data /var/www
cd php-5.4.7

./buildconf --force

./configure \
--prefix=/opt/php5 \
--with-config-file-path=/etc/php5 \
--with-config-file-scan-dir=/etc/php5/conf.d \
--with-curl \
--with-pear \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-xpm-dir \
--with-freetype-dir \
--with-t1lib \
--with-mcrypt \
--with-mhash \
--with-mysql \
--with-mysqli \
--with-pgsql \
--with-pdo-mysql \
--with-pdo-pgsql \
--with-openssl \
--with-xmlrpc \
--with-xsl \
--with-bz2 \
--with-gettext \
--with-readline \
--with-fpm-user=www-data \
--with-fpm-group=www-data \
--with-imap \
--with-imap-ssl \
--with-kerberos \
--with-snmp \
--disable-debug \
--enable-fpm \
--enable-cli \
--enable-inline-optimization \
--enable-exif \
--enable-wddx \
--enable-zip \
--enable-bcmath \
--enable-calendar \
--enable-ftp \
--enable-mbstring \
--enable-soap \
--enable-sockets \
--enable-shmop \
--enable-dba \
--enable-sysvsem \
--enable-sysvshm \
--enable-sysvmsg

make
sudo make install

The next step is to configure PHP-FPM:

sudo mkdir /var/log/php-fpm
sudo mkdir -p /etc/php5/conf.d
sudo chown -R www-data:www-data /var/log/php-fpm
sudo cp -f php.ini-production /etc/php5/php.ini
sudo chmod 644 /etc/php5/php.ini
sudo cp -f /opt/php5/etc/php-fpm.conf.default /etc/php5/php-fpm.conf
sudo cp -f /opt/php5/etc/pear.conf /etc/php5/pear.conf
sudo cp -f sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
sudo chmod +x /etc/init.d/php-fpm
sudo update-rc.d -f php-fpm defaults

We should also modify the init file and the configuration file for PHP with the standard path for the pid file (/var/run):

sudo sed -i".bak" "s/php_fpm_CONF=.*/php_fpm_CONF=\/etc\/php5\/php-fpm.conf/" /etc/init.d/php-fpm
sudo sed -i "s/php_fpm_PID=.*/php_fpm_PID=\/var\/run\/php-fpm.pid/" /etc/init.d/php-fpm
sudo sed -i".bak" "s/;pid = .*/pid = \/var\/run\/php-fpm.pid/" /etc/php5/php-fpm.conf

You can make the desired modifications to /etc/php5/php-fpm.conf file. This is were you can setup the server to listen on a socket or TCP port, or you can adjust how many processes you want. The file is very well documented in the comments. A sample conf file can be found here.

The newer versions of php complain if a time zone is not set on php.ini (so we grab the system's one)

TIMEZONE=$([ -f /etc/timezone ] && cat /etc/timezone | sed "s/\//\\\\\//g")
sudo sed -i".bak" "s/^\;date\.timezone.*$/date\.timezone = \"${TIMEZONE}\" /g" /etc/php5/php.ini

Create the logrotate file for PHP with the following command:

sudo sh -c "echo '/var/log/php-fpm/*.log {
weekly
missingok
rotate 52
compress
delaycompress
notifempty
create 640 www-data www-data
sharedscripts
postrotate
[ ! -f /var/run/php-fpm.pid ] || kill -USR1 \`cat /var/run/php-fpm.pid\`
endscript
}' > /etc/logrotate.d/php-fpm"

Because the executables for both PHP and NginX are not in a standard location you can add the following lines to /etc/environment so that the new path is always loaded:

export PATH="/opt/php5/bin:/opt/php5/sbin:/opt/nginx/sbin:$PATH"
sudo sh -c "echo \"PATH=\"$PATH\"\" > /etc/environment"

Now you should be able to see the version numbers for NginX and PHP:

nginx -V
php -v

And also the location of the executables:

which nginx
which php

Now let's install the latest APC:

cd ../APC-3.1.13
/opt/php5/bin/phpize -clean
./configure --enable-apc --with-php-config=/opt/php5/bin/php-config --with-libdir=/opt/php5/lib/php
make
sudo make install

You will have to add the extension in php.ini

sudo sh -c "echo 'extension = apc.so
apc.enabled = 1
apc.shm_size = 128M
apc.shm_segments=1
apc.write_lock = 1
apc.rfc1867 = On
apc.ttl=7200
apc.user_ttl=7200
apc.num_files_hint=1024
apc.mmap_file_mask=/tmp/apc.XXXXXX
apc.enable_cli=1
; Optional, for \"[apc-warning] Potential cache slam averted for key... errors\"
; apc.slam_defense = Off' > /etc/php5/conf.d/apc.ini"

This is it! You can now start the servers:

sudo /etc/init.d/php-fpm start
sudo /etc/init.d/nginx start

If you want a MySQL server installed you can run the following command.

apt-get install mysql-server mysql-client

This should be enough to get MySQL up and running. If you have a separate database somewhere you can only install the mysql-client package. For additional config parameters look into the /usr/share/doc/mysql-server-5.1/examples folder and you will find a few configurations. You can also get the MySQLTuner.pl script which will give a lot more information on how to fine tune your mysql server:

wget mysqltuner.pl

You should now have a fully functional LEMP platform.

An unattended script that installs everything can be found at GitHub. Check the README file for instructions on how to run the installer.

source { copy / paste } - vladgh[.]com

Edited by ilbr22

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...