Howto nginx + php5 + mysql + phpmyadmin + ubuntu shortest setup

The following tutorial is done with Ubuntu Lucid 10.04 on Lenovo Thinkpad X200 laptop. In this tutorial, I will show you the shortest and easiest way to set up nginx up with PHP5, MySQL and phpmyadmin running through FastCGI.

UPDATE: I am going to do another post on nginx installation, but this time with PHP-FM instead of spawn-fcgi, this new piece of software has gained attention recently. Stay tuned guys!

Install all softwares:

sudo apt-get install nginx mysql-server-5.1 php5-cgi php5-mysql phpmyadmin

Make sure you set up the MySQL root password.

Create php-fastcgi init script:

sudo nano /etc/init.d/php-fastcgi

Inside, paste the following code into it:

#!/bin/bash
BIND=127.0.0.1:9000
USER=www-data
PHP_FCGI_CHILDREN=15
PHP_FCGI_MAX_REQUESTS=1000

PHP_CGI=/usr/bin/php-cgi
PHP_CGI_NAME=`basename $PHP_CGI`
PHP_CGI_ARGS="- USER=$USER PATH=/usr/bin PHP_FCGI_CHILDREN=$PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS=$PHP_FCGI_MAX_REQUESTS $PHP_CGI -b $BIND"
RETVAL=0

start() {
      echo -n "Starting PHP FastCGI: "
      start-stop-daemon --quiet --start --background --chuid "$USER" --exec /usr/bin/env -- $PHP_CGI_ARGS
      RETVAL=$?
      echo "$PHP_CGI_NAME."
}
stop() {
      echo -n "Stopping PHP FastCGI: "
      killall -q -w -u $USER $PHP_CGI
      RETVAL=$?
      echo "$PHP_CGI_NAME."
}

case "$1" in
    start)
      start
  ;;
    stop)
      stop
  ;;
    restart)
      stop
      start
  ;;
    *)
      echo "Usage: php-fastcgi {start|stop|restart}"
      exit 1
  ;;
esac
exit $RETVAL

Next we make the script executable and make it auto-startup by default.

sudo chmod +x /etc/init.d/php-fastcgi
sudo service php-fastcgi start
sudo update-rc.d php-fastcgi defaults

Create virtual host file for phpmyadmin

sudo nano /etc/nginx/sites-available/phpmyadmin

The content is

server{
	listen 80;
	server_name phpmyadmin;
	access_log /var/log/phpmyadmin.access_log;
	error_log /var/log/phpmyadmin.error_log;

	location / {
	  root /usr/share/phpmyadmin;
	  index index.php;
	}

	location ~ \.php$ {
	    fastcgi_pass    127.0.0.1:9000;
	    fastcgi_index   index.php;
	    fastcgi_param   SCRIPT_FILENAME /usr/share/phpmyadmin$fastcgi_script_name;
	    include         fastcgi_params;
	}
}

Now we need to enable this vhost

sudo ln -s /etc/nginx/sites-available/phpmyadmin /etc/nginx/sites-enabled/phpmyadmin
sudo nano /etc/hosts

Append the ‘phpmyadmin’ into 127.0.0.1 line

127.0.0.1	localhost phpmyadmin

Now restart the nginx server

sudo service nginx start

Fire up your Firefox and type in http://phpmyadmin. Enjoy!

Advertisements

About Jones Lee

Nothing much about me..

53 responses to “Howto nginx + php5 + mysql + phpmyadmin + ubuntu shortest setup

  1. Jay

    Thanks, ill try this.

    Typo on step 1. mysql-server-5.1

  2. Jay

    Also missing is the installation of nginx.

    sudo apt-get install nginx

    • Andrés

      Jay, he edited without commenting, or he already had it, because I see it in the first line. In fact, it’s the first package he installs.

      joneslee85, THANK YOU VERY MUCH! Finally found a tutorial that explains how to get nginx and PHP working. I just needed PHP for some personal testing, not server setup, and well, all other documents were using PHP-FPM or the Lighttpd Spawn-CGI module, which for some reason I couldn’t get to work properly.

      Thanks again, and good luck!

  3. alzea

    thx for this tutorial, but i still have porblem here…

    i follow your tutorial and this one too (http://www.howtoforge.com/installing-nginx-with-php5-and-mysql-support-on-debian-lenny). but when i open php file (ex: index.php) it just download it to my PC, not open it on browser as a web!

    how to fix this?

    thanks before

    sorry for my bad English

  4. Hi , Jones!
    Thank you for the article , it was really useful.
    I found one mishmash: you used destination – source instead of source-destination as a ln‘s params.

  5. Gourav Shah

    I have automated nginx installation with php5 support. Here is the script and the article on how to use it http://www.initcron.org/how-tos/autoinstall-nginx-web-server-with-php-on-ubuntu-10-4-lucid/

  6. Jenny

    @joneslee85, I’m totally new to this, but want to try it out. I heard nginx is for static content. My blog uses buddypress which is a very interactive and dynamic theme. Can I still use nginx? or, what else to add on nginx to work with the dynamic contents?

  7. Pingback: PHPCOE » [Tutorial] Part 1: Installing Nginx, Php-Fpm, MySql, Phpmyadmin and apc on Ubuntu 10.10

  8. After 4 hours of agony, it just worked in seconds after reading this post. Thanks joneslee85!!!

  9. Hi,
    I get error at the beginning:
    “E: Couldn’t find package nginx”

  10. got it thanks.
    we’ll see how it goes.

  11. Agnostik One

    Great “how-to”… Worked like a charm…
    Thumbs up! 🙂
    Cheers 😉

  12. FAIL!

    very bad install, you MUST take care of setup and library dir, look at the default config for apache that comes with phpmyadmin. you failed. you failed multiple times, because you are not only doing bad, but you are teaching people bad! Double-FAIL!

    • Winter

      I’m no expert or anything but the whole idea of a nginx install is to not have apache installed which takes tons of memory correct? I want to avoid installing apache as much as possible.

  13. Winter

    Out of curiosity how does going to http://phpmyadmin work?

    without some ip in there.. wouldn’t anyone who visits that link visit the same exact page?

    • Tyler

      This would not allow others to go to the site. That is specific to that machine. For example on your local network if you go to http://phpmyadmin on a different machine it will not take you to the phpmyadmin interface. It is ONLY specific to that computer.

  14. Just a heads up, I was using this and ran into an error “no input file specified” when hitting the url.

    After some searching around I found I needed to add the GROUP to the php-cgi config.

    so added:

    GROUP=www-data

    and

    PHP_CGI_ARGS=”- USER=$USER GROUP=$GROUP …

  15. Pingback: Creating Debian VM on Parallels, w/ Rails,PHP, RVM, and NGINX | Victory's Awesome

  16. Debian squeeze repos for php-fpm (as alternative to php-fastcgi): http://www.dotdeb.org/2011/01/11/php-5-3-5-now-for-squeeze/
    Just add repos to /etc/apt/source.list
    apt-get update; apt-get install php5-fpm
    /etc/init.d/php5-fpm restart

  17. iain wright

    This is no longer an ideal install as PHP-FPM is now included with PHP 5.3.X+ which is a great FASTCGI Process manager!

    I highly recommend NOT creating your own PHP FASTCGI Wrapper and going this route…

  18. Pingback: Ubuntu 10.04TLSにnginx + php-fpmを出来るだけパッケージで構築する方法! | それでも地球はまわっている

  19. Tom

    Great tutorial, I’ve got one problem though.

    I want my phpmyadmin to be located at ‘http://localhost/phpmyadmin’
    and not ‘http://phpmyadmin’

    Does any1 know how to do this?

  20. Pingback: Build SUT-Kiosk

  21. Pingback: Ubuntu配置ngnix+phpmyadmin | what is the RIA? just it…||咖啡兔

  22. Pingback: Konfigurasi nginx, php, mysql dan phpmyadmin di BlankOn « Layar Hitam

  23. When I install phpmyadmin using apt-get it prompts me whether I have apache or lighttpd but ngnix is not listed so what do I select ?

  24. http://localhost or http://phpmyadmin do nothing (sends me to the website for phpmyadmin)
    Here’s what I have in the hosts file:
    ————-
    127.0.0.1 localhost phpmyadmin

    # The following lines are desirable for IPv6 capable hosts
    ::1 ip6-localhost ip6-loopback
    fe00::0 ip6-localnet
    ff00::0 ip6-mcastprefix
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters

    Any help?

  25. Thanks for the article. It worked even now with Ubuntu 11.10!

  26. maybe you sould change :

    PHP_FCGI_CHILDREN=15

    to:

    PHP_FCGI_CHILDREN=5

    it used all the memory on my poor 512M vps…..
    Hopefully I killed them before too late 🙂

  27. Pingback: From Apache to NginX | TECHiE TALKS

  28. Pingback: PHP, CGI/Perl Script Installation Service

  29. Greg

    it’s really not working on Debian 6 Squeeze, any help?

  30. martin

    It really works! thanks a lot!

  31. Dmitry Paskal

    It’s not the easiest way to do the thing. BTW, mysql-server5.5 in latest ubuntu for now.

  32. Minhazul Haq Shawon

    That works perfectly for me, only mysql-5.5 is to be replaced over 5.1.

    Great post!

  33. It did not still work. The welcome to nginx still shows

  34. oladele

    I had an error trying to restart the service nginx. This is what I got as output

    Restarting nginx: nginx: [emerg] unknown directive “With” in /etc/nginx/sites-enabled/default:68
    nginx: configuration file /etc/nginx/nginx.conf test failed

  35. Pingback: Nginx에서 phpmyadmin VirutalHost 설정 하기 » Ilhwan's Blog

  36. Pingback: Ubuntu 10.04TLSにnginx + php-fpmを出来るだけパッケージで構築する方法! | NO AC.Milan, NO LIFE

  37. victor

    Not so far I have found new cool tool to work with mySQL on ubuntu – Valentina Studio. Its free edition can do things more than many commercial tools!!
    I very recommend check it. http://www.valentina-db.com/en/valentina-studio-overview

  38. Pingback: Some links | Summer and winter and springtime and harvest

  39. Pingback: Install Nginx, Php-Fpm and MySql on Debian - Tidak lebih…… - Catatan Dalam Blog

  40. Pingback: Some links | summer and winter and springtime and harvest

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: