Nginx set subdomain to work with php - php

What I want to achive is this:
Create a subdomain my.domain.com
Execute an index.php on it
I created a file called my.conf and put it in /etc/nginx/conf.d/my.conf
Inside of it I put the nex code:
server{
listen 80;
listen [::]:80;
root /var/www/my;
index index.php index.html;
server_name my.domain.com;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
I created an A record Type: A, Name: my, Value: my IP
I put a file inside /var/www/my/ called index.php and wrote a small echo;
I did a nginx -t and everything was fine.
Then sudo service nginx restart and sudo systemctl reload nginx.
Everytime when I visit my.domain.com I get the content from domain.com. It looks like my.conf doesn't do what it should do because I don't see the index.php content on my subdomain.
Any ideas what am I doing wrong?
Edit: I also use Cloudflare.
Thanks

Related

Aws ec2 directory does html take priority over php if I upload both?

My question is a directory question. If I install a php script in a directory and I install an index.html file. Will the html file take priority when the domain is visited?
What I'm trying to do if work on a php script live by typing in www.domainname.com/index.php but when visitors type in the www.domainname.com I want it to take them to the html index.html. is this possible?
Another question, I installed a domain name and installed a placeholder index.html in the directory, I then created a subdirectory like www.domainname.com/restaurants/. This directory has an index.html file in it but it's not showing up in the browser. I created the directory in file zilla. Could there be a way I can find out why this is?
you may update nginx settings in /etc/nginx/sites-available/default
something like this:
server {
listen 80;
server_name your_domain www.your_domain;
root /var/www/your_domain;
index index.html index.htm index.php; // "add index.php here
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}

NGINX alias/root or rewrite for a link loading from a different directory [duplicate]

I want to serve multiple Laravel apps in a single nginx server, the first one has a root directory in /var/www/html/app1, the second one has /var/www/html/app2, and so on. The index.php file of each app is in a subdirectory named /public.
Whenever user calls http://www.mywebsite.com/app1, nginx shoulds return the app1 and if user calls http://www.mywebsite.com/app2, nginx shoulds return the app2.
My current nginx conf file is as below:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location /app1 {
root /var/www/html/app1/public;
index index.php;
}
location /app2 {
root /var/www/html/app2/public;
index index.php;
}
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
# try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}
}
But, nginx always returning 404 page result. What's going wrong here?
During one of the deployment on linux server, I came across some sort of your challenge. It was as follow
<base_url> : One Laravel project needs to served on this.
<base_url>/<sub_url> : Another Laravel project needs to be served on this.
Of course this can be extended to any number of Laravel projects which follows <base_url>/<unique_sub_url> concept.
Now let's dive into actual implementation
# Nginx.conf
# App 1(Path: /var/www/html/app1, Url: http://www.mywebsite.com)
# App 2(Path: /var/www/html/app2, Url: http://www.mywebsite.com/app2)
server {
# Listing port and host address
# If 443, make sure to include ssl configuration for the same.
listen 80;
listen [::]:80;
server_name www.mywebsite.com;
# Default index pages
index index.php;
# Root for / project
root /var/www/html/app1/public;
# Handle main root / project
location / {
#deny all;
try_files $uri $uri/ /index.php?$args;
}
# Handle app2 project, just replicate this section for further projects app3, app4
# by just replacing app2 with appropriate tag(app3/app4)
location /app2 {
# Root for this project
root /var/www/html/app2/public;
# Rewrite $uri=/app2/xyz back to just $uri=/xyz
rewrite ^/app2/(.*)$ /$1 break;
# Try to send static file at $url or $uri/
# Else try /index.php (which will hit location ~\.php$ below)
try_files $uri $uri/ /index.php?$args;
}
# Handle all locations *.php files (which will always be just /index.php)
# via factcgi PHP-FPM unix socket
location ~ \.php$ {
# At this point, $uri is /index.php, $args=any GET ?key=value and $request_uri = /app2/xyz.
# But we don't want to pass /app2/xyz to PHP-FPM, we want just /xyz to pass to fastcgi REQUESTE_URI below.
# This allows laravel to see /app2/xyz as just /xyz in its router.
# So laravel route('/xyz') responds to /app2/xyz as you would expect.
set $newurl $request_uri;
if ($newurl ~ ^/app2(.*)$) {
set $newurl $1;
root /var/www/html/app2/public;
}
# Pass all PHP files to fastcgi php fpm unix socket
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# Use php fpm sock which is installed on your machine like php7.2, php5.6
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
# Here we are telling php fpm to use updated route that we've created to properly
# response to laravel routes.
fastcgi_param REQUEST_URI $newurl;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
# Deny .ht* access
location ~ /\.ht {
deny all;
}
}
Note: When we're using session based laravel setup, all the route generator functions(url(), route()) use hostname www.mywebsite.com as root url, not www.mywebsite.com/app2. To resolve this issue please do following changes in laravel app.
Define APP_URL in .env file as APP_URL="www.mywebsite.com/app2"
Go to RouteServiceProvider which is located at app/Providers/RouteServiceProvider and force laravel to use APP_URL as root url for your app.
public function boot()
{
parent::boot();
// Add following lines to force laravel to use APP_URL as root url for the app.
$strBaseURL = $this->app['url'];
$strBaseURL->forceRootUrl(config('app.url'));
}
Update: Make sure to run php artisan config:clear or php artisan config:cache command to load the updated value of APP_URL.
For windows: Multiple Laravel Applications Using Nginx - Windows

Configure multi site on nginx server through IP using Digital Ocean

Problem: Unable to hit two websites handled under one nginx server i.e. <<ip-address>> & <<ip-address>>/web2
Configuration on Digital Ocean:
1 Droplet / Ubuntu 18 / LEMP
I have two test PHP website in the CodeIgniter framework
Folder config for 1st Website: /var/www/html/web1/
Folder config for 2nd Website: /var/www/html/web2/
Nginx Server Block configuration for two sites
web1.com
server {
listen 80;
root /var/www/html/web1;
index index.php index.html index.htm index.nginx-debian.html;
server_name <<ip-address>>;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
web2.com
server {
listen 80;
root /var/www/html/web2;
index index.php index.html index.htm index.nginx-debian.html;
server_name <<ip-address>>/web2;
location /web2/ {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
I am totally new to nginx server, I do according to the documentation provided by a community under a digital ocean.
Please help!
Thanks.
What you're trying to do is not how nginx works out of the box. It could, with a lot of fiddling, end up working that way, but I don't think it's worth the effort.
See, nginx configuration expects server_name to be either a FQDN (fully qualified domain name) or an IP address, but not a full URL with path.
In your case, the request for ip-address/web2 is probably actually matching web1's config (so pointing you to /var/www/html/web1/web2/ which doesn't exist)
Best way to work this out (assuming you want to keep both sites on the same droplet): get a FQDN for each site. It could be a subdomain for a domain you already have (i.e. web1.sharad.com and web2.sharad.com)... Then on each of nginx's config files use the appropriate server name (web1.sharad.com and web2.sharad.com), check for typos and errors with sudo nginx -t and if all is OK restart nginx with sudo systemctl restart nginx

nginx + wordpress in subfolder + debian configuration

In a subfolder on a domain I want to install a wordpress blog. I use nginx. The URL to access the blog should be like this: example.com/blog
site config looks as follows:
server {
listen 80;
listen [::]:80;
root /var/www/example.com/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name example.com www.example.com;
location /blog {
alias /var/www/example.comblog/html;
index index.php;
try_files $uri $uri/ /blog/index.php?q=$uri&$args;
}
location ~ /blog/.+\.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;
}
location ~ /\.ht {
deny all;
}
}
The wordpress files reside in the folder
/var/www/example.comblog/html. When accessing example.com/blog,
the browser shows a 404 error.
In /etc/php5/fpm/php.ini I adapted this: cgi.fix_pathinfo=0
nginx version: nginx/1.6.2
/var/log/nginx/error.log does not show anything of interest
UPDATE 1:
After setting error logging to debug, (among others) the following lines appear. Maybe this helps:
open index "/var/www/example.comblog/html/index.php"
internal redirect: "/blog/index.php?"
rewrite phase: 1
test location: "/blog"
test location: ~ "/blog/.+\.php$"
using configuration "/blog/.+\.php$"
http script var: "/blog/index.php"
trying to use file: "/blog/index.php" "/var/www/example.com/html/blog/index.php"
The internal redirect seems incorrect? And in the last line there should be /var/www/example.comblog/html/blog/index.php instead of /var/www/example.com/html/blog/index.php. I suspect this is the reason for the 404. Because the index.php does not exist at /var/www/example.com/html/blog/index.php.
Update 2:
Okay there seems to be a long standing issue with using alias together with try_files.

Nginx multiple projects in one domain blank page laravel

This is my config here for Nginx. I have a domain named tstdmn and two Laravel projects first tstdmn.com project and second florist project I want to deploy florist project into the tstdmn.com/florist, I set it all but it returns a blank page at tstdmn.com/florist what's my issue here?! And I know the problem is with my Nginx configuration because I switch the florist project to the main project and it works, it's not from my Laravel configurations
root /var/www/html/tstdmn.com/public;
# Add index.php to the list if you are using PHP
index index.html index.php index.htm index.nginx-debian.html;
server_name tstdmn.com www.tstdmn.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$query_string;
}
location ^~ /florist {
alias /var/www/html/florist/florist_backend/public;
try_files $uri $uri/ #laravel1;
location ~ \.php {
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(.*)$;
include /etc/nginx/fastcgi_params;
}
}
location #laravel1 {
rewrite /florist/(.*)$ /florist/index.php?/$1 last;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.1-fpm.sock;
}
And so another route of project show's blank like /register
I solved it by someone's solution at laracasts:
using subdomain instead of routing
server { listen 80; listen [::]:80;
root /var/www/html/florist/florist_backend/public;
# Add index.php to the list if you are using PHP
index index.html index.php index.htm index.nginx-debian.html;
server_name florist.tstdmn.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$query_string;
}
..... and some code not related
address of question in laracast https://laracasts.com/discuss/channels/servers/nginx-multiple-projects-in-one-domain-errors

Categories