Symfony + nginx virtual host configuration not working - php

Created a virtualhost for symfony application in local system
Here is the nginx config file
server {
listen 80;
server_name local.symfony;
root /home/guest/symfony_demo/web;
rewrite ^/app\.php/?(.*)$ /$1 permanent;
try_files $uri #rewriteapp;
location #rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
# Deny all . files
location ~ /\. {
deny all;
}
location ~ ^/(app|app_dev)\.php(/|$) {
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
fastcgi_index app.php;
send_timeout 1800;
fastcgi_read_timeout 1800;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
# Statics
location /(bundles|media) {
access_log off;
expires 30d;
try_files $uri #rewriteapp;
}
error_log /var/log/nginx/project_error.log;
access_log /var/log/nginx/project_access.log;
}
On load of app in browser its throwing an error
502 Bad Gateway Error:No input file specified.
Error caught from error.log file:
FastCGI sent in stderr: "Unable to open primary script: /home/guest/symfony_demo/web/app.php (No such file or directory)" while reading response header from upstream, client: 127.0.0.1, server: local.symfony, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "local.symfony"
Can anyone help me to configure symfony app to app_dev config file.
Any thoughts??

fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
This line is the problem. If you remove it, your error disappears. You may then have Opcache problems due to Symfony using symlinks to the current project.
With $realpath_root$fastcgi_script_name, the web server looks at the "real" path for the PHP script based on the root definition in your server block. Nginx must have read permissions to the path for this to work. Your code is in /home/guest/, if nginx is running as "www-data" give it permissions to your directories, or run nginx as the "guest" user (ignoring the security implications of this).

Why don't you start with configuration from official documentation (http://symfony.com/doc/current/cookbook/configuration/web_server_configuration.html#nginx) and when that works, you can try to add your custom configuration (caching of sttaic files ...) ?
This should work:
server {
listen 80;
server_name local.symfony;
root /home/guest/symfony_demo/web;
location / {
try_files $uri /app_dev.php$is_args$args;
}
location ~ ^/(app_dev|config)\.php(/|$) {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
}
error_log /var/log/nginx/project_error.log;
access_log /var/log/nginx/project_access.log;
}

This error happens when nginx is not able to find the php-fpm.sock file.
Can you make sure that the php-fpm.sock file is in the path as mentioned. I had to update fastcgi_pass like below
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
as my php-fpm.sock was there

Related

Running Wordpress/NGINX from different path

Im learning NGINX, so any help is really appreciated.
I have the frontend of a website running as the root of mysite.com, and now I want to run wordpress from mysite.com/blog.
My file structure is:
/srv/mysite/frontend
/srv/mysite/wordpress
this is the error i've been getting from the nginx logs
2020/03/29 00:09:03 [error] 23049#23049: *39 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: XXXXXXX, server: www.mysite.com, request: "GET /api HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.2-fpm.sock:", host: "mysite.com"
and this is my nginx config file so far
listen 80 default_server;
server_name www.mysite.com mysite.com;
charset utf-8;
location ^~ /blog {
root /srv/mysite/wordpress;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
location / {
root /srv/mysite/frontend/dist;
try_files $uri /index.html;
}
}
This error message shows either the wrong path of the sock file or the permissions. Make sure the php sock file exists in the path /run/php/php7.2-fpm.sock and change the permissions of the file.
For Debian
chown -R wwww-data:www-data /run/php/php7.2-fpm.sock
For Rhel
chown -R nginx:nginx /run/php/php7.2-fpm.sock
Also, you can try this config.
location /blog {
root /srv/mysite/wordpress;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;

Symfony not configured with Nginx server

I am new in PHP Symfony, I am trying configure symfony project with nginx but it is showing nginx 404 error, symfony path URL not working. only index.php file working.
Reference website:
https://www.nginx.com/resources/wiki/start/topics/recipes/symfony/
Here is my nginx code:-
server {
#listen [::]:80 default_server;
server_name dummyurl.com;
root /var/www/html/folder name;
index index.html index.php;
location / {
# try to serve file directly, fallback to index.php
try_files $uri /index.php$is_args$args;
}
# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(\.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
internal;
#fastcgi_param REQUEST_URI $uri?$args=404;
}
}
Here is the screenshot:
The problem is that you didn't set te server root to the public directory
server {
root /var/www/project/public;
}
Look at the documentation which Kamil Ścisłowski mentioned:
Offical Web Server Documentation

Install Laravel with nginx in Subdirectory

I try to enable PHP for only one subdirectory (the laravel directory) but I didn't manage to get this working. NGINX is always saying 404 File not found or php says "no input file specifed". What am I doing wrong?
This is my location config:
location /laravel {
root html/laravel/public;
index index.php index.html index.html;
try_files $uri $uri/ /index.php?$query_string;
location ~ \.php$ {
root html/laravel/public;
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME document_root$fastcgi_script_name;
#include fastcgi_params;
include fastcgi.conf;
}
}
UDPATE 1: It seems that nginx does not properly evaluate my location expressions:
2018/09/12 16:30:44 [error] 26476#24408: *1 CreateFile() "C:/Server/nginx/html/index.php" failed (2: The system cannot find the file specified), client: 127.0.0.1, server: localhost, request: "GET /laravel/ HTTP/1.1", host: "localhost"
This is the wrong path and at least the root of the / location:
location / {
root C:/Server/nginx/html;
index index.html index.htm index.php;
}
I tried to move the block but nothing changes.
UPDATE 2:
It seems that nginx is very buggy. The documentation states:
Checks the existence of files in the specified order and uses the first found file for request processing; the processing is performed in the current context. The path to a file is constructed from the file parameter according to the root and alias directives. It is possible to check directory’s existence by specifying a slash at the end of a name, e.g. “$uri/”. If none of the files were found, an internal redirect to the uri specified in the last parameter is made.
As my error log shows, The try_files directive does not respect the root path because it trys to open the file relative to another location block.
As #Richard pointed out in the linked Stackoverflow Thread, this seems to be a bug of nginx. For me this solution works with nginx:
location /laravel {
alias html/laravel/public;
index index.php index.html index.html;
try_files $uri $uri/ #nested;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
#fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi.conf;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
#include fastcgi_params;
}
}
location #nested {
rewrite /laravel/(.*)$ /laravel/index.php?/$1 last;
}
Source: https://serversforhackers.com/c/nginx-php-in-subdirectory
may be some changes and updates you need to apply:
If you want to put your laravel project in a subfolder on a server with ngnix-ubuntu 16-php.7.2, so here is update ngnix config :
1) your nested(subfolder) isn't inside your main folder
/var/www/main:
/var/www/nested:
then your config :
location /nested {
alias /var/www/nested/public;
try_files $uri $uri/ #nested;
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}
}
location #nested {
rewrite /nested/(.*)$ /nested/index.php?/$1 last;
}
2) your laravel-test folder (subfolder) inside your main :
/var/www/main:
/var/www/main/nested:
then your config :
location /laravel-test {
alias /var/www/main/laravel-test/public;
try_files $uri $uri/ #laravelTest;
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}
}
location #laravelTest {
rewrite /laravel-test/(.*)$ /laravel-test/index.php?/$1 last;
}

file not found for localhost sub-domain nginx wordpress

In the browser, I get "File not found". Server log and configuration are provided. I'm really not sure what to do at this point.
The Error:
2018/04/24 14:00:46 [error] 28717#28717: *9 FastCGI sent in stderr:
"Primary script unknown" while reading response header from upstream,
client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1",
upstream: "fastcgi://127.0.0.1:9000", host: "wasob.localhost"
wasob.localhost
server {
#Nginx should listen on port 80 for requests to yoursite.com
listen 80;
server_name wasob.localhost;
#Create access and error logs in /var/log/nginx
access_log /var/log/nginx/default.access_log;
error_log /var/log/nginx/wasob.error_log error;
root /var/www/html/wasob/;
#The homepage of your website is a file called index.php
index index.php;
location / {
try_files $uri $uri/ index.php
}
#Specifies that Nginx is looking for .php files
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
#if (!-f $document_root$fastcgi_script_name) {
# return 404;
#}
fastcgi_index index.php;
# SCRIPT_FILENAME parameter is used for PHP FPM determining
# the script name. If it is not set in fastcgi_params file,
# i.e. /etc/nginx/fastcgi_params or in the parent contexts,
# please comment off following line:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# Mitigate https://httpoxy.org/ vulnerabilities
fastcgi_param HTTP_PROXY "";
# include the fastcgi_param setting
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
}
}
The error and config are available at https://gist.github.com/skinuxgeek/4d4f86490f87805d1781782670551db9

PHP with gitlab bundled nginx not working

I've setup gitlab using the omnibus package on CentOS 7. I'd like to use the gitlab server to host additional websites. I've enabled custom nginx conf by adding the below code to /etc/gitlab/gitlab.rb
nginx['custom_nginx_config'] = "include /etc/nginx/conf.d/*.conf;"
I've also created conf files in /etc/nginc/conf.d. Static HTML files are working but when i try to run php scripts, I'm getting a File not found - 404 error.
Following is the nginx conf for php :
server{
listen 80;
server_name example.com;
root /var/www/vhosts/example;
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 /opt/gitlab/embedded/html;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include /opt/gitlab/embedded/conf/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
The following is the error log:
FastCGI sent in stderr: "Primary script unknown" while reading response from upstream, client x.x.x.x, server: example.com, request: "GET / HTTP/1.1", upsteam: "fastcgi://127.0.0.1:9000", host: "example.com"
Maybe your problem comes from your "location ~ .php$" config.
You already fix the first problem with gitlab omnibus by included the right fatscgi_params instead of the default. Now it seems to comes from the location config.
Try the following code for your configuration :
location ~ \.php$ {
#in your case maybe : /opt/gitlab/embedded/html
root [YOUR APP DIRECTORY];
try_files $uri $uri/ /index.php?$args;
index index.html index.htm index.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;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_intercept_errors on;
include /opt/gitlab/embedded/conf/fastcgi_params;
}
This fix works for me on a Debian 8 server.
Hope it could help.

Categories