**Contents** [[!toc levels=4]] # How to run a webserver on NetBSD First of all, logging as `root` is required thus you can install packages and edit system configuration files. If you want to install packages to NetBSD, you can do this in several different ways, using pkgsrc, pkgin or pkg\_add. Please see the [[chapter about installing additional software|guide/boot]] in the guide. There are several HTTP daemons. The following text will introduce [bozotic HTTP server](http://www.eterna.com.au/bozohttpd/) and [Apache HTTP server](http://httpd.apache.org/). ## bozotic HTTP server `bozohttpd` is a small and secure HTTP 1.1 server shipped with NetBSD (`/usr/libexec/httpd`) by default. It's very simple and there isn't even a configuration file. But it only provides the most basic features. ### Register the daemon #### Run with inetd `bozohttpd` can be run with `inetd`, and you need to register it to `inetd`. Daemons hosted by `inetd` is registered at `/etc/inetd.conf`. `inetd.conf` is an ascii file containing one service per line, and several fields per line. The basic layout is: service-name socket-type protocol wait/nowait user:group server-program arguments In this case(handling HTTP requests), the basic format is: http stream tcp nowait:600 nobody FULL_PATH_OF_DAEMON DAEMON_NAME_AND_ARGUMENTS Append this to `/etc/inetd.conf` (assuming that your files are located in `/var/www`): http stream tcp nowait:600 nobody /usr/libexec/httpd httpd /var/www Telling `inetd` to reload the file makes `httpd` work immediately. You may run the following command: # /etc/rc.d/inetd reload Reloading inetd config files. Here are the frequently used options: * `-X`: Enables directory indexing. * `-c CGI_BIN_PATH`: Enables the CGI/1.1 interface. To get more information about options, see `bozohttpd(8)`. #### Run in standalone mode `bozohttpd` can be run without `inetd`. You have to add `-b` to the parameters to enable daemon mode. /usr/libexec/httpd -b /var/www Because listening ports under 1024 requires root privilege, and running a webserver with root privilege is unsafe, you may want to change the user to `nobody` after initializing sockets. The command argument for this is `-U USERNAME`. Now the command line looks like this: /usr/libexec/httpd -b -U nobody /var/www To make `bozohttpd` run when the system boots, you have to activate it in your rc.local(5) by setting `httpd=YES`. Arguments you want to pass can be set with the `httpd_flags` variable, e.g. acitvating bozohttpd with directory indexing would result in: httpd=YES httpd_flags="-X" ### Examples * Use PHP parser(`/usr/pkg/bin/php`) to parse `.php` files in `/var/www`: `httpd -C .php /usr/pkg/bin/php /var/www` (you need php installed) * Enable directory listing to share files via HTTP: `httpd -x PATH_TO_SHARE` * Run CGI programs(`/var/cgi`): `httpd -c /var/cgi /var/www` ## Apache HTTP server The [Apache HTTP server](http://httpd.apache.org/) is a widely-used open source HTTP server. Apache is a powerful HTTP server, which can be extended by loading dynamic shared object (DSO). ### Install You have to install Apache, it is not shipped with NetBSD. You can do this by executing `pkgin install apache`, `cd /usr/pkgsrc/www/apache; make install` or by using pkg\_add: # pkg_add apache-2.4.3 pkg_add: Warning: package `apache-2.4.3' was built for a platform: pkg_add: NetBSD/i386 6.0 (pkg) vs. NetBSD/i386 6.0.1 (this host) apache-2.4.3: copying /usr/pkg/share/examples/httpd/extra/httpd-autoindex.conf to /usr/pkg/etc/httpd/httpd-autoindex.conf apache-2.4.3: copying /usr/pkg/share/examples/httpd/extra/httpd-dav.conf to /usr/pkg/etc/httpd/httpd-dav.conf apache-2.4.3: copying /usr/pkg/share/examples/httpd/extra/httpd-default.conf to /usr/pkg/etc/httpd/httpd-default.conf apache-2.4.3: copying /usr/pkg/share/examples/httpd/extra/httpd-info.conf to /usr/pkg/etc/httpd/httpd-info.conf apache-2.4.3: copying /usr/pkg/share/examples/httpd/extra/httpd-languages.conf to /usr/pkg/etc/httpd/httpd-languages.conf apache-2.4.3: copying /usr/pkg/share/examples/httpd/extra/httpd-manual.conf to /usr/pkg/etc/httpd/httpd-manual.conf apache-2.4.3: copying /usr/pkg/share/examples/httpd/extra/httpd-mpm.conf to /usr/pkg/etc/httpd/httpd-mpm.conf apache-2.4.3: copying /usr/pkg/share/examples/httpd/extra/httpd-multilang-errordoc.conf to /usr/pkg/etc/httpd/httpd-multilang-errordoc.conf apache-2.4.3: copying /usr/pkg/share/examples/httpd/extra/httpd-ssl.conf to /usr/pkg/etc/httpd/httpd-ssl.conf apache-2.4.3: copying /usr/pkg/share/examples/httpd/extra/httpd-userdir.conf to /usr/pkg/etc/httpd/httpd-userdir.conf apache-2.4.3: copying /usr/pkg/share/examples/httpd/extra/httpd-vhosts.conf to /usr/pkg/etc/httpd/httpd-vhosts.conf apache-2.4.3: copying /usr/pkg/share/examples/httpd/httpd.conf to /usr/pkg/etc/httpd/httpd.conf apache-2.4.3: copying /usr/pkg/share/examples/httpd/magic to /usr/pkg/etc/httpd/magic apache-2.4.3: copying /usr/pkg/share/examples/httpd/mime.types to /usr/pkg/etc/httpd/mime.types =========================================================================== The following files should be created for apache-2.4.3: /etc/rc.d/apache (m=0755) [/usr/pkg/share/examples/rc.d/apache] =========================================================================== =========================================================================== $NetBSD: how_to_setup_a_webserver.mdwn,v 1.2 2013/03/14 23:44:47 jdf Exp $ After apache-2.4.3, --enable-mpms-shared='event prefork worker' is passed to configure script, then three multi-process model is built and you can select the model in configuraton file. The mod_cgi.so module conflicts with non-prefork multi-process model, and mod_cgi.so module is not built anymore. You can use mod_cgid.so module instead. =========================================================================== ### Register the daemon The main tool to control the service of Apache is `apachectl`. To make it work like normal services, supporting commands such as `/etc/rc.d/apache stop`, you need to copy the script provided by Apache to `/etc/rc.d/apache`: # cp /usr/pkg/share/examples/rc.d/apache /etc/rc.d To make Apache run when system boots, you may set the rc variable `apache` to `yes`. This can be done by creating `/etc/rc.conf.d/apache` and insert the following text to the file: apache=yes or writing this line into your rc.conf(5) directly. You can also run this to start Apache immediately: # /etc/rc.d/apache start Starting apache. or: # apachectl start To see whether the server is started successfully, you can visit `http://127.0.0.1`. The dafault page is: