NGNIX server settings for laravel 5.2 - php

I have developed a website in laravel and I want to install it on a local machine running nginx as a web server. Here is my config file
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root html;
index index.html index.php;
# Make site accessible from http://localhost/
server_name localhost;
location / {
root html;
index index.php;
}
location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME C:/nginx/html/$fastcgi_script_name;
include fastcgi_params;
}
}
When I browse http://localhost/mysite/ it runs C:/nginx/html/mysite/index.php
I want to execute C:/nginx/html/mysite/public/index.php file instead. How i can do that?

From your Nginx file I will assume your root directory for your project is html and all your Laravel files are in this directory.
If this is the case then you can either just go to http://localhost/mysite/public or alter your root directory in your Nginx configuration to html/public.
If that doesn't work, set root to the full path on your local machine to reach the public folder of your Laravel project.
Edit
Try:
root /c/nginx/html/mysite/public
OR
root C:/nginx/html/mysite/public

Below is my configuration:
server {
server_name laravel.mydomain;
root /home/me/domain/laravel.mydomain/public/;
location / {
try_files $uri /index.php$is_args$args;
}
location ~ ^/index\.php(/|$) {
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*|$);
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
internal;
}
}
Just change root to where your public folder is.
try_files makes sure that everything is served by index.php which is in the root path when the file doesn't directly exist on the file system.

You have to change only one line: Looks the final code in below
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root C:/nginx/html/mysite/public;
index index.html index.php;
# Make site accessible from http://localhost/
server_name localhost;
location / {
root html;
index index.php;
}
location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME C:/nginx/html/$fastcgi_script_name;
include fastcgi_params;
}
}
Please look the code carefully. I have changed ony
root html to root C:/nginx/html/mysite/public;

Related

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

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.

Creating new Lumen app on nginx throwing 404 when hiting IP Adress through browser

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.

Nginx/Fcgi: index.php issue with pseudo-alias location

Nginx 1.6.2 on Debian Jessie
I want to map all example.com/forum/ requests to /path/to/htdocs/phpbb and cut off the /forum/ part in the URI. Someone on Stackoverflow recommended the "rewrite" solution instead of "alias", because there are some bugs.
server
{
listen [::]:80;
server_name example.com;
root /var/www/html;
index index.php index.html;
#try_files $uri $uri/ =404;
location /forum/
{
root /path/to/htdocs/phpbb;
rewrite ^/forum/(.*)$ /$1 break;
location ~ .+\.php$
{
rewrite ^/forum/(.*)$ /$1 break;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
}
}
The example configuration works fine – example.com/forum/viewtopic.php executes the script /path/to/htdocs/phpbb/viewtopic.php – but example.com/ (index.php) doesn't work:
"/var/www/html/index.php" failed (2: No such file or directory)
After removing the "index" line from server block:
directory index of "/path/to/htdocs/phpbb/" is forbidden
After moving the "index" and/or "try_files" line(s) into the location block:
index.php served without passing over to php-fpm…
Ok, what's wrong with my config? Any hints?
Ok, alias is buggy (rewrite too…), but if you avoid try_files and use if instead (even if evil…) it should work!
server
{
listen [::]:80;
server_name example.com;
root /var/www/html;
location /forum/
{
alias /path/to/htdocs/phpbb/;
index index.php index.html;
location ~ "^(/forum/)(.+\.php)(/.+){0,1}$"
{
if (!-f $document_root$2)
{
return 404;
}
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_param SCRIPT_FILENAME $document_root$2;
fastcgi_param SCRIPT_NAME $1$2;
fastcgi_param PATH_INFO $3;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
}
}
phpinfo() looks fine, but one question remains: Is it secure?

NginX with FastCGI and WordPress in a subdirectory - "No input file specified"

I'm trying to migrate a WordPress blog to a subdirectory of my website (i.e. example.com/blog/). Using NginX and FastCGI, I've managed to get the entire WordPress site working on port 8080, but as soon as I attempt to apply rules to place it in /blog, I get "No input file specified"
I think because I can get PHP and WordPress working, I can assume that there are no issues with my installation or the permissions of the relevant folders.
This is what my virtual host looks like (I am running a rails site at "/"):
server {
listen 80;
server_name example.com localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location /blog/ {
root /home/deploy/www/blog.example.com/html;
index index.php;
try_files $uri $uri/ /blog/index.php;
}
location ~ \.php$ {
root /home/deploy/www/blog.example.com/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location / {
root /home/deploy/www/example.com/public;
passenger_enabled on;
index index.html index.htm;
}
I have also tried this configuration for the same result:
location ~ ^/blog/.*(\.php)?$ {
fastcgi_split_path_info ^(.+\.php)(.*)$;
root /home/deploy/www/blog.example.com/html;
try_files $uri =404;
# fastcgi_split_path_info ^(/blog/.+\.php)(.*)$;
#fastcgi_split_path_info ^(.+\.php)(/.+)$;
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# fastcgi_intercept_errors on;
fastcgi_pass 127.0.0.1:9000;
}
What am I doing wrong?
Are you using WordPress with Multisite?
I am not clear about your setup, but a tutorial from this list will surely help you: http://rtcamp.com/wordpress-nginx/tutorial
If you can share more details, I can guide you better.
From where does 8080 coming into picture? R u using Nginx with Apache??
"No input file specified" looks like an error related to incorrect location of PHP file...
Try changing
enter code herefastcgi_index index.php;
to
try_files index.php blog/index.php
Assuming 'blog' is a folder where you moved your WordPress.

Categories