server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /home/thinkshare/public/;
index index.php;
server_name domain_here;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Here is my current NGinx configuration - I cannot for the life of me figure out what exactly is happening to warrent a 502 Gateway Error on connection, domain or IP direct. Any thoughts?
edit
Removed Domain
I was able to finally find the answer at this link.
Effectively, fastcgi_pass in the nginx file as well as listen in the fpm file needs to be changed to 127.0.0.1:9000.
Permissions for the socket are incorrect and the reason why changing to top solved it
In Ubuntu 16.04 I get this error too. I resolved it by editing the
/etc/php/7.0/fpm/pool.d/www.conf
user = yourname
group = yourgroup
listen.owner = yourname
listen.group = yourgroup
and restart php7.0fpm by the use of $sudo service php7.0-fpm restart
This error is due to your path isn't in default /var/www which is owned by www-data when you in other directory, you should set it!
Related
I am trying to configure CentOS 7.3, Nginx and PHP 7.3, but I am getting:
502 Bad Gateway
nginx/1.10.3
This is site.com.conf:
server {
listen 80;
server_name server IP;
root /var/www/site.com/public;
index index.php index.html index.htm;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
If I upload static HTML to the public directory, I can see it, but if I try to load PHP files, i see that error.
I have /var/run/php-fpm/php-fpm.sock.
The solution was to change listen.owner and listen.groups in /etc/php-fpm.d/www.conf to nginx:
listen.owner = nginx
listen.group = nginx
Then restart with service php-fpm restart.
You can check for errors with: systemctl status php-fpm.service.
I installed debian 8 with nginx and php 7 for creating an endpoint with zendframework. When i follow the website i have to add these to my virtual host config in nginx. Like i did see the code below:
server {
listen 80;
listen [::]:80;
root /var/www/endpoint/html/public;
server_name my_ip;
location / {
index index.php
try_files $uri $uri/ #php;
}
location #php {
# Pass the PHP requests to FastCGI server (php-fpm) on 127.0.0.1:9000
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME /var/www/endpoint/html/public/index.php;
include fastcgi_params;
}}
But when i visit the website its downloading the index.php instead of executing the index.php.
I hope anyone can help me resolving this issue.
I think you need to replace the fastcgi_pass value with socket path instead of server address and port.
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
Then restart your php7-fpm by type this command
sudo systemctl restart php7-fpm
Thanks #Dolly-aswin,
Thx it worked replacing the 127.0.0.1 to the php-fpm sock
location ~ \.php$ {
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME /var/www/endpoint/html/public/index.php;
include fastcgi_params;
}
My ZendFramework 3 is now working on Debian 8 with Nginx (PHP 7).
I installed Nginx & PHP using this guide here:
Nginx install guide
yum install php php-mysql php-fpm
edited /etc/php-fpm.d/www.conf
listen = /var/run/php-fpm/php-fpm.sock
listen.owner = nobody
listen.group = nobody
listen.owner = nobody
listen.group = nobody
ran:
systemctl start php-fpm
/etc/nginx/conf.d/default.conf
server {
listen 80;
server_name <my servers IP here - removed>;
# note that these lines are originally from the "location /" block
root /usr/share/nginx/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
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;
}
}
I then tested
http://<my server ip>/phpinfo.php
and it worked!
I then installed pear
yum install php-pear
but now php files don't work, the browser tries to download them instead of running them.
I've tried:
rebooting the server
restarting nginx
restarting php-fpm
checking all the config files to make sure they as the same as above.
I'm completely stuck. I don't know what to check to get php work again. This is the first time I've installed Nginx. I've looked around on the net for answers and on here.
I'm running Centos 7
help :)
UPDATE:
I've tried a much more shortened config file:
server {
listen 80;
server_name <my servers IP here - removed>;
# note that these lines are originally from the "location /" block
root /usr/share/nginx/html;
index index.php index.html index.htm;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
but that didn't work either.
I also tried:
fastcgi_pass 127.0.0.1:9000;
instead of:
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
but that didn't work too.
UPDATE 2
i found out that if I go to:
http://my ip/phpinfo.php - it works!
but if I go to:
http://domainname/phpinfo.php - it tries to download the PHP file instead of running it.
How do I make php files run while using the domain name instead of the IP?
According your second update, you have problem with Listen directive. nginx listen only IP-address or Domain name that mentioned in Listen directive. Maximillian gives you right answer. It you put listen 80 in your configuration file, you'll solve your problem, but there would be wotk only one site, with this listen. If you would like to configure PHP on your domain you could configure listen yourdomain.com:80.
Have you tried to edit
/etc/nginx/sites-enabled/default ?
Then you need to edit it as follows.
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.php index.html index.htm;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
afterward, you have to restart php-fpm and nginx
service nginx restart && service php-fpm restart
Try with below config for location,
location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html/$fastcgi_script_name;
include fastcgi_params;
}
And do clear cache beforehand.
For your 2nd update try below in server block,
listen 127.0.0.1:8000; #your ip with port
server_name example.com; #domain name
Recently I also need to setup linux and php environment, so I strictly follow your step to setup it.
I encounter this two issue when I setup the LEMP environment,
a. nginx issue, post and answer
b. php-fpm issue, post and answer
I don't encounter the issue you face, so I show the environment for you, you may compare with yours.
Centos Version
vi /etc/centos-release
CentOS Linux release 7.3.1611 (Core)
Mysql version
mysql -u root -p
//enter password
Server version: 5.5.52-MariaDB MariaDB Server
Php version
php -v
PHP 5.4.16 (cli) (built: Nov 6 2016 00:29:02)
Pear info
pear list // refer to this post
Installed packages, channel pear.php.net:
=========================================
Package Version State
Archive_Tar 1.3.11 stable
Console_Getopt 1.3.1 stable
PEAR 1.9.4 stable
Structures_Graph 1.0.4 stable
XML_Util 1.2.1 stable
Nginx config file
vi /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name 192.168.236.129;
# note that these lines are originally from the "location /" block
root /usr/share/nginx/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
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;
}
}
info.php vi /usr/share/nginx/html/info.php
<?php phpinfo(); ?>
I am trying to create a plain Lumen application on Nginx server on Digital Ocean. Following the official documentation for Laravel, I followed everything as it is said until the Laravel Installation bit.
Just instead of this
composer create-project laravel/laravel /var/www/lumen/ 4.1
I used
composer create-project --prefer-dist laravel/lumen blog` instead.
The Lumen document seems on the directory now when I use ls.
However when I hit the IP address, or IPAdress/index.php, it shows
404 Not Found - nginx/1.4.6 (Ubuntu)
Also, this is my configuration for Virtual Host file (nano /etc/nginx/sites-available/default)`:
server {
listen 80 default_server;
root /var/www/laravel/public/;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
# pass the PHP scripts to FastCGI server listening on /var/run/php5-fpm.sock
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
What am I doing wrong or missing?
Also, if I try connect to the SSH directory with PhpStorm, I received an error:
ssh://root#46.101.172.134:22null /usr/bin/php
env: /var/www/Lumen: Permission denied
Process finished with exit code 126
Please also check this question for further info on this error..
Edit 2:
I tried changing my server to..
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
listen 443 ssl;
root /var/www/lumen/public;
index index.php index.html index.htm;
server_name IPAddress;
ssl_certificate /etc/nginx/ssl/nginx.crt; // I didn't set this
ssl_certificate_key /etc/nginx/ssl/nginx.key; // and this
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I used this guide to create an SSL Certificate on nginx. Then, hitting http://ipaddress gives me the Lumen/Laravel stuff.
I'm using Ubuntu 13 and I've installed nginx and php5-fpm; before that I had PHP5 and apache installed; which I removed
/etc/php5/fpm/pool.d/www.conf
user = www-data
group = www-data
listen = /var/run/php5-fpm.sock
listen.owner = www-data
listen.group = www-data
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
chdir = /
nginx config file :
upstream php {
server unix:/var/run/php5-fpm.socket;
}
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm index.php;
# Make site accessible from http://localhost/
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
When I try
http://local.host/info.php
It downloads the info.php file instead of executing the file
But when I try:
http://my.ip.address/info.php
it shows the phpinfo() function
where is the problem ?
You could try changing you config a bit:
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
index index.html index.htm index.php;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
//fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_pass localhost:9000;
fastcgi_index index.php;
include fastcgi_params;
}
}
and your www.conf:
listen = localhost:9000
;listen = /var/run/php5-fpm.sock
Change line to server_name localhost local.host 127.0.0.1; (are you sure http://local(dot)host is not a typo?)
Check /etc/hostnames and if it differs from above, add it in too. Also check that you dont have another entry server{ ... } entry that listens to same port 80.
Check the folder /etc/nginx/sites-enabled/, the default filename also contains server{...} parameters. The http{...} block is found in /etc/nginx/nginx.conf and sometimes may contain the server{...} block.