Linux Inspirations

Installing Lighttpd With PHP5 And MySQL Support On CentOS 5.4

Posted on: 26 Januari, 2010

LighttpdLighttpd is a secure, fast, standards-compliant web server designed for speed-critical environments. This tutorial shows how you can install Lighttpd on a CentOS 5.4 server with PHP5 support (through FastCGI) and MySQL support.

I do not issue any guarantee that this will work for you!

1 Preliminary Note

In this tutorial I use the hostname server1.example.com with the IP address 192.168.0.100. These settings might differ for you, so you have to replace them where appropriate.

2 Installing MySQL 5.0

First we install MySQL 5.0 like this:

yum install mysql mysql-server

Then we create the system startup links for MySQL (so that MySQL starts automatically whenever the system boots) and start the MySQL server:

chkconfig –levels 235 mysqld on
/etc/init.d/mysqld start

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

mysqladmin -u root password yourrootsqlpassword
mysqladmin -h server1.example.com -u root password yourrootsqlpassword

3 Installing Lighttpd

Lighttpd is not available from the official CentOS 5.4 repositories, but from the RPMforge repositories (see http://dag.wieers.com/rpm/FAQ.php#B2 for instructions). We install the RPMforge package for RHEL 5 which works for CentOS 5.4 as well:

If you are on an x86_64 system:

wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.3.6-1.el5.rf.x86_64.rpm
rpm -Uvh rpmforge-release-0.3.6-1.el5.rf.x86_64.rpm

If you are on an i386 system:

wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.3.6-1.el5.rf.i386.rpm
rpm -Uvh rpmforge-release-0.3.6-1.el5.rf.i386.rpm

Afterwards, we can install Lighttpd like this:

yum install lighttpd

Then we create the system startup links for Lighttpd (so that Lighttpd starts automatically whenever the system boots) and start it:

chkconfig –levels 235 lighttpd on
/etc/init.d/lighttpd start

Now direct your browser to http://192.168.0.100, and you should see the following page:

Click to enlarge

(JavaScript must be enabled in your browser to view the large image as an image overlay.)

Lighttpd’s default document root is /srv/www/lighttpd on CentOS 5.4 (there’s no index file in that directory like index.html, that’s why you get a 404 Not Found error), and the configuration file is /etc/lighttpd/lighttpd.conf.

4 Installing PHP5

We can make PHP5 work in Lighttpd through FastCGI. Therefore we install the packages lighttpd-fastcgi and php-cli:

yum install lighttpd-fastcgi php-cli

5 Configuring Lighttpd And PHP5

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

vi /etc/php.ini

[...]
cgi.fix_pathinfo = 1

Then we open /etc/lighttpd/lighttpd.conf and uncomment “mod_fastcgi”, in the server.modules stanza:

vi /etc/lighttpd/lighttpd.conf

[...]
server.modules              = (
#                               "mod_rewrite",
#                               "mod_redirect",
#                               "mod_alias",
                                "mod_access",
#                               "mod_cml",
#                               "mod_trigger_b4_dl",
#                               "mod_auth",
#                               "mod_status",
#                               "mod_setenv",
                                "mod_fastcgi",
#                               "mod_proxy",
#                               "mod_simple_vhost",
#                               "mod_evhost",
#                               "mod_userdir",
#                               "mod_cgi",
#                               "mod_compress",
#                               "mod_ssi",
#                               "mod_usertrack",
#                               "mod_expire",
#                               "mod_secdownload",
#                               "mod_rrdtool",
                                "mod_accesslog" )
[...]

and then , further down the file, there’s a fastcgi.server stanza which we uncomment as well – make sure you use /tmp/php-fastcgi.socket in the “socket” line::

[...]
#### fastcgi module
## read fastcgi.txt for more info
## for PHP don't forget to set cgi.fix_pathinfo = 1 in the php.ini
fastcgi.server             = ( ".php" =>
                               ( "localhost" =>
                                 (
                                   "socket" => "/tmp/php-fastcgi.socket",
                                   "bin-path" => "/usr/bin/php-cgi"
                                 )
                               )
                            )
[...]

Then we restart Lighttpd:

/etc/init.d/lighttpd restart

 6 Testing PHP5 / Getting Details About Your PHP5 Installation

The document root of the default web site is /srv/www/lighttpd. 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 /srv/www/lighttpd/info.php

<?php
phpinfo();
?>

Now we call that file in a browser (e.g. http://192.168.0.100/info.php):

Click to enlarge

(JavaScript must be enabled in your browser to view the large image as an image overlay.)

As you see, PHP5 is working, and it’s working through FastCGI, as shown in the Server API line. If you scroll further down, you will see all modules that are already enabled in PHP5. MySQL is not listed there which means we don’t have MySQL support in PHP5 yet.

7 Getting MySQL Support In PHP5

To get MySQL support in PHP, we can install the php-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:

yum search php

Pick the ones you need and install them like this:

yum install php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc

Now restart Lighttpd:

/etc/init.d/lighttpd restart

Now reload http://192.168.0.100/info.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:

Mod Alias

Configuration

Open your lighttpd configuration file:
vi /etc/lighttpd/lighttpd.conf

Append/add mod_ alias to list of server modules:
server.modules += ( "mod_alias" )

Examples

Add cgi-bin alias for doamin theos.in
alias.url = ( "/cgi-bin/" => "/home/lighttpd/theos.in/cgi-bin/" )

Browse all documents installed at /usr/share/doc/ directory with following alias:
alias.url = ( "/docs/" => "/usr/share/doc/" )
alias.url += ( "/stats/" => "/home/theos.in/http/webalizer/" )

Open a browser and type url http://theos.in/docs/ or http://your-domain.com/docs/

Lighttpd Virtual Host (vhost)

Virtual hosting allows sharing a lighttpd web server so that you can have multiple domains hosted on single web server. For example:
=> http://www.nixcraft.com
=> http://www.cyberciti.biz
=> http://www.theos.in etc

Our setup

Lighttpd supports both Name-based and IP-based virtual hosts. Let us see how to configure two web sites for:
=> http://www.nixcraft.com
=> http://www.theos.in

First create a directory for each domain:

# mkdir -p /home/lighttpd/nixcraft.com/http
# mkdir -p /home/lighttpd/theos.in/http
# chown lighttpd:ftpuser1 /home/lighttpd/nixcraft.com/http
# chown lighttpd:ftpuser2 /home/lighttpd/theos.in/http

Replace ftpuser1 and 2 with actual ftp username.

Also create a log directory for each domain:
# mkdir /var/log/lighttpd/nixcraft.com
# mkdir /var/log/lighttpd/theos.in

Only allow a web server to access our logs:
# chown -R lighttpd:lighttpd /var/log/lighttpd

Open lighttpd configuration file:
# vi /etc/lighttpd/lighttpd.conf

Add support for domain nixcraft.com:
$HTTP["host"] =~ "(^|\.)nixcraft\.com$" {
server.document-root = "/home/lighttpd/nixcraft.com/http"
server.errorlog = "/var/log/lighttpd/nixcraft/error.log"
accesslog.filename = "/var/log/lighttpd/nixcraft/access.log"
server.error-handler-404 = "/e404.php"
}

Add support for domain theos.in:
$HTTP["host"] =~ "(^|\.)theos\.in$" {
server.document-root = "/home/lighttpd/theos.in/http"
server.errorlog = "/var/log/lighttpd/theos.in/error.log"
accesslog.filename = "/var/log/lighttpd/theos.in/access.log"
server.error-handler-404 = "/e404.php"
}

Where,

    • $HTTP[“host”] =~ “(^|\.)theos\.in$” : It will match request for both http://www.theos.in and theos.in domain
    • server.document-root = “/home/lighttpd/theos.in/http” : Server document root. You must set ftp/ssh user home directory to this root only
    • server.errorlog = “/var/log/lighttpd/theos.in/error.log” : Server access log file
    • accesslog.filename = “/var/log/lighttpd/theos.in/access.log”: Server error log file
    • server.error-handler-404 = “/e404.php” : Web server error 404 handler file

Save and close the file. Restart the server:
# /etc/init.d/lighttpd restart

Upload file(s) to respective web-root and test your configuration with a web browser.

Pustaka :

http://howtoforge.com/installing-lighttpd-with-php5-and-mysql-support-on-centos-5.4

http://www.cyberciti.biz/tips/configure-lighttpd-alias-mod_alias.html

http://www.cyberciti.biz/tips/howto-lighttpd-web-server-setting-up-virtual-hosting.html

 

 

Tinggalkan Balasan

Please log in using one of these methods to post your comment:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

ABout Me!


WordPress ini sengaja saya buat untuk menulis apa yang telah saya lakukan agar saya selalu ingat, tulisan yang saya buat kebanyakan dalam bentuk tutorials configurasi linux dari hasil uji coba saya dan sebahagian di kutip dari beberapa website lain yang saya anggap perlu untuk saya tulis disini. Mudah-mudahan tutorial yang saya buat ini berguna buat pencinta linux yang lain. Salam Sejahtera.

Blog Stats

  • 120,467 hits

Linux Inspiration

%d blogger menyukai ini: