Lighttpd Webserver setup with php5 and Mysql support

Sponsored Link
Security, speed, compliance, and flexibility --- all of these describe lighttpd (pron. lighty) which is rapidly redefining efficiency of a webserver; as it is designed and optimized for high performance environments. With a small memory footprint compared to other web-servers, effective management of the cpu-load, and advanced feature set (FastCGI, SCGI, Auth, Output-Compression, URL-Rewriting and many more) lighttpd is the perfect solution for every server that is suffering load problems. And best of all it's Open Source licensed under the revised BSD license.

Installing Lighttpd in Ubuntu

Lighttpd is available as a Ubuntu package, therefore we can install it like this

sudo apt-get install lighttpd

Now direct your browser to http://serverip and you should see the Lighttpd placeholder page

Lighttpd's default document root is /var/www on Ubuntu, and the configuration file is /etc/lighttpd/lighttpd.conf.

Installing PHP5 in Ubuntu

We can make PHP5 work in Lighttpd through FastCGI. Fortunately, Ubuntu provides a FastCGI-enabled PHP5 package which we install like this:

sudo apt-get install php5-cgi

Configuring Lighttpd And PHP5

To enable PHP5 in Lighttpd, we must modify two files, /etc/php5/cgi/php.ini and /etc/lighttpd/lighttpd.conf. First we open /etc/php5/cgi/php.ini and add the line cgi.fix_pathinfo = 1 right at the end of the file:

sudo vi /etc/php5/cgi/php.ini

cgi.fix_pathinfo = 1

Then we open /etc/lighttpd/lighttpd.conf and add "mod_fastcgi", to the server.modules stanza

sudo vi /etc/lighttpd/lighttpd.conf

server.modules = (
# "mod_rewrite",
# "mod_redirect",
# "mod_status",
# "mod_evhost",
# "mod_compress",
# "mod_usertrack",
# "mod_rrdtool",
# "mod_webdav",
# "mod_expire",
# "mod_flv_streaming",
# "mod_evasive"

and then right at the end of the file, we add the following stanza:

fastcgi.server = ( ".php" => ((
"bin-path" => "/usr/bin/php5-cgi",
"socket" => "/tmp/php.socket"

Installing MySQL 5.0

First we install MySQL 5.0 like this:

sudo apt-get install mysql-server mysql-client

Create a password for the MySQL user root (replace yourrootsqlpassword with the password you want to use):

sudo mysqladmin -u root password yourrootsqlpassword

Then check with

netstat -tap | grep mysql

on which addresses MySQL is listening. If the output looks like this:

tcp 0 0 localhost.localdo:mysql *:* LISTEN 2713/mysqld

which means MySQL is listening on localhost.localdomain only, then you're safe with the password you set before. But if the output looks like this:

tcp 0 0 *:mysql *:* LISTEN 2713/mysqld

you should set a MySQL password for your hostname, too, because otherwise anybody can access your database and modify data:

sudo mysqladmin -h serverip -u root password yourrootsqlpassword

Testing PHP5 in Lighttpd

The document root of the default web site is /var/www. We will now create a small PHP file (info.php) in that directory and call it in a browser. The file will display lots of useful details about our PHP installation, such as the installed PHP version.

vi /var/www/test.php


Adding MySQL Support In PHP5

To get MySQL support in PHP, we can install the php5-mysql package. It's a good idea to install some other PHP5 modules as well as you might need them for your applications. You can search for available PHP5 modules like this:

sudo apt-cache search php5

Pick the ones you need and install them like this:

sudo apt-get install php5-mysql php5-curl php5-gd php5-idn php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-mhash php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl php5-common

You might see a question like this one:

Continue installing libc-client without Maildir support? <-- Yes Now restart Lighttpd: sudo /etc/init.d/lighttpd restart (If you've installed the module php5-common and get warnings like this one: PHP Warning: Module 'json' already loaded in Unknown on line 0 it means that the module got loaded twice. Open /etc/php5/cgi/php.ini, scroll down to the end and comment out the line sudo vi /etc/php5/cgi/php.ini ; Then restart Lighttpd again: sudo /etc/init.d/lighttpd restart The warnings should now be gone. Now reload http://serverip/test.php in your browser and scroll down to the modules section again. You should now find lots of new modules there, including the MySQL module

Sponsored Link

You may also like...

24 Responses

  1. Declan says:


    Your tutorial helped my setup my new Dell server, I am experienced with Linux and the software mentioned above but I wanted to move away from Apache as it was being too resource heavy and lighttpd offered just this, it was a bit of a pain porting the stuff over but I am glad I did. Runs much better! 😀

  2. qix says:

    Thanks, finally I got that php thing to work with lighttpd.

  3. Damien says:

    Thanks mate, just got phpmyadmin working with it as well and all is good.

    Great tutorials here.

  4. F4jr says:

    How to install version 1.4.19 using apt-get ?

  5. Morgan says:

    Thanks so much for this!

  6. Steve says:

    Got up to modifying the lighttpd-conf but trying to reload the server i get:

    158 pos: 19 invalid character in variable name
    2008-09-27 20:31:27: (configfile.c.852) configfile parser failed at: (

    its with your stanza at the end of the file! if I leave it out i cannot parse php files I get “No input file specified”. I googled this but IM on my third day of trying to fix it, I think its a bug

  7. gimi says:

    Please note that lighttpd expects “normal” ascii quotes unlike “theese” used in the article.

    And instead of appending the fastcgi stuff /etc/lighttpd/lighttpd.conf, I’d try:

    cd /etc/lighttpd/conf-enabled
    ln -s ../conf-available/10-fastcgi.conf
    # In this file, change “php-cgi” to “php5-cgi”.

    But great article. Provided me with a guideline to set up a LLMP setup within minutes. =)

  8. Isn’t just easier to use XAMPP For Linux?. If I’m not mistaking, XAMPP sets up apache, php, mysql, phpmyadmin and something else too that I do not use 😛

  9. helloworld says:

    you can do

    # lighty-enable-mod

    and choose fastcgi instead of editing lighttpd.conf.

    editing /etc/php5/cgi/php.ini is also not required anymore.

  10. Erik says:

    hi, thanks,

    a great howto. Except the ” marks should be straight ones and not fancy. And I too am having problems with the sequence;

    fastcgi.server = ( “.php” => ((
    “bin-path” => “/usr/bin/php5-cgi”,
    “socket” => “/tmp/php.socket”

    I tried leaving it out, and at least it loads lighty.

    Still, great help!

  11. Mark says:

    Great article, very easy to understand. Only problem I had was installing MySQL, using apt-get would get me a corrupted version and when I was building from source it’d give me an error on “make”….turned out it was because I didn’t have g++ installed.

  12. messala says:

    God bless u!

  13. Alex says:

    why use apache when you got such a thing as lighttpd?
    Great tut, THANKS!

  14. LB says:

    Thank you for a great tutorial:-) Havent got around to PHP and MySql yet, but the placeholder-page came up on the right IP immediately. For at server newbie like myself, that was a great experience.

  15. Sten says:

    Thanks! Exactly what I needed.

  16. skaramanger says:

    I read your tutorial and the index and test pages worked flawlessly. However, My intent is to just view a couple of cgi pages locally and I am getting 404 pages. I am new to webservers. I get an error message when I try to enable fastcgi module:
    Duplicate config variable in conditional 0 global: fastcgi.server
    2010-01-31 17:17:30: (configfile.c.864) source: /etc/lighttpd/lighttpd.conf line: 172 pos: 1 parser failed somehow near here: (EOL)

    Line 172 is at the end of the file. I have tried a combination of en/disabling fastcgi and cgi modules. There is apparently something a miss here. Hope you have some suggestions for me.


  17. Eddy says:

    Merci beaucoup pour ce tutorial 🙂

  18. Sean says:

    Thanks for demystifying this process for me!

    Installing on Ubuntu Server 10.04 I encountered an issue with regard to the cgi module not being installed, whereby attempts to load the test.php file returned a 403 error.

    Once I had loaded the cgi module with ‘lighty-enable-mod’ the errors disappeared.

  19. Robbie says:


    In trying to get all my different things straightened out in Ubuntu, I have sifted through pages and pages of tutorials. This is the first one that I have seen in all those wasted hours that solves my problem perfectly! I followed these steps exactly and the result was happiness! Thank you so much!!

  20. Kimse says:

    Here is an easier way:

    sudo apt-get install lighttpd php5-cgi
    sudo lighty-enable-mod fastcgi
    sudo /etc/init.d/lighttpd force-reload

  21. Kimse says:

    sudo lighty-enable-mod fastcgi-php

  22. Jesse Donat says:

    Great post! Got me running in minutes!

    Two notes
    – Some of your examples have directional quotes, and I had to replace them with standard quotes after I copied and pasted.
    – I had to restart lightppd after making the changes

  23. WolBot says:

    Merci 😀

  24. Nerd says:

    Thanks!!! I needed package php5-mysql

Leave a Reply

Your email address will not be published. Required fields are marked *