Cannot get PHP to serve on Nginx — Error 404 - php

I'm fairly new to nginx and assumed it would be very straightforward to serve php with it since that setup is so common, but it seems like it's much more complex than I anticipated.
Here's my config..
server {
listen 80;
server_name domain.com www.domain.com;
location / {
root /srv/www/domain.com/public_html;
index index.php;
}
# serve static files directly
#location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt)$
# access_log off;
# expires 30d;
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_pass /var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
If I replace the "index.php" with a "index.html" file, nginx serves up the html perfectly.
I've seen guides that recommend modifying anything from iptables to php-fpm to the php.ini, to fast-cgi to sites-available..?
I'm not sure what many of these tutorials are trying to do exactly... for now I'd just like my index.php to serve up phpinfo(). What's the next step to troubleshoot the 404 error?
Is there a clear guide that goes over the various options available for serving php with nginx?
Debian Wheezy 7.3 on xen

Try this config:
server {
listen 80;
server_name domain.com www.domain.com;
root /srv/www/domain.com/public_html;
index index.php;
location ~ ^(.+\.php)(/.*)?$ {
fastcgi_pass localhost:9000;
include fastcgi_params;
}
}
(assuming your index.php file is in /srv/www/domain.com/public_html)

Related

php and nginx on virtual private server configuration problem - ip works but not domain

I have a VPS setup that isn't working properly.
Environment: Ubuntu 18.04, php7.4, postgresql and nginx
The IP address for the server resolves successfully and shows default "Success" page served. The domain I want to use to point to the server does not work, however, and resolves 404. I feel like this is a simple fix but after a day of searching I haven't found it yet.
Help is appreciated!
Troubleshooting:
nslookup on windows resolves domain name to the correct IP address (I can ssh into server with just domain name)
the domain still remains not found when using the domain in my browser. Here is the /etc/nginx/sites-available/temp.domain:
server {
listen 80;
server_name temp.domain;
root /var/www/temp.domain/public_html;
index index.php index.html;
access_log /var/log/nginx/temp.domain.access.log;
error_log /var/log/nginx/temp.domain.error.log;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
location ~ \.php {
fastcgi_index index.php;
fastcgi_pass unix:/usr/local/var/run/php-fpm.socket;
include fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
default config:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name 192.169.0.1; #temp ip address
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
had to install php-config and then make sure php.ini has postgresql extensions enabled. Also had to fix the server block in nginx sites-available/default

PHP 7.4, NGINX, Laravel 8 - only shows index page, all routes show 404

I have put a Laravel 8 application on a AWS t2.nano Linux AMI ec2 instance. I would like to start up front by saying I have been at this for about a day now. I have tried a few configurations.
Here's some configurations I have tried:
The default nginx config file from the Laravel 8 documentation
https://laravel.com/docs/8.x/deployment#nginx
Another very similar stackoverflow question referenced here
Laravel on nginx says 404 for all routes except index
At the end of the day, I cannot get it to work properly. My index page loads, but any of the other routes end up at a 404 page. You can view the application here.
https://technology.dvemedia.com/
So here are some tech specs and the current state of my conf file.
Laravel - 8
PHP - 7.4
NGINX - 1.12.2
# HTTP
server {
listen 80;
listen [::]:80;
server_name technology;
return 301 https://$host$request_uri; # Redirect to www
}
server {
listen 80;
listen [::]:80;
server_name technology.dvemedia.com;
root /var/www/html/technology/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fixes timeouts
fastcgi_read_timeout 600;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
What am I missing or doing wrong, because I cannot get it to route to save my life.
Try this:
## Nginx php-fpm Upstream
upstream dvemedia {
server unix:/var/run/php/php7.4-fpm.sock;
}
## Web Server Config
server
{
## Server Info
listen 80;
listen [::]:80;
server_name technology.dvemedia.com;
root /var/www/html/technology/public;
index index.html index.php;
## DocumentRoot setup
location / {
try_files $uri $uri/ #handler;
expires 30d;
}
## Disable .htaccess and other hidden files
location /. {
return 404;
}
## Rewrite all request to index
location #handler {
rewrite / /index.php;
}
## Execute PHP scripts
location ~ \.php$ {
try_files $uri = 404;
expires off;
fastcgi_pass dvemedia;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
and put all your optimisation/tweaks (like fastcgi_buffers ...) in fastcgi_params file
I made a bad assumption by thinking my php-fpm socket would stay the same. After looking at the directory structure, my socket for 7.4 ended up being here.
fastcgi_pass unix:/var/run/php-fpm/www.sock;
That actually fixed it and everything worked. I gave bad information when I wrote the path for my socket was actually this.
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
#Latheesan answer would most likely have worked had I had given the correct information, minus the spelling mistake of course.

PHP not executed when in subdirecory with nginx

When going on example.com, example.com/foo/ or example.com/foo/bar/, PHP was working great. Then I tried to modify Nginx conf to have foo.example.com pointing on example.com/foo/ (the DNS is already configured). It works, but now when I access to foo.example.com/bar/, I can download foo/bar/index.php instead of executing it.
Here is the Nginx configuration:
server {
listen 80;
server_name *.example.com;
root /var/www/html/;
location / {
index index.html;
}
}
server {
listen 80;
server_name foo.example.com;
root /var/www/html/foo/;
location / {
index index.php;
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_pass web_fpm:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I know there are plenty of similar threads, but none of those I read worked.
The first server declaration catches the request and since you don't have a PHP location block in this server declaration, Nginx just outputs the file. You need to add a PHP location block to the first server declaration.

How can I make a 'GET' request in the same server that php project deployed?

Situation:
I deployed my php project as a web server in the machine A, using nginx and fastcgi, and the config file is as following:
server {
listen 80;
server_name alpha.kimi.com;
index index.html index.htm index.php;
root /alidata/www/;
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
location / {
root /www/admin/;
index index.php;
if (!-f $request_filename){
rewrite ^/(.+)$ /index.php?$1& last;
}
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
access_log /data/log/nginx/access/output.log;
error_log /data/log/nginx/access/error.log;
}
so when I make a 'GET' request from my local machine as:
curl http://alpha.kimi.com/app/redirect/taskpush?build=10&gcdata=1
there will be json returned
{"res":200,"msg":"success","extra":[]}
However when I made the same request in the machine A, it just hanged there, and returned nothing. I also tried:
curl http://localhost/app/redirect/taskpush?build=10&gcdata=1
and
curl http://localhost:9000/app/redirect/taskpush?build=10&gcdata=1
all not working. I don't know what is the problem.
You need to configure nginx to listen via localhost or 127.0.0.1 for it to work.
See http://nginx.org/en/docs/http/ngx_http_core_module.html#listen for full instructions.
You can add multiple listen statements eg:
listen localhost;
listen 127.0.0.1;
Also see this for more detail: https://serverfault.com/questions/655067/is-it-possible-to-make-nginx-listen-to-different-ports

How do I configure nginx rewrite rules to get CakePHP working on CentOS?

Hi somebody please help me out, I’m trying to setup a cakephp environment on a Centos server running Nginx with Fact CGI. I already have a wordpress site running on the server and a phpmyadmin site so I have PHP configured correctly.
My problem is that I cannot get the rewrite rules setup correct in my vhost so that cake renders pages correctly i.e. with styling and so on. I’ve googled as much as possible and the main consensus from the sites like the one listed below is that I need to have the following rewrite rule in place
location / {
root /var/www/sites/somedomain.com/current;
index index.php index.html;
# If the file exists as a static file serve it
# directly without running all
# the other rewrite tests on it
if (-f $request_filename) {
break;
}
if (!-f $request_filename) {
rewrite ^/(.+)$ /index.php?url=$1 last;
break;
}
}
http://blog.getintheloop.eu/2008/4/17/nginx-engine-x-rewrite-rules-for-cakephp
problem is these rewrite assume you run cake directly out of the webroot which is not what I want to do. I have a standard setup for each site i.e. one folder per site containing the following folders log, backup, private and public. Public being where nginx is looking for its files to serve but I have cake installed in private with a symlink in public linking back to /private/cake/
this is my vhost
server {
listen 80;
server_name app.domain.com;
access_log /home/public_html/app.domain.com/log/access.log;
error_log /home/public_html/app.domain.com/log/error.log;
#configure Cake app to run in a sub-directory
#Cake install is not in root, but elsewhere and configured
#in APP/webroot/index.php**
location /home/public_html/app.domain.com/private/cake {
index index.php;
if (!-e $request_filename) {
rewrite ^/(.+)$ /home/public_html/app.domain.com/private/cake/$1 last;
break;
}
}
location /home/public_html/app.domain.com/private/cake/ {
index index.php;
if (!-e $request_filename) {
rewrite ^/(.+)$ /home/public_html/app.domain.com/public/index.php?url=$1 last;
break;
}
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/public_html/app.domain.com/private/cake$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
}
Now like I said I can see the main index.php of cake and have connected it to my DB but this page is without styling so before I proceed any further I would like to configure it correctly. What am I doing wrong?
Thanks seanl
At a glance, your problem might be that you are not pointing nginx to the webroot of your app. Deploying to the root cake folder is really not the way to go under any web-server.
The following is a complete server-block I use running Cake apps. In reality I only have the first four lines and then include the rest from a separate file "cakephp.inc".
A note on the line "fastcgi_param SERVER_NAME $host;". This is because some of my apps use $_SERVER['SERVER_NAME'] and it does not have the same meaning in nginx as in Apache. If youe server has several server_name(s) defined nginx will always pass the first one to php.
server {
server_name cakeapp.example.com;
root /var/www/vhosts/cake/app/webroot;
access_log /var/log/nginx/cakeapp.access.log;
error_log /var/log/nginx/cakeapp.error.log;
listen 80;
rewrite_log on;
# rewrite rules for cakephp
location / {
index index.php index.html;
# If the file exists as a static file serve it
# directly without running all
# the other rewite tests on it
if (-f $request_filename) {
break;
}
if (!-f $request_filename) {
rewrite ^/(.+)$ /index.php?url=$1 last;
break;
}
}
location ~* \favicon.ico$ {
expires 6m;
}
location ~ ^/img/ {
expires 7d;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
fastcgi_param SERVER_NAME $host;
}
location ~ /\.ht {
deny all;
}
}
There's now official documentation on this issue, which I used and confirmed works.
The documentation states:
server {
listen 80;
server_name www.example.com;
rewrite ^(.*) http://example.com$1 permanent;
}
server {
listen 80;
server_name example.com;
# root directive should be global
root /var/www/example.com/public/app/webroot/;
index index.php;
access_log /var/www/example.com/log/access.log;
error_log /var/www/example.com/log/error.log;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
I got this working:
root DIR/app/webroot/;
location / {
index index.php index.html;
rewrite ^/$ /index.php?url=/;
if (!-e $request_filename) {
rewrite ^(/.*)$ /index.php?url=$1 last;
}
}
and then of course handlers for php and stuff...
It is not advisable to use 'IF' blocks inside a 'location' block.
Here is a more natural way to achieve the same, using regex locations.
In this example, CakePHP 2.x is the root app on a vhost (skipping common stuff like server_name , logs etc):
root /path/to/cakephp-2.x_root/app/webroot;
index index.php;
location ~ .+\.php$ {
try_files $uri =404; #handle requests for missing .php files
include fastcgi_params;
fastcgi_pass 127.0.0.1:7001; #the FPM pool port
}
location ~ ^/(.*) {
try_files $uri $uri/ /index.php?url=$1&$args;
}
Note that the .php location block is BEFORE the / location block. That's important because with regex locations, they are searched until the first match.
If you need to make it run in a sublocation, eg http://www.example.com/something/, here is how I managed to do it. First I had to do a symlink to trick nginx: extract cakephp-2.x somewhere, then in 'app/webroot' create a symlink to itself with the same name as the sublocation, e.g. 'ln -s ../webroot something' .
Then the following config works to access cackephp under /something/:
location ~ ^/something/.+\.php$ {
try_files $uri =404; #handle requests for missing .php files
root /path/to/cakephp-2.x_root/app/webroot;
include fastcgi_params;
fastcgi_pass 127.0.0.1:7001; #the FPM pool port
}
location ~ ^/something(?:/)(.*) {
root /path/to/cakephp-2.x_root/app/webroot;
index index.php;
try_files $uri $uri/ /something/index.php?url=$1&$args;
}
Symlinking can probably be avoided by using 'alias' istead of 'root' but I could not figure out how.
I had a bunch of issues setting up a CakePHP site that was running an older version of CakePHP 1.2 - going by the date of this post it might be around the time. I recently blogged about it and simply suggest upgrading or installing a fresh version of the Cake library and all problems went away.
Please use below code in
vi /etc/nginx/sites-available/domainname.com
server {
server_name cakeapp.example.com;
root /var/www/vhosts/cake/app/webroot;
access_log /var/log/nginx/cakeapp.access.log;
error_log /var/log/nginx/cakeapp.error.log;
listen 80;
rewrite_log on;
# rewrite rules for cakephp
location / {
index index.php index.html;
# If the file exists as a static file serve it
# directly without running all
# the other rewite tests on it
if (-f $request_filename) {
break;
}
if (!-f $request_filename) {
rewrite ^/(.+)$ /index.php?url=$1 last;
break;
}
}
location ~* \favicon.ico$ {
expires 6m;
}
location ~ ^/img/ {
expires 7d;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
fastcgi_param SERVER_NAME $host;
}
location ~ /\.ht {
deny all;
}
}
Its working for me.

Categories