Wednesday, December 17, 2014

PHP Installation on OpenBSD 5.7 relayd-based httpd

In an effort to return to my OpenBSD roots, I've been planning a migration from the licensing conflagration that is pfsense (more on that later).  One of the features I've been keenly watching across the past 3-4 releases of OpenBSD was that their fork of Apache 1.3 was to be dropped in favor of nginx.  It was subsequently decided that nginx was too bloated (that was quick!), so a new http daemon was borne from relayd.

Given that I have some requirement for PHP, I thought I'd write a quick blurb on how to perform a fast, basic installation.

The new httpd supports a similar architecture as nginx using a unix socket with which to communicate to php-fpm via fastCGI.

Let's assume you have a fresh 5.7 installed.  Let's also assume you're root, though we all know the dangers of acting with unilateral authority as root.

First, let's get httpd running by creating a configuration file.  Create your /etc/httpd.conf as follows (this is very minimalistic):

# cat < /etc/httpd.conf
> server "default" {
>         listen on egress port 80
>         location "*.php" {
>                 fastcgi socket "/run/php-fpm.sock"
>         }
> }
> EOF

Secondly, we'll set a package path.  I chose rit.edu's mirror - you can select your own from OpenBSD's list.

# export PKG_PATH=http://filedump.se.rit.edu/pub/OpenBSD/snapshots/packages/`machine -a`/ 

Next, add the php-fpm package.  I'm forcing the 5.4.38, but you can select any version you like.  The -I switch disables the interactive prompts (assuming defaults) and the -v switch enables more verbosity.

# pkg_add -I -v php-fpm-5.4.38

You'll see a jumble of output, but it should eventually install.

I then created /var/www/htdocs/phpinfo.php to test for PHP's functionality.

# cat < /var/www/htdocs/phpinfo.php
>
> EOF

Lastly, I started php_fpm as well as httpd.  Note the -f on httpd may be necessary - I'm forcing it for the demonstration, but ultimately it's up to you.

# /etc/rc.d/php_fpm start
php_fpm(ok)

# /etc/rc.d/httpd -f start
httpd(ok)

At this point, you should be ready to test your installation of OpenBSD's new httpd + php by visiting your site at http://127.0.0.1/phpinfo.php (if you're local to the machine).

Enjoy!





P.S. To run Perl in CGI on the new httpd, you'll need to do a little extra!

Copy the necessary binary and its accompanying libraries to their respective directories in the /var/www root: (don't forget to create any necessary directories first!)

# mkdir -p /var/www/usr/{bin,lib,libexec}
# cp /usr/bin/perl /var/www/usr/bin
# cp /usr/lib/libpthread.so.18.1 /var/www/usr/lib
# cp /usr/lib/libperl.so.17.0 /var/www/usr/lib
# cp /usr/lib/libm.so.9.0 /var/www/usr/lib
# cp /usr/lib/libutil.so.12.1 /var/www/usr/lib
# cp /usr/lib/libc.so.78.1 /var/www/usr/lib
# cp /usr/libexec/ld.so /var/www/usr/libexec


Don't forget to set ownership/permissions!

# chown www:www /var/www/usr/bin/perl \
# /var/www/usr/lib/{libpthread.so.18.1,libperl.so.17.0,libm.so.9.0,libutil.so.12.1,libc.so.78.1} \
# /var/www/usr/libexec/ld.so

# chmod 550 /var/www/usr/bin/perl

# chmod 440 /var/www/usr/lib/{libpthread.so.18.1,libperl.so.17.0,libm.so.9.0,libutil.so.12.1,libc.so.78.1} \
# /var/www/usr/libexec/ld.so

Then be sure to add a section in your /etc/httpd.conf to handle your CGI, here's a simple entry:

location "*.shtml" {
  fastcgi socket "/run/slowcgi.sock"

}

Start slowcgi:

# /usr/sbin/slowcgi

Restart httpd:

# /etc/rc.d/httpd -f restart
httpd(ok)
httpd(ok)

Your server should be serving CGI now (using the Perl included with OpenBSD). Enjoy!

4 comments:

  1. Looks like your text was processed:
    I see
    ```
    # cat < /var/www/htdocs/phpinfo.php
    >
    > EOF
    ```
    though there should be `<<EOF` obviously and some PHP code between `cat` and `EOF`

    ReplyDelete
  2. This article helped me a lot, thank you! How do I enable php_fpm autostart after system restart?

    ReplyDelete
  3. Add `pkg_scripts="php_fpm"` line to `/etc/rc.conf.local`

    ReplyDelete
  4. Thanks for your article, I've used it in the past. If you're interested, I've put up a post on installing httpd/PHP/MariaDB with HTTPS for OpenBSD 6. Cheers.

    ReplyDelete