Wednesday, September 02, 2015

5.8-S amd64 X200: httpd Virtual Host.

Hi,

I've been having headache trying to solve the Virtual Hosting issue when using OpenBSD's new httpd (starting from 5.7) included in the base. After fairly quite a lot a trial and errors (mostly errors) and nearly downloading nginx today, I finally got the Virtual Host working now.

I really like the concept of OpenBSD's new httpd. Simple configuration, the devs are keeping the code simple and it's in the base. I also got Drupal 7 working fine (except for clean url, which is minor as this is for developing, not production) and I'm having fun learning Drupal here. But so far, I only managed to get 1 site working at a time. That means, I need to reconfigure my /etc/httpd.conf every time I want to change the website I want to work on. If I only want to work on a single site, this would not pose a problem. But I have more than 1 website I want to work on, so this is tiring me down.

I know that the httpd is chrooted by default, but I didn't know how to configure it properly until now. Well "properly" is not as "perfect" but more to "got it to work" kind of saying. So please correct me if you find anything that's wrong here. So here it goes.

My scenario, I'm working in 1 Drupal 7 site, then 1 another html/php site. The directory is as below:

/var/www/htdocs (the /var/www is the chroot directory)
/var/www/htdocs/drupal7 (my drupal dev directory, the default as installed)
/var/www/htdocs/karl (my html/php dev site)

For Drupal 7 installations and configurations, you can check my previous post. Now here's my /etc/httpd.conf.

---- /etc/httpd.conf start ----

# Macros
ext_addr="*"

# Server
server "default" {
    listen on $ext_addr port 80

    directory {
        auto index
    }

    location "*.php*" {
        fastcgi socket "/run/php-fpm.sock"
    }

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

}

---- /etc/httpd.conf end ----

Now that's for the default site. I noticed that if I change the upper part to:

server "www.example.com" {
    listen on $ext_addr port 80
    alias "example.com"


Then if I just type "example.com" in my browser, it will display my local site's main page instead of the internet's site. Take note that it's easier to use "Private Window" (Firefox) or "Incognito Window" (Chrome) for testing purpose. I also set the index to be created automatically because I don't want to create any site inside the root "/htdocs" directory and this is just a local development machine. Now for the Virtual Host setting inside /etc/httpd.conf.

---- /etc/httpd.conf continues ----

# virtual server for Drupal
server "drupal.example.com" {
    listen on $ext_addr port 80

    directory {
        index "index.php"
    }

    location "*.php*" {
        fastcgi socket "/run/php-fpm.sock"
    }

    root "/htdocs/drupal7"

}

# virtual server for Karl
server "karl.example.com" {
    listen on $ext_addr port 80

    location "*.php*" {
        fastcgi socket "/run/php-fpm.sock"
    }

    root "/htdocs/karl"

}

types {
    includes "/usr/share/misc/mime.types"
}


---- /etc/httpd.conf ends ----

Now this is the simplest working form of my /etc/httpd.conf. On httpd side, this should be ok. Now for the important part, making the Virtual Host part work. As httpd works in chroot by default, it will only access files inside /var/www for safety reason. So I need to create an etc directory inside /var/www.

# mkdir /var/www/etc

Then I need to copy /etc/hosts and /etc/resolv.conf into /var/www/etcb.

# cp /etc/hosts /var/www/etc
# cp /etc/resolv.conf /var/www/etc

This is the content of both files:

---- /var/www/etc/hosts start ----

127.0.0.1    localhost    www.example.com
::1    localhost    www.example.com

---- /var/www/etc/hosts end ----

---- /var/www/etc/resolv.conf start ----

# Generated by em0 dhclient
search Home
nameserver 127.0.0.1
lookup file bind

---- /var/www/etc/resolv.conf end ----

Ok. I was thinking that to make Virtual Host using httpd works, I need to edit that /var/www/etc/hosts file, as that is the chroot directory. I was wrong. I didn't manage to get the Virtual Host working by editing that file. It was the /etc/hosts file that needs editing. Here's mine.

---- /etc/hosts start ----

127.0.0.1    localhost    www.example.com
127.0.0.1    drupal.example.com
127.0.0.1    karl.example.com
::1    localhost    www.example.com

---- /etc/hosts end ----

I only use 2 Virtual Hosts as of now, but I'm guessing that more can be set up with appropriate configuration changes. So now I restarted the httpd.

# sudo /etc/rc.d/httpd restart

Open up my browser, typed all 3 urls:

www.example.com
drupal.example.com
karl.example.com

and all open up the appropriate pages. I'm loving this! Now this leave only 1 more minor thing to be done, the Clean URL. But like I've said, it's only a minor thing. Later.

4 comments:

Unknown said...

I'd be real interested if you can solve the Drupal 7 and Clean URLs problem on OBSD 5.7+. I have been hosting on OpenBSD for a decade, including Drupal 6 & 7 for over half that time. (Only mentioned to illustrate I understand both Drupal and OpenBSD pretty well)

I originally started, with the base httpd (1.3 branch) then migrated to about 4 years ago to Apache2 and customizing the environment. I skipped over the NGINX fiasco, by staying on Apache2. Now, I am keenly interested in the new native httpd, but my research and testing efforts for Clean URL's has thus far been stymied.

The problem: I believe it is because there is no direct replacement for mod_rewrite, which Clean URL's requires.

So far for me anyway, that has been a tough nut to crack.

Karl said...

Hi Larry,

Yeah you are right, there's no mod_rewrite capability yet in OpenHTTPd. The devs are keeping the code simple and only providing the most common stuff but hey, mod_rewrite are common nowadays too. For me, a newbie, I'm not too concern with Clean URL because this is just a local development setup but I can see the importance. If you can crack this nut, I'll be glad to read it.

Customized ERP said...

Our expert team has been working with Dynamics odoo erp customization for years and is recognized as a leader in the digital world. We already have support team to monitor our clients’ Dynamics instances pro actively, providing insight into potential issues before they arise, feel free please contact us : sales@bassaminfotech.com

Unknown said...

Nice article and very helpful for bigner thank you guy.

6.5 amd64: Modify existing certbot certificates.

Hi, It's been quite some time eh. As you can see, I still upgrade my OpenBSD system regularly but currently I do not have the time to ...