Wordpress in subdirectory nginx - php

Hi I have an Nginx Server with running wp's with no problem. I just created a site in blank html5 and added some functions with php. Then my client decided to have a blog too. So i installed Wordpress into a subdirectory. My only problem is the Nginx config which I cant figure it out. I have the site on example.com/ the wp is on example.com/wp/site/ But even when i have configured everything correctly My Admin panel does not works. So i share my code, and I would highly appreciate some help. I have found similar articles online, but they does not help at all. So please take a look at my Nginx cfg for this site, and tell me whats wrong. Thank you!
server {
server_name domain.com;
listen 80;
root /var/www/domain;
include wordpress.conf;
location / {
index index.php index.html index.htm;
}
# Block PHP files in uploads directory.
location ~* /wp/acc/wp-content/uploads/.*\.php$ {
deny all;
}
location ~ ^/\.user\.ini {
deny all;
}
location /wp/acc {
limit_except GET POST OPTIONS HEAD { deny all; }
try_files $uri $uri/ /wp/acc/index.php?$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
server {
server_name domain.com;
listen 81;
root /var/www/domain;
index index.php index.html index.htm;
include wordpress7080.conf;
location / {
index index.php index.html index.htm;
}
# Block PHP files in uploads directory.
location ~* /wp/acc/wp-content/uploads/.*\.php$ {
deny all;
}
location ~ ^/\.user\.ini {
deny all;
}
location / {
limit_except GET POST OPTIONS HEAD { deny all; }
try_files $uri $uri/ /wp/acc/index.php?$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
Additional Info:
Wp-config.php has not been modified.
Url's in database are correct.
Wordpress.conf only includes basic wordpress setup for nginx.

you have to change the root statement to the directory of each wordpress in the server { } code block
root /var/www/wp1;
root /var/www/wp2;
Also, you can check in your
/var/log/nginx/error*
/var/log/nginx/access*
to get more information on how things are working.

Related

nginx 404 Not Found for php files

I'm trying to setup PHP on Nginx. My Nginx is already configured for a Django website. The subdirectory /wiki will run php powered software.
server {
server_name = my_domain_name.com;
# django stuff here
location/wiki {
alias /var/www/mediawiki;
try_files $uri =404;
index index.html index.htm index.php;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # i have verified that this does exist on my system
}
location ~ /\.ht {
deny all;
}
# more django and certbot stuff
With this configuration, my_domain_name.com/wiki/index.html can be accessed, but my_domain_name.com/wiki/info.php yields a 404 Not Found nginx page. Yet, they both reside in /var/www/mediawiki. What is wrong with my configuration?
Per Richard Smith's reference, I figured out that I needed to include a nested location php tag:
location/wiki {
alias /var/www/mediawiki;
try_files $uri =404;
index index.html index.htm index.php;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
}
and php files are served now.

PHP app in subdirectory in Nginx

I'm using Ubuntu 16.04, Nginx 1.10.3 and PHP 7.0. The example PHP applications are CodeIgniter 3.1.5.
I am trying to run a static page at the root of my site www.example.com(works) with multiple other CodeIgniter applications each running in subdirectories at www.example.com/client-a, www.example.com/client-b, etc.
The static page at my root runs fine, however when redirecting to the apps at the sub directories none of the stylesheets and scripts get loaded resulting in 404 errors. The routing works though.
The application files of the subsequent apps don't exist within one another.The application root exists in /var/www/example/public_html while the "nested" applications lives in /var/www/client_a/public_html, /var/www/client_b/public_html etc.
Here is my Nginx server block:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name example.com www.example.com;
root /var/www/example/public_html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location /client-a {
alias /var/www/client_a/public_html;
try_files $uri $uri/ #nested;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
}
location #nested {
rewrite /client_a/(.*)$ /client_a/index.php?/$1 last;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
}
location ~ /\.ht {
deny all;
}
}

Nginx with angular and codeigniter applications in subfolders

I'm trying to make a website with codeigniter in the backend and angularjs in the frontend.
For the backend I'm using nginx as a server.
Currently I have problems with proper configuration of the nginx. What I want to achieve is to have codeigniter and angularjs applications in separate folders. And What I want is to access as follows
codeigniter from my_website.com/api
angular from my_website.com
And in terms of folder I want to keep them in:
codeigniter: /var/www/html/api
angular: /var/www/html/angular
So far I've manage to make it work from the same folder. So now I have codeigniter in /var/www/html and angular folder is in the same directory. And that way I can access codeigniter from my_website.com/ and angular from my_website.com/app which is not what I want.
I was trying multiple different setups for nginx /etc/nginx/sites-available/default file, but every single time with some problems. Farthest I've got was to have those in separate folders but codeigniter was working only for default_controller and I couldn't access any other files.
That's the current working config.
server {
listen 80;
listen [::]:80 default_server ipv6only=on;
root /var/www/html;
server_name localhost;
index index.php index.htm index.html;
location /app/ {
root /var/www/html/angular;
try_files $uri/ $uri index.html =404;
}
location / {
try_files $uri $uri/ /index.php;
#index index.html;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
include fastcgi_params;
}
}
And here is simple config where I just try to run codeigniter from demo sub-folder,
server {
listen 80;
server_name localhost;
root /var/www/html;
autoindex on;
index index.php;
location / {
#try_files $uri $uri/ /index.php;
try_files $uri $uri/ /index.php$request_uri;
location = /demo/index.php {
try_files $uri $uri/ /index.php$request_uri;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME /var/www/html/demo$fastcgi_script_name;
include fastcgi_params;
}
}
location ~ \.php$ {
return 444;
}
}
And here my_website.com/demo/index.php works fine but my_website.com/demo/index.php/welcome shows 500 Internal Server Error
I hope I make myself clear.
I tried so many different configs found online, but I always had some troubles with that. Can any of you propose some simple solution for that problem?
Thanks,
Mateusz
I've finally managed to make it work the way I want. I've used this solution link to make it work with codeigniter in sub-directory which was the main problem. Here is my config:
server {
listen 80;
server_name localhost;
root /var/www/html;
autoindex on;
index index.html;
location / {
root /var/www/html/angular;
try_files $uri/ $uri index.html =404;
}
location /api/ {
alias /var/www/html/api/;
try_files $uri $uri/ /api/index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
location ~ \.php$ {
return 444;
}
}
Thanks to #FrédéricHenri for suggesting using the log file under /var/log/nginx/*.log, that helped with tracking the way nginx is redirecting requests. Also what I've encounter was that web-browser was caching the website(I think that is what was happening) so that was giving me false results while I was making some changes to the config file.
If you have already set up ~ .php and other stuff. then Just need to add
location /ci-project {
try_files $uri $uri/ /ci-project/index.php;
}
In your server {} object of Nginx configuration

Migrating Yii2 from Apache to Nginx - failed on backend app

I try to move my Yii2 app from using apache to nginx. I'm using Yii2 advanced template on a single domain, so I follow the solution from this link creating a symlink from frontend/web to the backend/web. This solution works fine when I'm using apache, but when I try to use nginx I can't open the backend app.
The frontend app works fine, but when I try to open the backend app using app.local/belakang, it is redirected to app.local/belakang/site/login, but still stuck in the frontend app and therefore giving a [yii\web\HttpException:404] exception 'yii\base\InvalidRouteException' with message 'Unable to resolve the request "belakang/site/login".'
Here is the symlinks that I have on the frontend/web directory :
lrwxrwxrwx 1 vagrant vagrant 17 Jun 24 14:03 belakang -> ../../backend/web
and this is the nginx vhost config that I use:
server {
charset utf-8;
client_max_body_size 128M;
listen 80; ## listen for ipv4
server_name app.local;
root /var/www/html/frontend/web;
index index.php;
location / {
# Redirect everything that isn't a real file to index.php
try_files $uri $uri/ /index.php$is_args$args;
}
# deny accessing php files for the /assets directory
location ~ ^/assets/.*\.php$ {
deny all;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php5-fpm.sock;
try_files $uri =404;
}
location ~* /\. {
deny all;
}
}
Can anyone help so that I can open the backend app from app.local/belakang ?
Fix it myself by adding config below
location ^~ /belakang {
try_files $uri $uri/ /belakang/index.php$is_args$args;
location ~ /\.(ht|svn|git) {
deny all;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php/php5-fpm.sock;
try_files $uri =404;
}
}

Serving php files from different locations in nginx

I have my main website and wordpress in different directories on my server on which I use nginx as the web server. The main website is in /home/me/www and Wordpress is in /home/me/wordpress. I need to have them in separate directories this way for a particular reason. How do I specify this in the nginx configuration file? I currently have the following and it does not work:
location / {
root /home/me/www;
index index.php index.html index.htm;
}
location /blog {
root /home/me/wordpress;
index index.php index.html index.htm;
}
location ~ \.php$ {
set $php_root /home/me/www;
if ($request_uri ~ /blog) {
set $php_root /home/me/wordpress;
}
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $php_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
It currently returns HTTP 404 when I try to access http://mydomain/blog
Check out this question and the Nginx Manual.
Try changing your blog line to:
location ^~ /blog/ {
root /home/me/wordpress;
index index.php index.html index.htm;
}
I struggled with this for hours now and finally reached the working configurations as the following:
location /php-app {
passenger_enabled off;
alias /path/to/php-app/$1;
index index.php index.html;
try_files $uri $uri/ #rewrite;
}
location #rewrite {
rewrite ^/php-app(.*)$ /index.php?q=$1 last;
}
location ~ \.php$ {
alias /path/to/php-app/$1;
rewrite ^/php-app(.*)$ $1 last;
passenger_enabled off;
fastcgi_pass unix:/tmp/php-fpm.socket;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME /path/to/php-app$fastcgi_script_name;
fastcgi_intercept_errors on;
}
Just adding more detail in #Nick Presta answer.
^~: If a carat and tilde modifier is present, and if this block is selected as the best non-regular expression match, regular expression matching will not take place.
Checkout These differences enter link description here

Categories