Sunday, 27 January 2013

Installing and Configuring nginx and PHP on OpenBSD 5.2

I've been a long time OpenBSD system administrator and have dealt with Apache 1.3 most of the time. With the coming of OpenBSD 5.2 the base install now includes the mysterious (to me, anyways) nginx. I'm currently working on a project that involves updating an older OpenBSD 5.0 box running Apache. Since the developers needed PHP-5.3 I thought this would be a good opportunity to take a closer look at nginx. 

So to start this whole process, I backed up the important stuff from the old machine:

$ sudo tar czvf /waterdb.tgz /etc /var/www

 If your UNIX skills are a little rusty I'm using tar to backup my /etc and /var/www directories and store them in a gzip compressed archive on my root parition, all done through sudo because we need to be root. After that I used scp to copy it to another machine.

Next I downloaded the i386 version of the ramdisk kernel:

ftp ftp://ftp.openbsd.org/pub/OpenBSD/5.2/i386/bsd.rd

and moved it to my root partition (overwriting the old 5.0 bsd.rd):

$ mv ~/bsd.rd /bsd.rd

With that in place it was time to bid farewell to my system and reboot. 

I stopped the boot loader at the:

boot> 

screen (by hitting the space key) and told it to boot my ramdisk kernel:

boot> /bsd.rd 

After hitting enter and watching the kernel fly by I was left at the installer menu. 

I won't get too much into how to install OpenBSD because there are better tutorials out there, namely the INSTALL.i386 file and for the lazy visual learners this YouTube video (you can ignore all the stuff about VirtualBox): 

http://www.youtube.com/watch?v=014wVkGEi5c

For the install, I created a separate /var/www partition and only installed:


base52.tgz = base system

etc52.tgz = etc directory

man52.tgz = man pages (not essential)

bsd = kernel

bsd.rd = emergency / install ramdisk kernel

After rebooting the system, I enabled the user I created during the install to run commands via sudo by logging in a root and running:

# visudo That ran vi on the sudoers file, to actually make the change I used the arrow keys and scrolled down to the lines:

# Uncomment to allow people in group wheel to run all commands# and set environment variables.# %wheel  ALL=(ALL) SETENV: ALL

And while my cursor was on top of the "#" in front of the "%wheel" line I tapped x on my keyboard to delete the "#" character. To save and edit I pressed Esc and then typed :wq to write and quit the file. (If you run into trouble, hitting Esc and then typing :q! will get you back to the shell, without saving).

Now that my regular user had sudo abilities I logged off as root and back in as my user. 

The next step was to install php-frm by running:

$ sudo pkg_add php-fpm

pkg_add went off and installed the dependencies I needed. Once that was all said and done it was time to configure php-fpm and nginx.

Now there are a few ways of having nginx talk to the PHP module, I wanted to use unix sockets because they involve less overhead than TCP. 

To do this you'll have to remember that when nginx is running, it's inside of the chroot so the root directory (/) in config files (and scripts running on the site) is actually at /var/www when on the system.

To have php-fpm leave a socket where nginx can see it I ran:


$ sudo vi /etc/php-fpm.conf

and changed the line:

listen = 127.0.0.1:9000

to:

listen = /var/www/tmp/php.sock


By using the x to delete and then Esc and i to switch vi back into inserting mode.

After saving and exiting, it was time to edit the nginx.conf:

$ sudo vi /etc/nginx/nginx.conf


I looked for the lines:

location ~ \.php$ {
            root           /htdocs;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /htdocs$fastcgi_script_name;
            include        fastcgi_params;
        }

and uncommented them. I also changed:

fastcgi_pass   127.0.0.1:9000; 

to:

fastcgi_pass   unix:/tmp/php.sock;

If you're wondering why it's not unix:/var/www/tmp/php.sock it's because of the chroot. 

Before exiting I also changed this line:

index  index.html index.htm;

to:

index  index.html index.htm index.php;

Now, I was ready to make a test file:

$ sudo vi /var/www/htdocs/phpinfo.php

with:

<?php phpinfo(); ?>

And finally start the services:

$ sudo nginx

$ sudo /usr/local/sbin/php-fpm-5.3

Then fire up a browser to my site /phpinfo.php, and bask in the glory of PHP and nginx living in harmony :)

As a side note, I'll post some instructions on how to get nginx and php-fpm-5.3 to start up at boot time.

EDIT: Okay, so this is probably just for 5.2 as it will be different in 5.3:

$ sudo vi /etc/rc.local.conf

Add the line:

pkg_scripts="nginx php_fpm"

Save & exit and you're done! (maybe reboot to check it).

Thanks for reading!