Pages

2008-01-19

Sysadmin Sunday: OpenBSD 4.2, Apache, MySQL, PHP (OAMP?)


Editor's note: I know, I know. This is SATURDAY! But I'm putting this out there for a good reason. Today is also KC PHP User's Group's first meeting of 2008, and what better way to kick off the KCPUG new year than with some fresh AMP-Stack Content. So, here we go.
Last week, I discussed how OpenBSD sysadmins can make their lives a little easier with some software I wrote. This week on Sysadmin Sunday, I'll show you how easy it is to get a decent AMP (Apache, MySQL and PHP) environment up and running on OpenBSD 4.2, which is the current version as of writing.

It used to be that some stuff had to be compiled by hand to make everything work just right, but the package/port guys finally nailed it. With the help of obsd_pkgscripts, OAMP is a breeze. The benefits of running your AMP-driven website atop OpenBSD are many, not the least of which are the facts that it's a minimalistic, secure, and high-performance platform out of the box.

OpenBSD already ships with Apache. It's a patched and tweaked version of Apache, but it's Apache all the same. You're already halfway to OAMP land with OpenBSD and Apache installed out of the box.

First, install the MySQL Server. I obviously chose to use my pkg_get.sh tool for this, but it's not needed.
$ pkg_get.sh mysql-server
Attempting to fetch/install package...
mysql-client-5.0.45: complete
p5-DBD-mysql-3.0008:p5-Net-Daemon-0.39: complete
p5-DBD-mysql-3.0008:p5-PlRPC-0.2018p0: complete
p5-DBD-mysql-3.0008:p5-DBI-1.53: complete
p5-DBD-mysql-3.0008: complete
mysql-server-5.0.45: complete
--- mysql-server-5.0.45 -------------------
You can find detailed instructions on how to install a database
in /usr/local/share/doc/mysql/README.OpenBSD.

Next, that README contains some useful information for more advanced installations and performance, but summarized, follow these instructions. I use sudo, and rarely use the root account. I recommend doing the same.

Create the DB:
-bash-3.2$ sudo /usr/local/bin/mysql_install_db
Start MySQL:
-bash-3.2$ sudo /usr/local/share/mysql/mysql.server start
To make MySQL Start up at boot, add the above mysql startup line to /etc/rc.local without the "sudo" at the beginning (optional but highly recommended). There is a "theoretically correct" start-up snippit on the documentation, but I've found this works just as well.

Set a password for mysql's root user (choose a better one than this):
-bash-3.2$ sudo /usr/local/bin/mysqladmin -u root password 'new-password'
That's pretty much it as far as getting MySQL up and running is concerned. Now we just need to get PHP installed with the MySQL client library...

-bash-3.2$ sudo pkgscripts/pkg_get.sh php5-mysql
Attempting to fetch/install package...
php5-core-5.2.3: complete
php5-mysql-5.2.3: complete
--- php5-core-5.2.3 -------------------
To finish the install, enable the php5 module with:
/usr/local/sbin/phpxs -s

To enable parsing of PHP scripts, add the following to
/var/www/conf/httpd.conf:

AddType application/x-httpd-php .php

Copy the config file below into /var/www/conf/php.ini
/usr/local/share/examples/php5/php.ini-recommended

Don't forget that the default OpenBSD httpd is chrooted
into /var/www by default, so you may need to create support
directories such as /var/www/tmp for PHP to work correctly.
--- php5-mysql-5.2.3 -------------------
Enable this module in php.ini using the following command:

/usr/local/sbin/phpxs -a mysql
Then you have to follow the post-install instructions:
-bash-3.2$ sudo /usr/local/sbin/phpxs -s
-bash-3.2$ sudo /usr/local/sbin/phpxs -a mysql
Edit /var/www/conf/httpd.conf and add the below line with the other AddTypes:
AddType application/x-httpd-php .php

I also recommend since we're going to be running PHP, adding index.php to the DirectoryIndex line:
DirectoryIndex index.html index.php

Save the httpd.conf file.

Now, copy the php.ini file over from the examples:
-bash-3.2-$ sudo cp \
/usr/local/share/examples/php5/php.ini-recommended \
/var/www/conf/php.ini
Now, make sure apache is set to start.
If you plan on keeping all your web-related content content in /var/www, find the httpd_flags line in /etc/rc.conf and change it to httpd_flags=""

If you intend on giving users traditional ~username (public_html) directories, start apache without being chrooted. Chroot is more secure, but it's a royal pain if you want user home-directories on a vanilla OAMP box, and beyond the "quick and dirty OAMP" scope of this article. Find the httpd_flags line in /etc/rc.conf and change it to httpd_flags="-u" to disable chroot if you must.

The above change to httpd_flags will make apache auto-start upon reboot. You can manually start it with apachectl, though:
-bash-3.2$ sudo apachectl start
/usr/sbin/apachectl start: httpd started
Make a phpinfo example php file and move it into the web root.
-bash-3.2$ echo "<?phpinfo();?>" > phpinfo.php
-bash-3.2$ sudo mv phpinfo.php /var/www/htdocs/
Then test it out by pointing a browser to your new phpinfo.php file (example: http://10.23.45.67/phpinfo.php ) and you should see output from PHP. If you see a blank screen or just the phpinfo line, something went wrong. Re-check your steps.


There you have it! As a test, I installed Joomla on this system (screen shot below). Joomla is a popular open-source CMS. I unpacked it into /var/www/htdocs/joomla and pointed my browser to it, following all of its instructions. This includes making a database, changing permissions on some of the Joomla files, and modifying some values in php.ini. The installation went off without any problems just like it would on any LAMP box. I'd imagine you'll see similar results with other *AMP packages such as phpMyAdmin, Xoops, and others.

9 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Joomla finally got to version 1.5 stable after like 4 years of RC's. W00t.

    ReplyDelete
  3. Just curious, did you ever come across an error similar to this when installing php5-core and running a few modules (i.e. php5-mysql)? It's preventing php from starting, apparently:

    "PHP Warning: PHP Startup: mysql: Unable to initialize module
    Module compiled with module API=20060613, debug=0, thread-safety=0
    PHP compiled with module API=20050922, debug=0, thread-safety=0
    These options need to match
    in Unknown on line 0"

    ReplyDelete
  4. What's your environment (OS and version)?

    Did you install from source, from ports, or from the binary packages?

    What apache modules are enabled, and does it work if you disable one of the modules?

    Answer those and I'll try to see what I can do to help, or maybe post in the HiR Portal mailing list to keep comment clutter down (and to have a friendlier archive of the problem and the solution)

    I haven't ever encountered a problem like this when going from ports or packages, but google shows a lot of people have the problem, and not a lot of help on fixing it. From the look of the error, it would seem that you're building apache modules from two different apache versions perhaps.

    ReplyDelete
  5. OBSD4.2-RELEASE

    Just did a package upgrade (pkg_add -u and I'm sure the PKG_PATH was correct)

    About the Apache modules, that actually solved the problem for me. It turns out that I had installed the previous PHP5 module in an unorthodox path during my prior 4.1 installation. However, '/usr/local/sbin/phpxs -s' appended the new path below the previous one and Apache just loaded the first one it saw (the wrong one) and ignored the other. I commented the old version out as a future reminder to myself and everything's running hunky dory now =)

    Thanks for showing a BSD neophyte the error of his ways ;)

    ReplyDelete
  6. Hey there, a good tutorial. But mysql didn't work for me with a chrooted httpd. httpd couldn't reach the mysql.sock file. And to solve this I just made a hard link in the /var/www/var/run/mysql/mysql.sock directory to the original mysql.sock file. I hope this doesn't bring any security problems.

    ReplyDelete
  7. That's pretty much the preferred way to do it if you want to run apache chrooted.

    ReplyDelete
  8. Yeah, and if you want more information about chroot'ed Apache, the best place to look is in the official FAQ.

    ReplyDelete
  9. i install before i have problems with joomla 1.5 install when on box is php 5.2.3 installed :/

    ReplyDelete