how do i setup nginx websites as in Xampp - php

I am trying to migrate from XAMPP to Nginx webserver. I used to have multiple websites running with Xampp with each web folder residing under xampp/htdocs folder. so for example, I will access sites(yii web sites) as [code]http://myserver/site1[code] and [code]http://myserver/site2[code] and they would be accessed from the site1 and site2 folders under htdocs. But I am having trouble setting this up in nginx. I have setup the default configuration with the root folder as /etc/share/nginx/www (and site1 and site2 folders are under www) and when i access the same way with nginx, the webpage gives an error saying "No Input file specified". I understand that multiple sites are setup in nginx with different domain names and different root folders, but is it possible to have the xampp like configuration? Because i am testing this on my local network and i do not want to setup multiple domain names for this.

Config for latest nginx versions is pretty simple. First you should edit nginx/conf/nginx.conf and make sure you have something like include vhosts/*.conf; inside http { section. That will make nginx look for extra configs under vhosts.
Also it's a good idea to declare the following in http { as well not to repeat it for each individual config:
gzip on;
charset utf-8;
index index.php index.htm index.html;
Then in nginx/conf/vhosts/mydomain.com.conf:
server {
listen 80;
server_name mydomain.com;
root /var/www/mydomain.com/www;
location / {
try_files $uri $uri/ /index.php?$args; # Redirect everything that isn't real file to index.php including arguments.
}
location ~ \.php$ {
include fastcgi.conf; # that's if you have one of latest versions of nginx. If not, see below
fastcgi_pass 127.0.0.1:9000; # or pass through socket if it's how you've configured php-fpm
}
location ~ /\.(ht|svn) {
deny all;
}
}
In case you don't have fastcgi.conf here it is:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param HTTPS $https if_not_empty;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
Now the only thing left is to add mydomain.com to your hosts file so it's recognized locally.

That's simple.
Drop your databases, compress them, import them again OR use mysql -u root -p BASENAME < /the/path/BASEDROP.sql
Then simply move your default dir like /var/www/html and set permissions to 0777 or less by using chmod 0777 -R /the/path and also use chown www-data:www-data -R /the/path AND THEN reboot your system.

Well, after struggling through I finally found the ideal configuration. I understand this is not a recommended setup, but this is the give problem for me (nginx with multiple sites as subdirectories) and had to find a solution for it.
location /Site1/ {
root /usr/share/nginx/www/Site1;
try_files $uri $uri/ /index.php?$query_string;
}
# the images need a seperate entry as we dont want to concatenate that with index.php
location ~ /Site1/.+\.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
root /usr/share/nginx/www/Site1;
}
# pass the PHP scripts to FastCGI server
location ~ /Site1/.+\.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
allow 127.0.0.1;
# # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# # With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
}
location /Site3/ {
root /usr/share/nginx/www/Site3;
}
# pass the PHP scripts to FastCGI server
location ~ /Site3/.+\.php$ {
allow 127.0.0.1;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
#we are directly using the $request_filename as its a single php script
fastcgi_param SCRIPT_FILENAME $request_filename;
}
More details on this blog, http://programmersjunk.blogspot.com/2013/11/nginx-multiple-sites-in-subdirectories.html

Related

CodeIgniter 3, Nginx rewrite how to?

I installed CodeIgniter 3 after a long time on PHP-fpm and nginx (Ubuntu). Previously I had always used CodeIgniter on Windows and configuring it on Windows and Apache it was a piece of cake.
Now I wanna install it on nginx, because I wanna use nginx-push-stream-module, which isn't possible from apache.
Now when I'm configuring it, its not working.
If I type localhost/myexample.com or localhost/myexample.com/index.php it works (myexample.com is the name of that directory)
but when I try to access
localhost/myexample.com/welcome
or
localhost/myexample.com/welcome/index
or
localhost/myexample.com/index.php/welcome
or
localhost/myexample.com/index.php/welcome/index
it doesn't work in any of the 4 cases (with or without index.php)
My root directory is /var/www/html/myexample.com
I tried all of the rewrite settings available online (including the following settings) from different blog posts etc (as I'm not used to nginx myself)
server {
server_name myexample.com;
root /var/www/html/myexample.com/;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php;
}
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
expires 15d;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/myexample.com/index.php;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
}
Edit: I also tried the method mentioned at Nginx's official website, but that's also not working.
You should modify your /etc/hosts and add this line:
127.0.0.1 myexample.com
And after that use myexample.com or myexample.com/welcome to access your CodeIgniter site
First - confirm what the server name is going to be. Set that in the your hosts file, then confirm the web root.
So in /etc/hosts add
127.0.0.1 myexample.com
Then your index.php file should be in
/var/www/html/myexample.com/
You should get CI up and working on the url
http://myexample.com

FastCGI sent in stderr primary script unknown while reading response header from upstream

I have in nginx.conf
upstream php-fpm7.0
{
server unix:/run/php-fpm/php7.sock;
}
I have in conf.d/default.conf
location ~ \.php$ {
include php-fpm;
}
I have in php-fpm
fastcgi_pass php-fpm7.0;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
# Additional variables
fastcgi_index index.php;
But i get error 404.
When i comment
#location ~ \.php$ {
# include php-fpm;
#}
files are available
Error even if
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/php7.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
FastCGI sent in stderr: "Primary script unknown" while reading
response header from upstream
The same error if not using socket:
fastcgi_pass 127.0.0.1:9000;
Centos 7
Update 1.
After i used:
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
fastcgi_param SCRIPT $fastcgi_script_name;
fastcgi_param FOLDER $document_root;
fastcgi_param FOLDER_SCRIPT $document_root$fastcgi_script_name;
I got $_SERVER['SCRIPT'] /index.php
and $_SERVER['FOLDER'] /home/www/m-a-x/www
and $_SERVER['FOLDER_SCRIPT'] /home/www/m-a-x/www/index.php
When i back
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
all works.
Mystery.
You can always check nginx variables by adding them to return statement, for example:
location / {
return 200 $document_root$fastcgi_script_name;
}
Open the URL, a file will be downloaded, file will contain variables
returned by nginx. For example /srv/www/index.php
Check that nginx/php-fpm user group has access to the file:
sudo -u www stat /srv/www/index.php
Set permissions to read and execute the files for the www user group all along the path.
chmod g+x /srv
chmod g+x /srv/www
chmod g+x /srv/www/index.php

Nginx rewrite path to query string

I use nginx (php-fpm) and fatfree framework. I need some redirect logic in my routing engine. It looks like:
http://example.net/page/...
Instead of dots there could be something like:
another.net/someurl
http://another.net/?local_query
another.net/path/to/article.html
It breaks nginx logic and I see 404 error.
My nginx config looks simply:
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include /etc/nginx/conf.d/fastcgi_params.conf;
fastcgi_param SCRIPT_FILENAME /var/www/$main_host/www$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT /var/www/$main_host/www;
fastcgi_param PHP_ADMIN_VALUE upload_tmp_dir=/var/www/$main_host/tmp/upload;
fastcgi_param PHP_ADMIN_VALUE session.save_path=/var/www/$main_host/tmp/sessions;
}
So when I use:
http://example.net/page/another.net/path/to/article.html
Nginx tries to find file in filesystem with name article.html as I understand. How to write rule to ignore any symbols when there is keyword page after domain?
Solution #1:
Probably it doesn't work because there is no fastcgi_pass directive inside /etc/nginx/conf.d/fastcgi_params.conf.
If so, all you need is simply add it to your \.php$ location. In general case it looks like:
fastcgi_pass 127.0.0.1:9000;
but this line might be different, depending on how PHP-FPM is configured.
Solution #2:
Your current configuration makes Nginx pass request to PHP-FPM only if URI ends with .php.
To make Nginx do it when URI starts with /page/ as well, you should write a new location with almost the same content (except for SCRIPT_FILENAME line) and (important!) put it before existing \.php$ location.
Copying location content would cause a code duplication, and I would recommend write the config in a slightly different way:
fastcgi_param SCRIPT_FILENAME /var/www/$main_host/www$my_script_name;
fastcgi_param DOCUMENT_ROOT /var/www/$main_host/www;
fastcgi_param PHP_ADMIN_VALUE upload_tmp_dir=/var/www/$main_host/tmp/upload;
fastcgi_param PHP_ADMIN_VALUE session.save_path=/var/www/$main_host/tmp/sessions;
include /etc/nginx/conf.d/fastcgi_params.conf;
location ~ /page/ {
fastcgi_pass 127.0.0.1:9000;
set $my_script_name /path/to/file.php;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
set $my_script_name $fastcgi_script_name;
}
In this case you will have 2 scenarios:
if URI starts with /page/, Nginx will run /var/www/$main_host/www/path/to/file.php.
if URI ends with .php, Nginx will run requested php file, as usual.
Solution #3:
Leave your \.php$ location as is and put the following location before it:
location ~ ^/page/ {
include /etc/nginx/conf.d/fastcgi_params.conf;
fastcgi_param SCRIPT_FILENAME /var/www/$main_host/www/path/to/file.php;
fastcgi_param DOCUMENT_ROOT /var/www/$main_host/www;
fastcgi_param PHP_ADMIN_VALUE upload_tmp_dir=/var/www/$main_host/tmp/upload;
fastcgi_param PHP_ADMIN_VALUE session.save_path=/var/www/$main_host/tmp/sessions;
}
Just replace /path/to/file.php with real path to file which should handle such requests.

NGNIX - look in multiple directories for a .php file and then excecute it

I have .php files in multiple directories (/jobs/marketing/, /jobs/content/ etc) that need to map cleanly to /jobs/name-of-file.php.
For example hitting the url:
/jobs/digital-marketing
needs to map to:
/jobs/marketing/digital-marketing.php
It's safe to assume the file name of each php file is unique across directories.
my current nginx setup is the following:
location /jobs {
expires max;
add_header Cache-Control public;
add_header Pragma public;
rewrite ^/jobs[\/]?$ /marketing/jobs.php last;
location ~* ^/jobs/([\-a-z0-9]*)$ {
try_files /marketing/jobs/engineering/$1.php
/marketing/jobs/marketing/$1.php
/marketing/jobs/business-development/$1.php
/marketing/jobs/content/$1.php;
}
location ~ ^/.+\.php($|/) {
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
include fastcgi_params;
fastcgi_read_timeout 3000;
}
}
This seems pretty close, except a plain text version of the file gets downloaded instead of nginx redirecting to it. I think I need to somehow catch the correct file with another location block, but I nothing seems to be working (that might not even be the correct approach).
Any ideas about how to achieve this? Or a better approach perhaps?
Thanks.
I belive that you are using try_files a bit wrong. What it does is
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.
You have one context with location ~* ^/jobs/([-a-z0-9]*)$ and another with location ~ ^/.+.php($|/)
So you are finding files but not processing them through PHP as your processing is in another context.
Therefore I belive you should add php processing in your try_files location.
location ~* ^/jobs/([\-a-z0-9]*)$ {
try_files /marketing/jobs/engineering/$1.php
/marketing/jobs/marketing/$1.php
/marketing/jobs/business-development/$1.php
/marketing/jobs/content/$1.php;
...
fastcgi_param ...;
fastcgi_pass ...;
}
To not write same config for both /jobs and .php locations you can cut it to file and include like you do with include fastcgi_params;
So ok, if your actual location directs to correct files - we're half way there. At this moment nginx is trying to download files - let's try to add another location directing those files to PHP parser - this one works for me:
location ~ ^/.+\.php($|/) {
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
include fastcgi_params;
fastcgi_read_timeout 3000;
}
Add this piece of code after your location and let me know if this works for you. You can read more about nginx with PHP here

Magento on Nginx - Configuration [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm working on an nginx.conf for running Magento, the site mostly works, magento is run using php-fpm.
But some parts of it are still not working, and I've tried every wiki, blog, etc around the web.
My problem is that where ever I have a Javascript pop-up on CMS pages and blocks, mainly the tiny_mce WYSIWYG editor, (/js/tiny_mce/plugins/advimage/image.htm etc) they open a page not found.
I don't know what should I do so this editor displays correctly.
Also, the downloader doesn't display.
It seems that each of these use its own index.php inside a different folder than root, so should I change the index to that?
like $document_root/downloader/index.php ?
I HIGHLY suggest you read and follow the nginx primer by Martin Fjordvald.
I use the following configuration for Magento. It not only works great, it also turns off access_log for images, etc. and has a special php-fpm configuration. Please note that the server root is specified within the server block. Several configuration files incorrectly specify it within a location block.
Magento nginx configuration file:
(Be sure to replace all paths and domain names accordingly)
server {
listen 80;
#listen 443 default ssl;
server_name DOMAIN.COM;
#rewrite requests to www
rewrite ^ $scheme://www.DOMAIN.COM$request_uri permanent;
}
server {
listen 80;
#listen 443 default ssl;
#ssl_certificate /path/to/ssl.crt;
#ssl_certificate_key /path/to/ssl.key;
server_name www.DOMAIN.COM;
# most likely /var/www/...
root /path/to/files;
include /etc/nginx/restrictions.conf;
location / {
index index.php;
if ($request_uri ~* "\.(ico|css|js|gif|jpe?g|png)$") {
access_log off;
expires max;
}
try_files $uri $uri/ #handler;
}
# protect directories
location /app/ {
deny all;
}
location /includes/ {
deny all;
}
location /lib/ {
deny all;
}
location /lib/minify/ {
allow all;
}
location /media/downloadable/ {
deny all;
}
location /pkginfo/ {
deny all;
}
location /report/config.xml {
deny all;
}
location /var/ {
deny all;
}
location /var/export/ {
# restrict access to admins
auth_basic "Restricted";
auth_basic_user_file htpasswd;
autoindex on;
}
location #handler {
rewrite ^(.*) /index.php?$1 last;
}
# include php specific configuration
include /etc/nginx/php.conf;
}
This is a php-fpm specific configuration file which intercepts error codes and splits the path info correctly so you have access to the correct path parts in PHP. I also use a Unix socket rather than a port due to performance improvements. Also note that you don't need to repeat the fastcgi_params already specified in fastcgi_params.
fastcgi_intercept_errors on;
# this will allow Nginx to intercept 4xx/5xx error codes
# Nginx will only intercept if there are error page rules defined
# -- This is better placed in the http {} block as a default
# -- in that virtual host's server block
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# A handy function that became available in 0.7.31 that breaks down
# The path information based on the provided regex expression
# This is handy for requests such as file.php/some/paths/here/
include fastcgi_params;
fastcgi_index index.php;
fastcgi_pass unix:/var/run/phpfpm.sock;
}
My fastcgi_params configuration file is optimized for a small server (<1GB RAM). Be sure to adjust yours according to your server's performance:
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
fastcgi_connect_timeout 90;
fastcgi_send_timeout 180;
fastcgi_read_timeout 360;
fastcgi_buffer_size 1024k;
fastcgi_buffers 8 512k;
fastcgi_busy_buffers_size 1024k;
fastcgi_temp_file_write_size 1024k;
fastcgi_intercept_errors on;
fastcgi_pass_header *;
We have magento installed to mydomain.com/store, and we use next config for nginx:
server {
listen <needed ip(s)>:80;
server_name mydomain.com;
root /www/mydomain;
location ~ /\. {
deny all;
}
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {
access_log off;
expires 30d;
}
location /store/ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/store/index.php;
fastcgi_param SCRIPT_NAME /store/index.php;
fastcgi_index index.php;
}
location /store/static/ { }
location /store/skin/ { }
location /store/media/ { }
location /store/errors/ { }
location ~* /store/errors/.*\.xml$ { deny all; }
location ~* /store/errors/.*\.phtml$ { deny all; }
location ~* /store/errors/.*\.php$ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME /store/errors$fastcgi_script_name;
fastcgi_index index.php;
fastcgi_read_timeout 600;
}
location /store/js/ { }
location ~* /store/js/.*\.php$ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/store/js$fastcgi_script_name;
fastcgi_param SCRIPT_NAME /store/js$fastcgi_script_name;
fastcgi_index index.php;
fastcgi_read_timeout 600;
}
}
you have to rewrite all .htaccess rules to ngnix configuration to get this working. Worth to read http://www.nbs-system.co.uk/blog-2/magento-optimization-howto-en.html

Categories