Twitter: making a run to staples to get batteries for my keyboard and a box to return the 2 books [...]

How To: Lighttpd, PHP5 on TextDrive

Jun 02, 2007 in , ,

After hearing about the joys of Lighttpd, a lightweight alternative to the Apache web server, for a long time I opened a Connector account with Joyent/TextDrive. I planned on using this account as a development sandbox where I can tinker with Lighttpd without the possibility of bringing down this blog, which experienced brief downtime last week when I upgraded to PHP5 my clumsy self.

However, I quickly found working with TextDrive complex due to their scattered documentation and use of Webmin. In this post, I’ll be walking you through getting Lighttpd and PHP5 running on a TextDrive/Joyent Connector account.

Add Your Domain

  • Login to your domain name registrar’s website and change the DNS servers for the domain you wish to use your Connector account with to:
    dns1.textdrive.com
    dns2.textdrive.com
    dns3.textdrive.com
    dns4.textdrive.com
  • In Webmin, head to Servers » Virtualmin Virtual Servers
  • Click on the domain name that is currently there, likely in the format of username.textdriven.com
  • In the settings page that opens up, click Change Domain Name on the bottom.
  • Enter the domain name on the next page and change “Change home directory?” to the leave as… option. Save.

Lighttpd and PHP5 Setup

  • File a ticket with TextDrive to receive a port to run lighttpd on.
  • SSH into your account with the OS X terminal or Putty. Alternatively you can run commands within Webmin. For the most part, I’ll be following this TxD knowledge base article. Although it was written with Ruby on Rails users in mind so I’ll do a few things differently.
    ssh username@yourdomain.com
  • Once you’re in with SSH, run these commands:
    cd ~
    mkdir var/
    mkdir var/log
    mkdir var/run
    mkdir etc/rc.d
    mkdir etc/lighttpd
    mkdir etc/lighttpd/vhosts.d
  • Now, FTP into the server and find your way to /users/home/username/etc. Save the following code as lighttpd.conf after you change USERNAME right under the default domain comment to your TextDrive username and replace PORTNUMBER with the port number you received from TxD support. That is the port that Lighttpd will be running on.
    ### Lighttpd Configuration File
    #– Lighttpd modules
    server.modules = ( "mod_rewrite",
    "mod_redirect",
    "mod_access",
    "mod_cgi",
    "mod_fastcgi",
    "mod_compress",
    "mod_accesslog" )
    #– CGI configuration
    cgi.assign = ( ".pl" => "/usr/local/bin/perl",
    ".cgi" => "/usr/local/bin/perl" )
    #– Mimetypes
    include_shell "cat /usr/local/etc/lighttpd_mimetypes.conf"
    #– Default domain
    server.username = "USERNAME"
    server.port = PORTNUMBER
    server.groupname = server.username
    var.base = "/users/home/" + server.username
    server.document-root = base + "/web/public/"
    server.pid-file = base + "/var/run/lighttpd.pid"
    server.tag = "Lighttpd | TextDriven"
    server.indexfiles = ( "index.php", "index.html",
    "index.htm", "default.htm" )
    url.access-deny = ( "~", ".inc", ".ht" )
    #– Logging
    accesslog.filename = base + "/var/log/lighttpd.access.log"
    server.errorlog = base + "/var/log/lighttpd.error.log"
    #– VHOSTS
    include "vhosts.d/APPNAME.conf"
  • Upload that lighttpd.conf file to the lighttpd folder found at etc/lighttpd.
  • Save the following code as APPNAME.conf after you change yourdomain.com in the second line, to the domain you’ll be using.
    ### Rails Application Configuration File
    $HTTP["host"] =~ "(www\.)?yourdomain\.com" {
    server.document-root = base + "/web/public/"
    server.error-handler-404 = "/dispatch.fcgi"
    fastcgi.server = (
    ".fcgi" => ( "localhost" => ( "socket" => base + "/var/run/APPNAME-0.socket" ) )

    ### PHP
    ,".php" =>
    ( "localhost" =>
    (
    "socket" => base + "/var/run/php-fastcgi.socket",
    "bin-path" => "/usr/local/www/cgi-bin/php5-fcgi",
    "bin-environment" =>
    ( "PHP_FCGI_CHILDREN" => "1", "PHP_FCGI_MAX_REQUESTS" => "2500" ),
    "bin-copy-environment" =>
    ( "PATH", "SHELL", "USER" ),
    "max-procs" => 1,
    "idle-timeout" => 20
    )
    )

    )
    }

  • Upload this APPNAME.conf file to etc/lighttpd/vhosts.d
  • Save the following code as lighttpd.sh and upload it to etc/rc.d
    #!/bin/sh
    # This is for Starting/Stopping/Restarting Lighttpd. You won't need
    # to edit anything.

    HOME=/users/home/`whoami`
    LIGHTTPD_CONF=$HOME/etc/lighttpd/lighttpd.conf
    PIDFILE=$HOME/var/run/lighttpd.pid
    export SHELL=/bin/sh

    case "$1" in
    start)
    # Starts the lighttpd deamon
    echo "Starting Lighttpd"
    PATH=$PATH:/usr/local/bin /usr/local/sbin/lighttpd -f $LIGHTTPD_CONF
    ;;
    stop)
    # stops the daemon bt cat'ing the pidfile
    echo "Stopping Lighttpd"
    kill `/bin/cat $PIDFILE`
    ;;
    restart)
    ## Stop the service regardless of whether it was
    ## running or not, start it again.
    echo "Restarting Lighttpd"
    $0 stop
    $0 start
    ;;
    reload)
    # reloads the config file by sending HUP
    echo "Reloading config"
    kill -HUP `/bin/cat $PIDFILE`
    ;;
    *)
    echo "Usage: lighttpdctrl (start|stop|restart|reload)"
    exit 1
    ;;
    esac

Setting up the Proxy

In order for lighttpd to work, you must use a proxy with the port number you were given.

  • Go back to Webmin and go to Servers » Apache Webserver » Aliases and Redirects.
  • Under Map local to remote URLs put a forward slash for Local URL Path, select the radio button for Remote URL and enter in http://127.0.0.1:XXXX/ as the Remote URL. Be sure to change XXXX with the port number you were given. Save.
Lighttpd on TextDrive
  • Go to Servers » Apache Webserver » Proxying.
  • Set Preserve original Host: header to Yes. Save.
  • Click the Apply Changes in the top right area of Webmin.
  • Back in SSH, run the following command to start lighttpd (replace username with your TxD username):
    /users/home/username/etc/rc.d/lighttpd.sh start

Making sure it works

  • In the web directory (username/web/public), upload a file named index.php that contains the following code:
    <?php
    phpinfo();
    ?>
  • With your Firebug-enabled Firefox browser, load up your domain (keep in mind if you just changed your name servers it will take up to 48 hours to propagate). It should correctly process the PHP file and display info about your PHP5 installation.
  • Now to make sure that proxying is working and that Lighttpd is running, open Firebug, click on Net and then select any element in the tab (although there should only be one, the domain). This will let you view the headers, where you should find some indication that the page was served with Lighttpd.
Lighttpd on TextDrive
  • Yay, it works! Should you ever need to stop or restart lighttpd, such as when making changes to lighttpd.conf, just run the appropriate command in SSH (replace username with your TxD username):
    /users/home/username/etc/rc.d/lighttpd.sh start
    /users/home/username/etc/rc.d/lighttpd.sh restart
    /users/home/username/etc/rc.d/lighttpd.sh stop

Files

For your convenience, here are the 3 files from this post: Lighttpd_files.zip.

Why Use Lighttpd?

Disclaimer

I’m not responsible if your server goes boom and the CPU lets out magic smoke. I only wrote this as a guide for others to follow after my own bad experience trying to set it all up. I wrote it after I did it myself so I might have put a step or two in the wrong place. Try it and get back to me.

Promote this article on various sites or email to your friends:     



10 Comments

  1. Awesome detailed guide.

    Thanks!

  2. I’m on Text Drive, too. I haven’t been doing much PHP on it, so tell me, what’s the rationale behind running PHP through Lighttpd? Since mod_php hooks directly into Apache, it’s way faster than Lighttpd running PHP through FastCGI, right? (At least that’s the impression I’ve gotten in the past.) Plus, Text Drive proxies everything through Apache, so if you’re just running PHP why not do it directly from Apache instead of proxying and then fork/exec’ing for FastCGI?

    It has only now occurred to me that you’re probably doing this just to learn how, in which case, more power to you.

  3. Richard, I’ll admit - most what you just said flew right past me. =) Basically, I setup PHP with Lighttpd because without it, PHP wasn’t even running (not even Apache’s) and trying to run a PHP file would just result in a binary download.

  4. Hm that’s really weird. I haven’t taken the plunge into deep understanding the ins and outs of Text Drive’s PHP, but it should just work on the default Apache config.

  5. @Richard, when you enable proxying to get Lighttpd working it routes every request through Lighttpd and I don’t think Apache sees any of them. so that’s a good guess as to why PHP doesn’t use Apache in this case.

  6. Another good reason to run php via fastcgi is to put it in a jail. One of the problems I have had with lighttpd + php is that you can run into all kinds of headaches when you get an app that does complicated url rewritting and a lot of apps want to do that these days.

  7. Hi,

    Everything looks completely right, but something is not working while trying to start lighttpd with lighttpd.sh. First textdrive won’t recognize ‘whoami’. I fixed this by just substituting my username in the lighttpd.sh source. The second problem is that I also get “SHELL=/bin/sh: is not an identifier”. lighttpd.sh is getting hung up at line 8: “export SHELL=/bin/sh”.

    Any ideas.

    Sam

  8. @Sam - is appname.conf setup?

  9. $HTTP["host"] =~ "(www\.)?andresfaceshirt\.com" {
    server.document-root = base "/domains/andresfaceshirt.com/web/public/"
    server.error-handler-404 = "/dispatch.fcgi"
    fastcgi.server = (
    ".fcgi" => ( "localhost" => ( "socket" => base "/var/run/afs_old_rails-0.socket" ) )

    the file is named correctly “afs_old_rails.conf” and put in the ect/lighttpd/vhosts.d/ directory.

  1. [...] How To: Lighttpd, PHP5 on TextDrive - PaulStamatiou.com lighty + php5. Rovid leiras Paultol. (tags: lighttpd php web server tutorial paulstamatiou) [...]

Post a comment, receive Stammy points.


Send a trackback.


  • If you plan on posting code, run it through Postable first.
Copyright © 2005 - 2008 PaulStamatiou.com  Privacy Policy - Terms of Service Can't spell my name? Use PSTAM.com. Go back up ↑.