PHP5 (with FastCGI) and MySQL
Bolt on PHP5 for server-side web applications configured with FastCGI and MySQL to handle our databases. And tweak that lot for optimum performance of platforms from WordPress to Drupal.
From the first slew of tutorials in this intrepid Unmanaged-VPS-Ubuntu-Nginx guide, this Linux box is now quite the tough guy, easier to connect to and use, and fully updated.
It’s time to add server-side scripting, database functionality and caching.
But first, we’ll strap on the Build Essential meta package, which includes some applications that we need to help install larger packages further down the road.
Setup Unmanaged VPS: The Ubuntu-Nginx Guide
Take your virtual private server from zero to hero
.. from blank box to cute-as server ..
with this easy-to-follow copy/paste guide.
22+ parts with video, here’s the index.
Video: Install & Tweak PHP & MySQL
Watch the, er, guvideo for a better idea of how to do this.
Without further ado, get that terminal open…
Install Build Essential
Type:-
…or if you followed Edit bashrc for User-Friendly Linux, plus System Updates, enjoy those aliases and just type:-
Install PHP5
Copy & paste this lot:-
Imagick, a php extension to create and modify images, can be a problem install. We get around it this way:-
…hitting “Enter” on prompt.
Configure PHP’s php.ini File
We need to make some adjustments for the Imagick installation, as some of you may have noticed was mentioned at the end of the installer.
If you’re planning to host what can be memory-intensive applications such as WordPress or Drupal, we’ll consider the memory limit as well.
Let’s open the file:-
We’ll add the line needed for Imagick, to the end of the file:-
php.ini, memory_limit & php-Intensive Platforms (WordPress, Drupal, MODx etc)
For heavily php-based applications, such as WordPress and many content management platforms such as Drupal, you may need to increase the allocated memory limit, which defaults to 16M in the file, php.ini. The optimal setting will vary from site to site. It depends largely on the platform, but also on what modules (plugins, extensions, add-ons or whatever else they’re called) are bundled.
If you’ve got any tips about this, platform-specific or generally, please let me know and leave a comment below.
The worst way to discover a memory problem is when your system throws a php error, your homepage replaced by the ‘white screen of death’, saying something like “Fatal Error: PHP Allowed Memory Size Exhausted”.
If you want to run a php framework of some kind, I recommend you head to the appropriate forum and run a search for something like “php.ini memory_limit” and see what’s being suggested, and change the value accordingly.
For the record, mine is set to 64M, but I am a bit plugin-happy
To open php.ini, type:-
Here’s the line to consider changing:-
…and, for instance with WordPress, you may do well to change the value to:-
It’s very difficult to predict your needs but, generally and for instance, I would consider 32M for Drupal running extra modules, or 48M for WordPress running several plugins.
Please let me know what you find out. I’ll add more guidelines accordingly.
502 Bad Gateway Error
A couple of folks have reported 502 errors with this method, which generally means fastCGI is having a freak.
If, having gone on to set up Nginx and your first site or two, you receive 502 errors when trying to navigate your site, come back here and try this. Open your php-fastcgi file:-
Replacing this:-
With this:-
Thanks to Haynes for that tip
Install MySQL Server & Secure the Installation
Type this:-
…when prompted, add and confirm a MySQL password for the MySQL user “root”.
Now secure your new MySQL server by typing:-
…when prompted, enter your new root password; change it again if you want to, but this isn’t necessary; type “y” to remove anonymous users; type “y” to disallow root login remotely; type “y” to remove test database and access to it; type “y” to reload privilege tables.
Simple!
.. And that’s that. Yup, index ..
Setup Unmanaged VPS: The Ubuntu-Nginx Guide
Take your virtual private server from zero to hero
with this easy-to-follow copy/paste guide.
“My local PC runs Windows” Show me for Linux/Mac
“My local machine is Linux/Mac” Show me for Windows
22+ parts with video, here’s the index ..
-
Setup Unmanaged VPS: The Ubuntu-Nginx Guide
Introducing vpsBible’s how-to guide for setting up a super-charged unmanaged VPS featuring Ubuntu and Nginx.
-
VPS (Virtual Private Server) vs Shared vs Dedicated
Before getting stuck in, let’s compare the web host types in detail and weigh up the differences between managed & unmanaged VPS.
-
Set Up a VPS Linux Distribution
Opening a VPS hosting account, we’ll choose, configure & launch a Linux server ‘distribution’.
-
Set Up Command Line Interface (CLI) using PuTTY
Now we’ve got a server to play with, we’ll connect to it, using the ’ssh’ protocol for a super-secure link.
-
Create a Linux User & Set Permissions
Initially you’ve logged into your remote Linux machine as ‘root’. We’ll add you as a user now, with root or SuperUser permissions.
-
Encrypt Data with OpenSSH & Auto-Login with PuTTY
Now we can use ‘authentication keys’ to shore up our connection while simplifying login to an automated, password-free yet secure process.
-
Harden the Secure Shell (SSH) & Create a Firewall
The final security step: galvanizing the OpenSSH protocol and setting up an ‘iptables’ firewall. Now the server is rock ruddy solid.
-
Edit bashrc for User-Friendly Linux, plus System Updates
We use the terminal a whole lot so let’s cut ourselves a break and create some command shortcuts, then update the server.
-
Add a Domain Zone to Your VPS
To make your server aware of yourDomain.com you’ll need a few ‘DNS’ records. Damn this is simple I’m practically asleep!
-
Prepare Linux Server for Email with Postfix
In order to send and receive email from our web applications we’ll install some software and tweak the ‘RDNS’ record.
-
Install PHP5 & MySQL – The One-Stop FastCGI Index Page
Add PHP5 & MySQL, but which FastCGI method is best? Here’s the benchmarking, 4 ways to install/compile, add caching modules & more.
-
Nginx (better than Apache) Web Server
Let’s install this supersonic web server, tweaking its file structure & adding default configuration files.
-
Setup FileZilla for Secure FTP (SFTP)
There’s FTP, and then there’s ‘Secure FTP’. We’ll set up FileZilla using SFTP – and those authentication keys – for secure file transfers.
-
Serve Multiple Sites & Blogs with Virtual Hosts
We want a website folder structure, some permissions, some site-specific configuration files and, hey, we’ll hook up ‘FastCGI’.
-
Adding Sub-Domains with Nginx
Need a sub-domain? If you do, here’s how to add subDomain.yourSite.com with an ‘A record’ and a configuration file.
-
Integrate A CMS Like WordPress, WP MU, Drupal etc
For those of you wanting to use a specific content framework, such as a CMS, blogging or forum software, pop in here and we’ll sort that out.
-
Google Apps for Domain-Specific Email
GA’s free Standard Edition for catchall webmail – POP-ping or IMAP-ped to your desktop client – saves precious server resources for your webs.
-
Moving Day! How to Move Your Blog or Site
Nearly there. Here’s the web site or blog migration guide, relocating to your VPS with minimal or no downtime.
Manage Unmanaged VPS: Ubuntu-Nginx Administration
Already set it up? We’d best maintain it then.
Toggle to the .. Ubuntu-Nginx Admin Index
Manage Unmanaged VPS: Ubuntu-Nginx Administration
Maintain your virtual private server with ..
.. cheatsheets, tutorials, tips & guides.
Head back to the .. Ubuntu-Nginx Installation Index
-
Nginx Control Panel: Here’s the Deal
You’re migrating shared-to-VPS and want a GUI like cPanel? No you don’t, not after you read this! What’s more, you don’t need one either.
-
Nginx Control Panel Workarounds
For every control panel module there’s an equivalent (and faster) terminal command. Let’s compare these.
-
Upgrade to Nginx Latest Version with ZERO Downtime
Keep Nginx up-to-date – and optimized for security. This guide shows precisely how *with no missed server requests*.
-
Compile an Advanced Nginx Module Configuration
Need special web server functions? Here’s how to install or upgrade Nginx with bespoke required modules & parameters.
-
Add Web Sites Nginx Cheatsheet
Once you’ve set up your first site, adding more gets easier. Especially when you refer to this. (Let’s face it, I’m just too good to you.)
-
Adding Sub-Domains with Nginx
Rather than add somesite.tld, maybe you want some.subsite.tld? Here’s how to set your A record using a DNS manager and the vhost file for Nginx.
-
Add/Delete MySQL Database from Command Line
Need a database? Want to bin an old db? This tutorial shows you how using the terminal command line console whatnot.
-
Configure phpMyAdmin for Nginx to Maintain MySQL
Rather than have to crunch databases via the command line, we can make life easier by adding this splendid tool to manage them.
-
Backup/Export MySQL DB with CLI & mysqldump
Here’s how to backup a database from the command line using the handy mysqldump utility.
-
Import MySQL Database using Command Line
Import data to a new database with this simple mysqldump guide .. great for reinstating an old backed-up db, for instance.
-
Batch Replace/Delete Data in MySQL Database
Change repetitive db data with a single command. Rather than manually find & alter each instance, isolate the lot & update multiple content.
-
LOST a MySQL Password? How to RESET it
Lost your MySQL password? That’s a bit of a bummer .. or it would be if you didn’t have this new password setup guide, huh.
-
Backup or Upload with Secure Copy SCP (Linux to Linux)
Never mind FTP m8! Save time and download/upload your files using encrypted SCP (secure copy), the #1 Linux-Linux file transfer solution.
-
Backup or Upload with PuTTY PSFTP (Windows/Linux)
Equivalent to the above, how to transfer your files if you’re unlucky enough to be running Windows for your local PC. Not that I’m biased.
-
*USEFULLY* Park A Spare Domain with Nginx
Got a spare domain name sat around doing nothing? Put it to some use, have it point its traffic at your primary domain. Here’s the deal ..
-
Password Protect Nginx Web Files/Directories
Maybe you want a hidden website directory, say, for projects, or are developing a site and want it off-limits ’till launch day. Top tip ..
-
Redirect Web Pages with Nginx Rewrite Rule
Here’s the syntax to point a site or blog to another domain. That way, for instance, traffic from a passive site benefits an active one.
-
Nginx Error & Access Logs
Need to troubleshoot some error, or want to find out who’s trying to hack your site? Here’s a start .. those tell-tale logs.
-
Subversion for Easy CMS Updates ie WordPress/Drupal/etc
A brief introduction to install and know about the power of Subversion, the version control system to keep scripts and platforms up-to-date.
-
You Tell Me …
Something missing? Probably. Let me know what you’d like included. And if it’s listed below, shout out anyhow and I’ll try to bump it.
Nginx Admin: In the Works ..
This lot’s marked for addition already:-
- Setup or Edit DNS using Bind
- Network Tools Troubleshooting Guide
- The Comprehensive Permissions Guide
- Configuring Nginx Rewrites
- Custom Website Error Page
- Setting up Cron Jobs
- Rsync for Incremental Remote-to-Local Backup
- Cron & Rsync for Automatic Backup
- Cron & mysqldump for Auto DB Backup
- Safeguard Bandwidth with Hotlink Protection
- Block Access with Nginx’ IP Deny
Got a Question? Want to Comment?
This site has no comments system. We've got a better system!
Please use the link at the top of the post and goto this topic's forum thread:-
- to help build community
- for a superior knowledge base
- to keep info in one place, not diluted between forum & comments.
For those posts ported from Guvnr.com (pre-Feb/2010), archive comments remain.
Handy info from these comments is in the process of being ported to the forum and attributed to the original author.
If you think this idea sucks, let me know.
Thank you.






















Install/Upgrade WORDPRESS with SUBVERSION - VPS Bible #15 - GUVNR June 4th, 2009 at 9:49 am
[...] Part 8: * PHP5, MySQL, Xcache, Subversion (for Platforms like WordPress) [...]
justGeorge July 22nd, 2009 at 6:18 pm
great stuff Guv, these tuts have helped me out through a lot I never touched before. Your thorough research and blog posts are greatly appreciated.
One awkward thing that happens to me on my side is the mail that is send to my yahoo account, the mail goes directly to the spam box. I take it that there something being filtered here. would you know of a way to avoid this for the sake of future circumstances.
thanks,
George
the_guv July 22nd, 2009 at 9:53 pm
@justGeorge .. big cheers appreciate that.
Yahoo .. hmmn, never used it, sorry .. but that sounds like a q for uncle google. sounds like a common error that you’ll find an answer to easily enough. *i guess* sorting your mail thru yahoo would be pretty similar to going the google apps route though. sorry, that was basically a cop out, huh! best of luck tho’.
trav 19 August 2nd, 2009 at 4:30 am
hello, mister guv! im having issues after doing the last part of the 3-line installing imagick: “sudo pecl install imagick”, gave me this error:
make: *** [imagick_helpers.lo] Error 1
ERROR: `make’ failed
what do i do first? =( my box is ubuntu 9. i’m assuming there was a remaining incomplete install in the box since it gave that error. how do i clean it first? or do i have to do that? i’m really the noob of all noobs.
thank you! and you have a nice accent btw.
thanks,
travis
the_guv August 2nd, 2009 at 12:53 pm
@Travis .. thank you and LOL!
Hmmn, I’ve not tried this with Jaunty/Ubuntu9. Quite likely there is no tangible difference but I don’t think it’s worth the risk. I recommend Hardy Heron/8.04, because it’s an LTS (long term support) edition which means it will be supported for much longer, adn that’s important for a production server. Also, any new edition will inevitably have bugs, some perhaps obscure, whereas with an older version the vast majority of these have been ironed out or worked around. To clarify, that’s for a server .. for my home PC I do use the latest Ubuntu, so Jaunty these days.
Having said that, there will be a workaround for your problem, somehow, but, not having experimented with your method, I don’t know what it is! Sorry. Have you tried the Ubuntu forums .. support there is brilliant and I would be surprised if this topic wasn’t covered alrady for Jaunty.
But do beware, as a general tip, if you’ve been tempted to use the latest edition – of any software – just because it’s the latest, that’s not always a good reason. For a stable production server, it makes no sense, IMHO, however tempting.
trav 19 August 5th, 2009 at 9:40 pm
Hi again, guvnr! I was able to fix it by downloading the 2.2.x v of imagick instead of 2.3 for the meantime.
But my question is.. a lot of sites ive been reading prefer eAccelerator to xcache… what if i wanted to use that instead but i have already instealled xcache? what do i do?
(((
trav 19 August 5th, 2009 at 9:43 pm
Hi guv pls disregard my prev question. i was wrong. thanks!
the_guv August 5th, 2009 at 11:53 pm
@Travis .. well, come on .. spit it out man .. give us an explanation! Why did you go with Xcache and not eAccelerator .. I know I had a reason, vaguely recall, but would love to hear yours.
.. Nginx have one of their own, as well, if you fancy a play .. check out nCache. I haven’t tried it, but it looks pretty solid. All in all, for this guide, I think Xcache is best.
PS. Your initials aren’t by any chance TLB are they? If so, splendid to see you on Twatter.
trav 19 August 6th, 2009 at 1:22 am
TLB? No, I don’t have a twitter account yet.
I was just gonna say, that I did follow everything in this series to a tee and then I read some people who chose eAccelerator. I really didn’t switch to it as your choice has been reinforced by wordpress devs to be the best caching plugin to date.
Thanks very much!
the_guv August 6th, 2009 at 8:28 am
@Travis .. cheers to you.
Sarteck September 18th, 2009 at 4:11 am
GUV EDIT .. RE: imagick errors with Jaunty 9.04? .. END EDIT
Thanks, guv. Like Trav @#4, I got the same imagick errors with 9.04. I’m not sure that I actually need it, tbh, so I didn’t bother installing the 2.2.x version like Trav did.
However, mysql_secure_installation gave me an error when trying to put in my password. Problem is, my password contained special characters that must be escaped inside the quotes used in /usr/bin/mysql_secure_installation (y’know, stuff like $%^&).
For those of you running into this problem, either do not user special characters, or modify the script:
sudo nano /usr/bin/mysql_secure_installation
Press Ctrl+C to see the sursor position, and run it down to about line 47. You will see the following:
echo “# mysql_secure_installation config file” >$config
echo “[mysql]” >>$config
echo “user=root” >>$config
echo “password=$rootpass” >>$config
}
get_root_password() {
Replace the line
echo “password=$rootpass” >>$config
with
echo “password=’$rootpass’” >>$config
Because it’s now single-quoted, it will accept anything short of single quotes and backslashes, I think.
the_guv September 18th, 2009 at 6:39 am
@Sarteck – thank you. that’ll really clarify the imagick thing for a few folks running 9.04, and I hope you don’t mind me hi-lighting your cmt.
Paul Maneesilasan September 25th, 2009 at 11:15 pm
GUV EDIT .. RE: imagick errors with Jaunty 9.04? .. END EDIT
Ok, I also had issues with image magic and 9.04. Here are the steps to solve the problem
as per the Gov’s instructions:
sudo apt-get install php-pear
then, u’ll install imagick manually:
first remove old if you attempted:
sudo apt-get remove imagemagick
then grab resources:
sudo apt-get install libperl-dev gcc libjpeg62-dev libbz2-dev libtiff4-dev libwmf-dev libz-dev libpng12-dev libx11-dev libxt-dev libxext-dev libxml2-dev libfreetype6-dev liblcms1-dev libexif-dev perl libjasper-dev libltdl3-dev graphviz gs-gpl pkg-config
then wget: ftp://ftp.imagemagick.org/pub/ImageMagick/ImageMagick.tar.gz
cd ImageMagick-6.5.6 (ver at time of post)
./configure
make
sudo make install
then add export LD_LIBRARY_PATH=/usr/local/lib to your /etc/profile or ~/.bashrc
sudo ldconfig
then test via: make check
once you see it all pass and feel good, continue with:
sudo pecl install imagick
and pick up with the rest of the tutorial
great work btw Guv!
the_guv October 6th, 2009 at 6:59 am
@Paul .. righty-ho
appreciate that .. guess it’s a bit of a change with the new Nginx stable release I substituted in the guide. er, i didn’t beta very well, huh!
will be running off a new server with the bible’s spec next week, and will upgrade whatever posts have any issues tho, *gulp*, i kind of think this is the only one i’ve not tested and changed. famous last ..
again, hope you don’t mind the edit. i know that’ll help a lot of folks every day.
adergaard October 20th, 2009 at 8:33 am
Hey Guv, excellent help you are giving here.
Question: I’m on a Linode360 plan just like you. Having completed this step, my memory is down to it’s last 16 MB and there are a huge amount of processes just sitting there when I run TOP.
Is this something that I should fix with a “re boot”. I generally don’t like to have to re-boot because what if this happens when in “production” I’d like to be back to the mid 40 MB that it was before this step.
Any ideas?
Thanks again for excellent tutorials.
the_guv October 21st, 2009 at 3:27 pm
@adergaard .. now I’m no expert on this but, from what I have read ..
the fact your RAM is showing as low is not necessarily a sign that you need more RAM (or SWAP) but could just indicate that the RAM is being used to maximum effect. With a higher demand, you may find a similar amount of RAM is being occupied.
Mine regularly shows I have under 20MB, yet with no adverse affect.
Then again, any RAM people out there, please educate us.
Robert October 29th, 2009 at 3:24 pm
The aptitude install command for PHP5 has php5-sqlite specified twice.
Why is that ?
the_guv November 5th, 2009 at 8:38 am
@Robert .. because, er, I am a clot!
Thank you, 1 instance removed.
(Actually, wouldn’t have made any difference in install .. fortunately aptitude is pretty clot-resistant.)
Thanh November 21st, 2009 at 11:26 am
I did everything on this tutorial but when i check the phpinfo() output, it shows that variable cache is disabled. And the cache size doesnt match up with the ones in the php.ini values. Is there something wrong?
Thanh November 23rd, 2009 at 3:07 am
update: i had to reboot to server to see the xcache changes. is there a way to reload those changes without rebooting the server? I tried rebooting nginx but that didnt change anything.
the_guv November 24th, 2009 at 12:31 pm
@Thanh .. sorry to appear elusive, been a bit frantic.
Not sure what you did but the changes should have been automatic. At least, they have been for me, implementing Xcache.
Thanh December 3rd, 2009 at 5:35 am
When I make changes to php.ini, it does not update automatically. I tried rebooting nginx but that did not work either. I had to reboot the entire server for the changes to be recognized. is there a way to reload php.ini changes without a server reboot.
kevinayP December 16th, 2009 at 4:17 pm
Hi Guv,
Thanks again for an awesome tutorial to set things up! I’m only wondering about one thing. What if there is an update to PHP or MySQL? Or if in the future PHP6 is released how can you update PHP5? Also, how can you remove/uninstall say, Subversion, for example. Other than that, I managed to follow every step up until now.
Thanks,
Kevin
Install/Upgrade WORDPRESS with SUBVERSION: VPS BIBLE - vpsBible February 10th, 2010 at 4:59 pm
[...] PHP5, MySQL and Xcache (for Platforms like WordPress), amongst other things, we installed Subversion. In Nginx (better than Apache) Web Server we set up [...]
HOW-TO Install PHP-FPM + MySQL 4 Nginx *TESTED PERFECT*: VPS BIBLE - vpsBible April 1st, 2010 at 2:47 am
[...] wanted to write this tutorial a year ago (in favor of the more basic PHP/FastCGI guide that I did write) but, at the time, wasn’t convinced PHP-FPM was being properly [...]
Compare FastCGI, Spawn-FCGI & PHP-FPM for PHP5/Nginx: VPS BIBLE - vpsBible April 23rd, 2010 at 7:58 pm
[...] FastCGI [...]
LOCK LINUX! – Configure SSH & an iptables Firewall: VPS BIBLE - vpsBible June 1st, 2010 at 1:46 pm
[...] general housekeeping, making the CLI more user-friendly and updating the system and locale. Then in PHP5, MySQL and Xcache we’re really cooking on gas before, in Add a Domain Zone to Your VPS, we well on the way to [...]