I have a Slicehost slice for a dev server, with nginx and PHP.
I'm trying to get drupal running on localhost/drupal and a codeigniter app running on localhost/codeigniter.
I can get one or the other to work, but not both -- the rewrite and fastcgi seem to be interfering with one another.
Does anyone know how to have /drupal and /codeigniter both working, with rewrite rules (for SEF URLs), in separate folders in my /var/www?
Cheers.
Ok, you have to create a file (no extension needed) in /etc/nginx/sites-available that represents the name of your folder/domain (ex: drupal, yoursite.com).
Here's a sample file:
server {
server_name yourdomain.com;
root /var/www/yourdomain;
index index.php;
location / {
autoindex on;
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
}
In the sample above it will actually send url rewrites to $_SERVER['REQUEST_URI']. For more nginx rewrites, you can take a look at http://wiki.nginx.org/HttpRewriteModule for more reference.
Then you want to enable it by creating a symlink of this file in your /etc/nginx/sites-enabled folder
Example: # ln -s /etc/nginx/sites-available/yoursite /etc/nginx/sites-enabled/yoursite
Then restart/reload nginx
# services nginx reload
Related
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
I have been stucked with this for a while. We have a Symfony 2.8 project and I want to deploy it to the cloud. I have properly created the ElasticBeanstalk environment and it works! But I still need to access http://domain/app.php in order to make it work. I managed to delete web/ changing documentDirectory part from URL but app.php is still there.
I have also tried with a new brand project following this:
https://docs.aws.amazon.com/es_es/elasticbeanstalk/latest/dg/php-symfony-tutorial.html
But when I change it to public/ happens to me the same I need to use index.php in the URL
I have modified NGINX conf following:
https://community.bitnami.com/t/how-to-change-default-root/65639/9
https://symfony.com/doc/current/setup/web_server_configuration.html
This is what I added to /etc/nginx/nginx.conf.default
location / {
try_files $uri $uri/ /app.php$is_args$args;
}
location ~ \.php$ {
try_files $uri $uri/ =404;
fastcgi_pass unix:/var/run/php-fpm/www.sock;
fastcgi_index app.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
I have restart nginx service using
sudo service nginx restart
but it remains the same.
I have also tried renaming app.php to index.php just in case. But I need to add /index.php to the URL then.
Any idea?
Use the index nginx directive to make app.php the default handler of http://domain/ requests:
server {
...
index app.php;
# your locations here
}
There are some things you can try.
https://symfony.com/doc/2.8/setup/web_server_configuration.html#nginx
Here you can find a configuration for your project. There you can see that the location is rewritten to the app.php.
location ~ ^/(app_dev|config)\.php(/|$) {
so i would suggest to take the configuration and test it.
I have a project running under php, but I have a third party subfolder(download folder) that I want to add into the current project.
Meaning, root folder = testlaravel =>www.testlaravel.com
with sub folder = testlaravel/download => www.testlaravel.com/download
Is there anyway I can do this?
Are you using Apache or Nginx ? If Apache, you need to config your vhost file to serve Laravel from a sub-folder. Particularly, you need to configure that when testlaravel.com/download URI is requested, it should be served using /home/testlaravel/download/public directory (basically different root/home location).
You also need to use mod_rewrite to rewrite your URL requests to the sub-folder be served from index.php from above location (and also prettify URLs).
Similarly, your configuration in vhost for your main website will be different (so there will be 2 configurations, one for your main website and one for laravel requests)
The above should work just fine since its done same way in Nginx, for which I have included a full example of how its conf file should look
Here is how I setup-ed my location block which is working for me perfectly:
location ^~ /facebookschedule {
alias /home/netcans/facebookschedule/public;
try_files $uri $uri/ #foobar;
location ~ \.php {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(.*)$;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/wwww/facebookschedule/public/index.php;
}
}
location #foobar {
rewrite /facebookschedule/(.*)$ /facebookschedule/index.php?/$1 last;
}
Source: http://shubhank.gaur.io/setup-laravel-5-in-subfolder-with-nginx/
I am using the following configuration, which is simpler than most other published solutions, and which does not require any paths/folders to be hard-coded.
We simply prefix all requests with public/ and then remove it from SCRIPT_NAME so that the application can autodetect its environment correctly.
location ~ /myproject/(.*) {
rewrite /myproject(.*) /myproject/public/$1 break;
try_files $uri /myproject/public/index.php$is_args$args;
location ~ /index\.php$ {
include /etc/nginx/fastcgi.conf;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_param SCRIPT_NAME /myproject/index.php;
}
}
I'm trying to install 3 websites on a VPS running centos 6, NGINX, PHP-FPM and WordPress. I followed the instructions shared in this article :https://deliciousbrains.com/hosting-wordpress-yourself-setting-up-sites/ and i created the below configuration file in sites-available directory
server {
server_name 7symptoms.com;
access_log /var/www/html/7symptoms/logs/access.log;
error_log /var/www/html/7symptoms/logs/error.log;
root /var/www/html/7symptoms/public/;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
I tested the files using nginx test command and php-fpm is configured correctly. But when i try to access a simple hello world (hi.php) file on my website 7symptoms.com, i get 502 bad gateway or 404 file not found error. What's the problem with the above code?
You created config in "sites-available", now you should link this fie to "sites-enabled":
cd /etc/nginx/sites-enabled/ #please check that in centos this is correct path
ln -s ../sites-available/website_config_file .
Next look into log files and check, does your request are entering the correct website.
Next, if you are using nginx + php-fpm the error "502" means that php-fpm is not running or you wrote wrong path to socket. Please check that file exists: /var/run/php-fpm.sock and (using htop, top or ps) does php-fpm process is working.
Next - if you want to have 3 different websites, it is more secure to use 3 different user, and 3 different php-fpm config (for each website), it means there will be also 3 different unix sockets created.
i'm trying to configure nginx to serve several releases of one symfony project.
Releasefolder structure:
/var/www/my-project/releases/24/
/var/www/my-project/releases/46/
/var/www/my-project/releases/47/
I'd like to call a URL like "http://my-server/my-project/release/47" which should access /var/www/my-project/releases/47/web/app.php on the server.
I tried for a while now but couldn't find the final solution, any help would be great!
My last try:
server {
server_name my-server;
location / {
# try to serve file directly, fallback to app.php
try_files $uri $uri$2app.php$is_args$args;
}
location ~ /my-project/release/([-a-zA-Z0-9]+)/app\.php(/|$) {
root /var/www/my-project/releases/$1/web;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/app.php;
fastcgi_param HTTPS off;
error_log /var/log/nginx/my-project_$1_error.log;
access_log /var/log/nginx/my-project_$1_access.log;
# Prevents URIs that include the front controller. This will 404:
# http://domain.tld/app.php/some-path
# Remove the internal directive to allow URIs like this
internal;
}
}
This config produces following error:
[error] 2272#0: *169 rewrite or internal redirection cycle while internally redirecting to "/my-project/release/47app.phpapp.phpapp.phpapp.phpapp.phpapp.phpapp.phpapp.phpapp.phpapp.phpapp.php"
You are missing a root directive in your server container -- is /var/www the default on your architecture? Otherwise, add a root directive:
root /var/www;
try_files is failing for find a match and will rewrite the URL by appending app.php to it. The resulting URL does not match your other location block, so it gets rewritten again and again.
The final term on your try_files directive is wrong. You have $2 where /web/ should be (probably inherited from a previous experiment).
Using try_files to make a general rewrite is a bad idea, as any mistyped URL could lead to a rewrite loop. Try:
location / {
rewrite ^(/my-project/release/[-a-zA-Z0-9]+)$ $1/app.php;
}
I changed the config to this:
server {
server_name my-server;
root /var/www;
location / {
rewrite ^(/my-project/release/[-a-zA-Z0-9]+)$ $1/app.php;
}
location ~ /my-project/release/([-a-zA-Z0-9]+)/app\.php(/|$) {
alias /var/www/my-project/releases/$1/web;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/app.php;
fastcgi_param HTTPS off;
error_log /var/www/my-project/releases/$1/app/logs/nginx_error.log;
access_log /var/www/my-project/releases/$1/app/logs/nginx_access.log;
# Prevents URIs that include the front controller. This will 404:
# http://domain.tld/app.php/some-path
# Remove the internal directive to allow URIs like this
internal;
}
}
Now a call like "http://my-server/my-project/release/47" points correctly to "/var/www/my-project/releases/47/web/app.php".
The problem is now that the url "my-project/release/47" arrives at symfony router which can't find a route to this url.
What is now the right way to keep the url showing up like "//my-server/my-project/release/47" at browser but arrives at symfony as "//my-server/"?
Thank you all for your help, unfortunately i couln't get it working. I think i have to learn more about nginx configuration generally to be able to handle such problems, before i can use it in production.
My workaround for my problem is to create an own nginx conf per release. The file will be created at build process, is then transfered to the webserver, copied to /etc/nginx/conf.d/[releasename].conf and activated by sudo service nginx reload.
A deinstallation script within the release dir removes sources and config, when release no longer needed.