How To Build Working CentOS Server with LightTPD and PHP5 (Part 4)
| |This article is the next part for my tutorial about how to build CentOS server with Lighttpd as its webserver. In previous articles we learned already about how to install Lighttpd, PHP5, MySQL and how to setup Lighttpd to work with PHP5 with FPM. Today in this page you’ll see how to get MySQL support in PHP5. Read on..
In my previous article about how to install PHP5 you can see there’s no MySQL module within the PHP. It means you have to firstly install necessary modules to get MySQL works together with PHP.
How to Get MySQL Support In PHP5
Step 0 – I assumed you’ve followed all the steps in the Part 1. So if at some point you feel confused, go back to read the part 1, 2 and 3.
Step 1 – Issue following command to install necessary PHP5 modules to make MySQL support:
yum install php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc
It asks you for a confirmation, simply type Y then hit Enter.
Step 2 – Once you see the Complete! message now reload PHP using this command syntax:
/etc/init.d/php-fpm reload
You’ll then see the OK message with no issue.
Step 3 – Now confirm this by visiting your info.php page again in your browser. This time you should see the MySQL module listed there along with other detailed information:
That’s it.
How to Install Other PHP Modules
There are several others PHP modules available for install but make sure you’ll only install what you really need it.
Step 1 – Check all available modules using this command:
yum search php
You’ll then see very long list like below..
Step 2 – Now find what module you wish to install then use this command to install it:
yum install php-modulename1 php-modulename2 php-modulename3
Replace php-modulename1 with the name of the module. The working example is the one in tutorial above (installing PHP module for MySQL) or this one below..
How to Install APC (Alternative PHP Cache)
APC is a free and open PHP opcode cacher for caching and optimizing PHP intermediate code. It is a free module you can install in your server to optimize how PHP behave by caching some frequently executed codes. This module uses the APC user cache as a cache backend for various PHP-based scripts and CMSs like WordPress and Drupal. PHP must be parsed, compiled and executed everytime a script is called. APC will store the compiled code for execution so there is not a need to parse and compile the code over again. Basically there are several other accelerators (see the list) but APC is the free one that is also very recommended. The best part, APC gives you the ability to not only cache parsed PHP scripts for optimized loading but the ability to cache data as well. Here’s how to install it:
Step 1 – Issue this command:
yum install php-pecl-apc
As usual, your server will ask for confirmation, type Y then hit Enter.
Step 2 – Reload PHP with this Unix command:
/etc/init.d/php-fpm reload
Now if you reload the info.php page again in your browser, you should now see APC is listed there along with detailed information.
How to Avoid TCP Overhead Issue
You may notice that PHP-FPM service is by default listening on port 9000 on localhost 127.0.0.1. However as user you can also make a slight adjustment to make PHP-FPM use a Unix socket with a purpose to avoid the TCP overhead issue. The configuration you can adjust is located in www.conf file.
Step 1 – Edit it:
nano /etc/php-fpm.d/www.conf
Step 2 – Now locate the line that says: listen = 127.0.0.1:9000. Found it? Now add the ; symbol to comment it and add additional line so it looks like this:
;listen = 127.0.0.1:9000 listen = /tmp/php5-fpm.sock
Here’s the screenshot:
Hit Control+O to save then Control+X to exit once done.
Step 3 – Reload PHP once again:
/etc/init.d/php-fpm reload
Step 4 – yet, you have to also edit fastcgi.conf which is Lighttpd’s PHP configuration file:
nano /etc/lighttpd/conf.d/fastcgi.conf
Step 5 – Now replace the host and port lines with “socket” => “/tmp/php5-fpm.sock”. See below:
Before:
After:
That’s it. Now you can save it and exit.
Step 6 – Also restart Lighttpd service:
/etc/init.d/lighttpd restart
That’s it. Enjoy!..
hello , how to add website php+mysql on lighttpd?
“Step 5 – Now replace the host and port lines with “socket” => “/tmp/php5-fpm.sock”. See below:”
Doesn’t quite work for me…
(mod_fastcgi.c.1754) connect failed: No such file or directory on unix:/tmp/php5-fpm.sock
(mod_fastcgi.c.3021) backend died; we’ll disable it for 1 seconds and send the request to another backend instead: reconnects: 0 load: 1
(mod_fastcgi.c.2848) fcgi-server re-enabled: unix:/tmp/php5-fpm.sock
I fixed my issue by changing path in /etc/php-fpm.d/www.conf and /etc/lighttpd/conf.d/fastcgi.conf to /var/run/php5-fpm.sock
/etc/php-fpm.d/www.conf >>
listen = /var/run/php5-fpm.sock
also I have
listen.owner = lighttpd
listen.group = lighttpd
listen.mode = 0666
user = lighttpd
; RPM: Keep a group allowed to write in log dir.
group = lighttpd
/etc/lighttpd/conf.d/fastcgi.conf >>
fastcgi.server = (
“.php” => ((
# “host” => “127.0.0.1”,
# “port” => “9000”,
“socket” => “/var/run/php5-fpm.sock”
# “broken-scriptfilename” => “enable”
# “docroot” => “/srv/www/servers/www.example.org/htdocs/”
)))
thanks for your tip 🙂