How To: Lighttpd, PHP5 on TextDrive

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.