Comments on Installing Nginx With PHP5 (And PHP-FPM) And MySQL Support (LEMP) On Ubuntu 12.04 LTS
Nginx (pronounced "engine x") is a free, open-source, high-performance HTTP server. Nginx is known for its stability, rich feature set, simple configuration, and low resource consumption. This tutorial shows how you can install Nginx on an Ubuntu 12.04 LTS server with PHP5 support (through PHP-FPM) and MySQL support (LEMP = Linux + nginx (pronounced "engine x") + MySQL + PHP).
16 Comment(s)
Comments
I haven't tried this, but I guess
location ~ ^(.+\.php)(/.+)$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
could work. What do you think?
The best way to do it is to have a block like this:
location / {
try_files $uri $uri/ /index.php;
}
This will check if the requested file exists, and if not, perform an internal rewrite to index.php. Then the index.php FastCGI rule will always match (and 404s will be routed via your PHP app).
See:
http://wiki.nginx.org/Codeigniter
http://wiki.nginx.org/Pitfalls#Front_Controller_Pattern_based_packages
https://nealpoole.com/blog/2011/04/setting-up-php-fastcgi-and-nginx-dont-trust-the-tutorials-check-your-configuration/
This tut has vulnerable configs (not that its easy to prevent when using this method).
Thanks a lot for the tutorial on how to install ngnix with PHP5 support on Ubuntu. However I see here the same problem that I see in all PHP5 installations using FastCGI on nginx and that is:
location ~ \.php$
This line tells nginx to pass the request over to the php-cgi process when the request ends with .php but what about PHP scripts that doesn't end in PHP, like CodeIgniter URLs:
example.com/index.php/shoes/display/23
This will cause an error. A way to fix this is to change the regular expression for this one:
location ~ \.php
But this is not very safe because it causes the php-cgi process to try and run any file which contains .php anywhere on it's name for example an image called
tutorial.php-mysql.jpg
Will be passed as a script file to the php-cgi process. In that case it will cause an error but this can also give an attacker a way to execute malicious code at will.
So, have anyone found any way to solve this issue?
Take a look at the CodeIgniter article on the Nginx wiki and how its configuration handles it using the try_files setting.
i get stuck after creating vi /usr/share/nginx/www/info.php
even though info.php exists in the /usr/share/nginx/www folder, the browser returns a 404 not found error.
can someone help me here.
I had the same issue. It seems to work with:
fastcgi_pass unix:/tmp/php5-fpm.sock;
instead of
fastcgi_pass unix:/var/run/php5-fpm.sock;
for example:
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/tmp/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
Hope that helps.
I have been using windows in my development and when configuring a server on linux, it has been apache2. I decided to use nginx for the speed it provides (and it's asynchronous nature) but was struggling to configure it (with PHP) and every article I found online was just messing up my install. Then I came across your article and things worked like a dream! Your configuration is simple, your tutorial steps are to the point. I wish I stumbled upon this article sooner!
Thank you for de-mystifying the waters!
The latest distribution of PHP5-fpm came pre-configured to run on a socket first. I think most will notice this too. But the default socket for me, just in case anyone needs to know, was...
/var/run/php5-fpm.sock
I don't know if you want to update this article since people will find out anyways.
Thank you for this great article.
I faced an issue in Step # 7 though. After reloading php I got a 502 Bad Gateway. The fastcgi_pass in step # 5 had to be changed to /tmp/php5-fpm.sock. Originally there were 2 lines of fastcgi_pass and I had to comment out one.
listen = /tmp/php5-fpm.sock
I had to use:
listen = /var/run/php5-fpm.sock
I also didn't need to modify:
/etc/nginx/sites-available/default
I get nginx 404 using codeigniter.
If you merge steps 5 and 7 before attempting to view info.php you may avoid the 502 bad gateway error that I got, even after I'd taken into account the above comments. After all, if your server spec is out of line with www.conf then you might reasonably expect trouble. Step 6 is next.
Hi Timmen,
Really nice article. I ran across a few small issues while setting this up and so documented and wrote a guide on it as well. Just small things like removing apache first, installing phpmyadmin as part of the install, and a slightly different server container config as I had a small issue using the one here while using TCP/IP.
I'll leave a link here for those that might run into small issues and need a fix for them. I am also making a video just to further clarify.
http://portal.ioflare.com/knowledgebase/4/Installing-LEMP-Stack-On-Your-Ubuntu-Cloud-Server.html
After searching a lot, I found you page. This is really informative and it helped me to great extent.
This is over a year old but I really found it helpful. On step 7 I found I had to restart php5-fpm in order to hit any of my test pages from the previous steps. Just a heads up if you made it that far :)