Configuring Nginx & php5-fpm to work with Codeigniter
I found the solution for this problem. It wasn't related to Codeigniter nor Nginx. the probem was within MySQL, since I auto-loaded database library which connects to MySQL upon requesting any page. MySQL itself wasn't installed anyway, that's why nothing works.
In Debain/Ubuntu all I did was apt-get install mysql-server mysql-client php5-mysql
and then setting up mysql and restarting php-fpm service php5-fpm restart
. and you're good to go.
Related videos on Youtube
Abdulaziz
Updated on September 18, 2022Comments
-
Abdulaziz over 1 year
As I'm trying to make my switch from Apache 2 to Nginx, I faced dozens of problems, and still fixing them. anyway, I'm trying to make them work, but no-luck yet. So I'm asking here looking for help/previous experience.
I used the default configurations to run PHP, and it works great when running PHP files only. but unfortunately codeigniter URL's doesn't end in *.PHP. So I think PHP-FPM doesn't treat my scripts as PHP files, since the URL doesn't end in
*.PHP
. I'm just guessing though.So, Is there any possible way to make them work togather? it seems impossible to me, I tried lots of configurations(after modifying them to fit my environment, ofcource) but non of them worked with Codeigniter.
Hopefully posting my current configurations that works with normal php files, will lead to some solutions.
Note: This is the exact same one posted in Nginx's wiki page for Codeigniter framework.server { server_name _;#_ to catch all requests. root /usr/share/nginx/www; index index.html index.php; # set expiration of assets to MAX for caching location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ { expires max; log_not_found off; } location / { # Check if a file exists, or route it to index.php. try_files $uri $uri/ /index.php; } location ~* \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_split_path_info ^(.+\.php)(.*)$; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } }
using the previous configuration for my
/sites-enabled/default
configuration makes PHP works but not codeigniter, all I get is white pages, and codeigniter's custom 404 when accessing non-existent files.I also reconfigured config.php files like the following:
$config['base_url'] = "http://localhost/"; $config['index_page'] = ""; $config['uri_protocol'] = "REQUEST_URI";
I tried these configurations and lots of others, usually the same results.
Anyway, there are few points that will(hopefully) get me an answer:
1.I tried these configurations on Debian 6/Ubuntu 12.04
2.I'm running the latest
php5-fpm
,nginx
fromapt-get
.3.Something I notice, commenting out
SCRIPT_NAME
andSCRIPT_FILENAME
(infastcgi_params
file) causes codeigniter to show an error. however commenting these on the current configuration above doesn't have any impact.4.File permissions for all files under
/usr/share
is777
, I know it's insecure, but I'm using it for testing only.-
Michael Hampton almost 12 yearsDid you reload nginx after changing your configuration? I don't see any obvious problems here. Also, if you get the white screen of death, check PHP's error log.
-
Abdulaziz almost 12 years@MichaelHampton Yes, after every single change I use(as
root
)service nginx restart
. -
Michael Hampton almost 12 yearsWhat PHP errors are in your logs?
-
Abdulaziz almost 12 years@MichaelHampton Nginx error logs? Nothing related at all.
-
Michael Hampton almost 12 yearsYou should set up PHP to log errors, so that you can find out what it's doing.
-
Abdulaziz almost 12 years@MichaelHampton I set the
error_logs
totrue
inphp.ini
, anderror_reports
to everything, but still no errors shown, only some start and bye-bye message invar/log/php5-fpm.log
file. -
Hawili almost 11 yearsYour configuration is not secure! check this for more info nealpoole.com/blog/2011/04/… to make it easy for you just add
try_files $uri =404;
as the first line forlocation ~* \.php$ {
-