CodeIgniter 3, Nginx rewrite how to? - php

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

Related

Phalcon and Nginx - download php file instead execute when in first level

I have problem with Nginx and Phalcon configuration.
This is my server block:
server {
listen 80;
server_name testapp.dev;
root /srv/http/testapp/public;
index index.php index.html index.htm;
charset utf-8;
location / {
try_files $uri/ /index.php?_url=$uri&$args;
}
location ~ \.php$ {
fastcgi_index index.php;
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
include fastcgi_params;
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;
}
location ~ /\.ht {
deny all;
}
}
It's from Phalcon website: https://olddocs.phalconphp.com/en/3.0.0/reference/nginx.html
Everything is ok until I don't enter into browser address like this (when there is "Test" module): testapp.dev/test. But it works when I pass testapp.dev/test/ or testapp.dev/test/part or testapp/test/part/.
Interesting is, that if I enter name of module that don't exist, for example: testapp.dev/test2, it executes index file and shows error that module definition path doesn't exist.
To summary above:
testapp.dev/test - not working
testapp.dev/test/ - working
testapp.dev/test/part - working
testapp.dev/test/part/ - working
testapp.dev/test2 - module not exist, but index is executing, so, it's working
testapp.dev/test2/ - like above...
When I enter into: testapp.dev/index.php?url_=/test it's working.
First thought - I should try add / at the end of try_files directive, but when I insert it, it's not working either.
I tried also this:
Phalcon and nginx - framework run only indexController. It's like above...
Also I was trying to add trailing slash like this: https://serverfault.com/questions/645667/php-file-downloaded-instead-of-executed-with-nginx-try-files, but no success.

How do I install other applications alongside Laravel?

I'm building a website in Laravel 5.2 and rather than building a forum from scratch, I wish to install one such as SMF.
Laravel is currently in the root directory of my web server and I wish to keep it there as I wish to install SMF in a folder.
For example: www.example.com/smf
I'm thinking to install it in Laravel's /public folder but I'm afraid they will they conflict with each other. Is the /publicfolder the correct place to install SMF and should I use a route to point to the SMF folder?
Server: D.O droplet via Laravel Forge
You need to add custom rules for the folder(s) you want to use before Laravel related rules:
location /smf/index.php(/.*)?$ {
fastcgi_split_path_info ^(/smf/index.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_read_timeout 1000;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}
location /smf/ {
if (!-e $request_filename) {
rewrite ^.*$ /smf/index.php last;
}
try_files $uri $uri/ smf/index.php?args;
}
Please look for sample nginx config file here.
You could use Nginx to redirect www.example.com/smf to your SMF installation. To do so add this to your server block:
location /smf {
# nginx will concatenate the string above with the root value
# so your SMF files should be in "/path/to/smf/parent/dir/smf".
# Make sure that Nginx can access them.
root "/path/to/smf/parent/dir";
# change this config to suit your needs
index index.php index.html index.htm;
location ~ \.php$ {
# Here use the same config from the server block that allows you
# to execute PHP scripts
fastcgi_pass 127.0.0.1:9123;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
A couple of things I should add:
Backup the config file before editing them.
Although I've tried the code above (and it works on my machineā„¢) I must say that I'm not an Nginx expert.

SEO friendly URL (Permalinks) not working for Wordpress on NGINX

We have wordpress installedin a subdirectory on our NGINX server. We want our blog URL to look something like www.example.com/blog. Individual blog posts urls should be like www.example.com/blog/post-name. For this when we go to setting->permalinks menu in wordpress and change it to Post Name from Default, it starts giving error. But it works just fine when we leave it default (www.example.com/blog/?p=123).
Blog directory is installed under html folder in nginx.
We've made following entry in nginx.conf file:
location /blog {
root /usr/share/nginx/html;
index index.html index.php;
try_files $uri $uri/ /index.php?$args;
location ~ \.php$ {
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_index index.php;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
}
}
blog directory is installed at same level as the folder for our main site (example.com).
What are we doing wrong?
Try this,
location /blog/ {
root /usr/share/nginx/html;
index index.html index.php;
try_files $uri $uri/ /blog/index.php?$args;
}
Had a similar issue and I added this in my nginx conf to make it work for wordpress /index.php/permalink urls in nginx hhvm 3.21
Adding this for everyone's reference:
location / {
...
fastcgi_param SCRIPT_FILENAME $document_root/index.php$fastcgi_script_name;
}
or
location / {
rewrite ^/([^\.]+)$ /index.php/$1 break;
}
Ensure you are using a fastcgi and not a server version (in server version you may get too many redirects due to rewrite)
TESTED
QA PASSED

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.

how do i setup nginx websites as in Xampp

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

Categories