Install NginX with PHP-FPM in Ubuntu 10.04

Categories: Ubuntu; Tags: php-fpm, NginX;

If you want a web server with a very small memory footprint then you should think of trying NginX. It's very light weight and in some situations it performs much better than Apache (like serving static content). Many websites, use it as a frontend to apache, so that all the static content is served by NginX and the rest is proxied to a backend Apache. It can also handle php files by itself with the help of fast-cgi, spawn-fcgi or php-fpm. Php-Fpm is not as mature as Apache's mod_php, but it performs about the same.

So now let's install NginX with a php-fpm proxy.

I usually do not compile programs from sources, especially in Ubuntu, but when it comes to such a small application there are some advantages (like having the newest version or changing the server signature).

cd ~
wget http://sysoev.ru/nginx/nginx-0.7.67.tar.gz
tar -zxvf nginx*
cd nginx*/

At this point you can change the server signature by modifing the following file:

vi +48 src/http/ngx_http_header_filter_module.c

And changing the following line to read something like:

static char ngx_http_server_string[] = "Server: Vlad Web Server" CRLF;
static char ngx_http_server_full_string[] = "Server: Vlad Web Server" CRLF;

Now let's configure an install it:

mkdir /var/tmp/nginx
 
./configure \
  --prefix=/usr \
  --conf-path=/etc/nginx/nginx.conf \
  --http-log-path=/var/log/nginx/access.log \
  --error-log-path=/var/log/nginx/error.log \
  --pid-path=/var/run/nginx.pid \
  --lock-path=/var/lock/nginx.lock \
  --user=nginx \
  --group=nginx \
  --http-client-body-temp-path=/var/tmp/nginx/client \
  --http-proxy-temp-path=/var/tmp/nginx/proxy \
  --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi &&
make &&
make install
 
adduser --system --no-create-home --disabled-login --disabled-password --group nginx

At this point you have the main executable in /usr/sbin, the temporary files in /var/tmp/nginx, and the configuration files in /etc/nginx. You also created a use called nginx.

The next step is to create the start-up script for Ubuntu. You can download a copy from here: http://code.google.com/p/nginx-init-ubuntu/.

cd ~
wget http://nginx-init-ubuntu.googlecode.com/files/nginx-init-ubuntu_v2.0.0-RC2.tar.bz2
tar --use-compress-program bzip2 -xvf nginx-init-ubuntu_v2.0.0-RC2.tar.bz2
mv nginx /etc/init.d

Do not forget to edit the files and change the path of the executable and configuration path.

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

vi /etc/logrotate.d/nginx

And add the following lines:

/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
}

Now let's install PHP with FPM:

apt-get install python-software-properties
add-apt-repository ppa:brianmercer/php
apt-get update
apt-get install php5-fpm php-apc php5-cgi php5-cli php5-mysql php5-common php-pear php5-curl php5-suhosin php5-gd php5-imagick imagemagick
 
echo "apc.shm_size = 64" >> /etc/php5/conf.d/apc.ini
echo "apc.rfc1867 = on" >> /etc/php5/conf.d/apc.ini
sed -i'.original' 's/^# configuration for php imagick module/; configuration for php imagick module/' /etc/php5/conf.d/imagick.ini

The last thing you have to do is to configure the nginx server. Wiki.NginX.org has some very interesting examples, or you can download the attached configuration files, and use them as an example.

/etc/init.d/php5-fpm restart && /etc/init.d/nginx/restart

Installing MySql Server on Ubuntu is a very simple task:

sudo apt-get install mysql-server mysql-client

One useful utility is the mysql_secure_installation script, which limits access to the ‘root’ account, removes the test database, and removes anonymous accounts.

mysql_secure_installation

That was the installation part. Now, for configuration things get a little bit more complicated. It all depends on your server's RAM and processing power. I recommend that you use MySqlTuner, a small perl script that gives you useful information on how to "fine tune" your MySql configuration. All the configuration is done by editing /etc/mysql/my.cnf file. For a low memory VPS (256MB) you can add the following lines to it:

key_buffer = 16K
max_allowed_packet = 1M
thread_stack = 64K
table_cache = 4
sort_buffer = 64K
net_buffer_length = 2K
skip-innodb

*You can alos use the atached my.cnf file as an example.

Restart the MySQL Server:

/etc/init.d/mysql restart

In order to use the mail function you should also install the Postfix server:

sudo apt-get install postfix

General type of mail configuration: <-- Internet Site
System mail name: <-- srv.vladgh.com

For a null client (a server that only sends emails), you replace the existing /etc/postfix/main.cf file with the following:

myorigin = $mydomain
relayhost =
inet_interfaces = loopback-only
local_transport = error:local delivery is disabled
smtpd_banner = $myhostname ESMTP $mail_name
alias_maps = hash:/etc/aliases
message_size_limit = 104857600

Edit also the /etc/aliases file accordingly:

admin: root
root: yourname@yourdomain.com

Make changes permanent with:

newaliases
/etc/init.d/postfix restart
AttachmentSize
nginx.conf1.8 KB
security.conf448 bytes
vladgh.com_.conf1.47 KB
my.cnf1.33 KB

Books

NginX HTTP Server

The book includes detailed instructions for each of the processes it describes: downloading and installing the application, configuring and using modules, and much more. It provides a step-by-step tutorial to replace your existing web server with Nginx. With commented configuration sections and in-depth module descriptions, you will be able to make the most of the performance potential offered by Nginx.

Source: Packt Publishing

Google AdSense

Affiliates