unable to access php file nginx - php

I am completely new to nginx .
I have project based on angular js which have a index.html, and on a certain event i perform a angular http request to x.php file and fetches the response from it.
Its running perfect on my local system and a apache based private hosting server.
I created a free tier ec2 instance and started a centos based linux instance on which i hosted the code and installed nginx .
Here is my nginx config
server {
listen 80;
server_name mydomain.co.in www.mydomain.co.in;
location / {
root /var/www/html/indm;
index index.php index.html index.htm;
try_files $uri/ $uri /index.php?$query_string =404;
}
location ~ \.php$ {
try_files $uri $uri/ /index.php?q=$uri&$args =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
add_header Access-Control-Allow-Origin *;
proxy_set_header Access-Control-Allow-Origin $http_origin;
}
error_page 500 502 503 504 /50x.html;
At first it was giving me 500 gateway error when the http request was performed on that php file. I check in the XHR request.Here is the error in nginx error.log
[error] 17193#0: *2 rewrite or internal redirection cycle while internally redirecting to "/index.php", client: 42.111.38.254
I searched a bit and changed appended a "=404" to try uri statement. But now it redirect to a 404 . I want to run that php file.
the server
Nginx server is running
php fpm is also running
Please help

The location ~ \.php$ block has no root. Move the root statement into the server block so that the same value is inherited by both location blocks.
For example:
server {
...
root /var/www/html/indm;
location / {
...
}
location ~ \.php$ {
...
}
...
}

Related

nginx + codeigniter configuration not working on CentOS 7?

I am new on nginx and try to find the issue on StackOverflow but not found the exact one on forums.
After successful installation of nginx, MySQL and php-fpm I test the php.info that was working fine.
I am moving the CodeIgniter project to the nginx from apache server. I edit the nginx.conf file that has code
server {
listen 80;
server_name 173.249.40.xxx;
# note that these lines are originally from the "location /" block
root /usr/share/nginx/html/ci;
index index.html index.htm index.php;
location /ci {
try_files $uri $uri/ /index.php;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
it was working fine for php.info like my path is http://173.249.40.xxx/ci/info.php but not working for CodeIgniter controller name like http://173.249.40.xxx/ci/index.php/Welcome when called. I try from
https://www.nginx.com/resources/wiki/start/topics/recipes/codeigniter/#
for CodeIgniter application but it also not work for me.
The CodeIgniter application path is '/usr/share/nginx/html/ci/;'
Please suggest me something. how can I fix it?
The codeigniter has it's own routing mechanism so in order to work you need from web server to pass everything you can't find to the index.php not to toss 404 messages.
So try to change your location / {...} block to the following:
location /ci/ {
# Check if a file or directory index file exists, else route it to index.php.
try_files $uri $uri/ /ci/index.php;
}
The above it's taken from the link in your question.

How to configure Nginx for Ember.js + Wordpress?

The scenario is that I'd like to use Wordpress as a backend API provider for our Ember.js frontend app.
The Ember.js frontend needs to be served from the root, and the Wordpress instance ideally would be reachable by going to a subdirectory. So for example on localhost it would be http://localhost and http://localhost/wordpress
On the disk the two are deployed in /srv/http/ember and /srv/http/wordpress respectively.
I was trying to assemble the configuration going by the example on the Nginx site:
https://www.nginx.com/resources/wiki/start/topics/recipes/wordpress/
The config:
http {
upstream php {
server unix:/run/php-fpm/php-fpm.sock;
}
server {
listen 80;
server_name localhost;
root /srv/http/ember;
index index.html;
try_files $uri $uri/ /index.html?/$request_uri;
location /wordpress {
root /srv/http/wordpress;
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass php;
fastcgi_split_path_info ^(/wordpress)(/.*)$;
}
}
}
However this is obviously not the correct solution.
Upon trying to access the address http://localhost/wordpress/index.php I get the following in the logs:
2016/05/01 17:50:14 [error] 4332#4332: *3 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /wordpress/index.php HTTP/1.1", upstream: "fastcgi://unix:/run/php-fpm/php-fpm.sock:", host: "localhost"
The recipe isn't clear about where to put the root directive for the location of wordpress. I also tried with adding index index.php, which doesn't help either.
(Serving the Ember app works fine.)
From your question it seems that the location ~ \.php$ block is used by WordPress alone. However, it needs a root of /srv/http in order to find the script files for URIs beginning with /wordpress under the local path /srv/http/wordpress.
As there are two locations which both use the same WordPress root, it is possibly cleaner to make /srv/http the default (that is, inherited from the server block) and move root /srv/http/ember; into a separate location / block.
server {
listen 80;
server_name localhost;
root /srv/http;
location / {
root /srv/http/ember;
index index.html;
try_files $uri $uri/ /index.html?/$request_uri;
}
location /wordpress {
index index.php;
try_files $uri $uri/ /wordpress/index.php?$args;
}
location ~ \.php$ {
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass php;
}
}
Notice that the default URI in location /wordpress is /wordpress/index.php and not /index.php as you originally had.
I have explicitly set SCRIPT_FILENAME as it may or may not appear in your fastcgi.conf file.
fastcgi_split_path_info has been removed as it is unnecessary in your specific case, and I think it would actually break WordPress the way you had it.

Vagrant setup for nginx load balancing with a php site

I've looked up a few topics on here around this, but none of the solutions I've found so far seem to work.
I have 3 boxes created via a Vagrantfile with puppet modules, which have nginx and php installed. I've created a simple webpage to output the host name statically, plus php info.
On the load balancer I have the following code for /etc/nginx/sites-available/127.0.0.1 (note this is now the default site and linked setup through my Vagrantfile)
# vagrant/puppet/modules/nginx/files/loadBalancer/127.0.0.1
upstream backend {
server 192.168.205.20; #ip of second machine
server 192.168.205.30; #ip of third machine
}
server {
listen 80;
server_name _;
root /var/www/app;
index index.php;
location / {
try_files $uri /index.php;
proxy_pass http://backend;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
}
The two additional hosts which host this web app, have the following file for their /etc/nginx/sites-available/127.0.0.1
# vagrant/puppet/modules/nginx/files/127.0.0.1
server {
listen 80;
server_name _;
root /var/www/app;
index index.php;
location / {
try_files $uri /index.php;
proxy_pass http://backend;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
}
However, this results in only one page coming up (the load balancers, it never alternates to the other two like it should).
I have also tried passing in the backend upstream as the fastcgi_pass but this causes a 502 bad gateway. Is there something I am misunderstanding as far how this should function? Any help would really be appreciated!

Error on Installing Wordpress on Nginx

I'm trying to install a wordpress site in a Linux VPS with LEMP Setup. So far I've done setting up the wordpress files and setting ownership for nginx user/group on the WP directory/files, but when I go to the address to access the installation page for WP (https://domain.tld/wp-admin/install.php), I end up with a php file download instead.
Here's my virtual host configuration for the WP site:
server {
listen 80;
server_name domain.tld;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
ssl on;
ssl_certificate /directory/to/crt;
ssl_certificate_key /directory/to/key;
server_name domain.tld;
root /var/www/html/domain.tld;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
include fastcgi_params;
}
}
EDIT: I tried it in Firefox, and it's acting differently than in Chrome. The install.php page instead ends up in an error page like this:
An error occurred.
Sorry, the page you are looking for is currently unavailable.
Please try again later.
If you are the system administrator of this resource then you should check the > error log for details.
Faithfully yours, nginx.
I got it working now. The error was on the virtual host's *.conf file. I got the directory for php-fpm sock wrong. So that was the reason the php is not working on the site, and instead just downloads the install.php file, and domain is ending up in error.
fastcgi_pass unix:/var/run/php5-fpm.sock; <---- I just got the directory wrong on this one.
Maybe just u forget to start your php-fpm
cd /usr/local/php7-chanxiao/etc/
../sbin/php-fpm

Nginx Wordpress Configuration, PHP file in theme directory is not passed to FastCGI

I am new to NGINX configs so bear with me. Below is my configuration which works fine for the whole site:
server {
listen ...;
server_name funkyoslo.no;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location / {
root /usr/share/nginx/funkyoslo.webbr.org/html;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/funkyoslo.webbr.org/html/;
}
# 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 /usr/share/nginx/funkyoslo.webbr.org/html/$fastcgi_script_name;
include fastcgi_params;
}
}
However, I am trying to load the file /wp-content/themes/funkyoslo/load-songs.php and it's giving me a 500 internal server error. I've checked the error logs and evidently the file isn't passed to FastCGI at all.
I tried adding the following block to no avail:
location ~ .*/wp-content/themes/funkyoslo/.*\.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/funkyoslo.webbr.org/html/wp-content/themes/funkyoslo/$fastcgi_script_name;
include fastcgi_params;
}
Any help is greatly appreciated!
Look I don't know what's wrong with your config but try this and hopefully it should work, this is the minimal config.
Notes:
Removed the index to the server block level, check link to know why
Removed root to server block level, check link to know why
Removed all extra config from the php block level, by trials I realised that I only need those two I've written.
Added http:// to fastcgi_pass, I don't know if it's really required but I got used to writting it this way.
Edit: Ok apparently I don't need the http://, I just checked.
-
server {
listen 80;
server_name funkyoslo.no;
root /usr/share/nginx/funkyoslo.webbr.org/html;
error_page 500 502 503 504 /50x.html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php$request_uri;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass http://127.0.0.1:9000;
}
}

Categories