Lighttpd Webserver setup with php5 and Mysql support
Posted by admin on March 7th, 2008
Email This Post
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_access”,
“mod_alias”,
“mod_accesslog”,
“mod_fastcgi”,
# “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
<?php
phpinfo();
?>

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 extension=json.so:
sudo vi /etc/php5/cgi/php.ini
;extension=json.so
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

If you want to be notified the next time we write something please subscribe to our RSS feed.Thanks for Visiting!


April 30th, 2008 at 2:02 am
Thanks!
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!
May 12th, 2008 at 9:25 pm
Thanks, finally I got that php thing to work with lighttpd.
June 19th, 2008 at 5:36 am
Thanks mate, just got phpmyadmin working with it as well and all is good.
Great tutorials here.
August 13th, 2008 at 4:33 pm
How to install version 1.4.19 using apt-get ?
September 19th, 2008 at 8:08 pm
Thanks so much for this!
September 27th, 2008 at 6:34 pm
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
September 30th, 2008 at 1:35 pm
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. =)