Jump to content

Guide to Unix/BSD/OpenBSD/As a Webserver

From Wikibooks, open books for an open world

OpenBSD ships its own webserver, httpd(8). (Despite the similarity of the name, this is not Apache.)

Getting Started

[edit | edit source]

To setup your webserver, you'll first need to configure it. It's a good idea to work from the example configuration provided.

cp /usr/src/etc/examples/httpd.conf /etc/httpd.conf
vi /etc/httpd.conf

You'll probably want to start with a minimal configuration first, and add features as you need them. A sample basic configuration is below:

ext_addr="egress"

server "default" {
listen on $ext_addr port 80
}

If you want to see what features are available, view the man(1) page:

man httpd.conf

Check that your configuration is correct:

httpd -n

Finally, to make your computer serve pages, you'll need to begin the daemon:

httpd

Now, place your html files in /var/www/htdocs, and you will be able to access them with a browser.

Non-html files

[edit | edit source]

If you wish to use files such as images on your webpages, you will have to define by file extension each file's w:Mime type in the httpd.conf. For example, if you wanted to serve Javascript, text, HTML and SVG files, you might add a section like this to your httpd.conf:

types {
        image/svg+xml                   svg;
        application/javascript          js;
        text/txt                        txt;
        text/html                       htm html;
}

Other extensions mentioned as useful in the users manual. (RUN: man httpd.conf [92%])

types {
        image/gif                       gif;
        image/jpeg                      jpeg jpg;
        image/png                       png;
        text/css                        css;
        application/xml                 xml;
}


Wikipedia's article on mime types has a long list of them. If not listed above, look for whatever file type you want to serve on there.

For server-side scripting (using languages such as Perl or PHP), you'll need to add some lines to your httpd.conf. A sample configuration is below:

ext_addr="egress"

server "default" {
        listen on $ext_addr port 80

        location "/cgi-bin/*" {
                fastcgi
                root "/"
        }
}

This alone is not enough, however. OpenBSD puts its webserver in a chroot(8) - that is, in the eyes of the webserver, the root directory is /var/www. The webserver is unable to see any other files on your computer. This is for security reasons - a cracker would have fewer resources should they gain access to your system. On the other hand, it means that the languages that you would use for server-side scripting are not present. To maintain this level of security, you should only copy over files that you need.

Also, note that fastcgi may not be enough for what you need. If things aren't working, you'll need to run slowcgi(8):

slowcgi

To run (with the above configuration), you will need to place your cgi scripts in /var/www/cgi-bin.

Copy the main perl binary and required libraries:

mkdir -p /var/www/usr/bin
mkdir -p /var/www/usr/lib
mkdir -p /var/www/usr/libexec
cp /usr/bin/perl /var/www/usr/bin/perl
cp /usr/lib/libc.so.77.0 /var/www/usr/lib/libc.so.77.0
cp /usr/lib/libm.so.9.0 /var/www/usr/lib/libm.so.9.0
cp /usr/lib/libperl.so.15.0 /var/www/usr/lib/libperl.so.15.0
cp /usr/lib/libpthread.so.18.0 /var/www/usr/lib/libpthread.so.18.0
cp /usr/lib/libutil.so.12.1 /var/www/usr/lib/libutil.so.12.1
cp /usr/libexec/ld.so /var/www/usr/libexec/ld.so

Test your script chrooted in /var/www and as the www user to check for any missing modules or other errors.

chroot -u www /var/www <command>

Authors

[edit | edit source]

If you made a contribution to this article please feel free to add your username.