nginx load wordpress from a subdirectory - php

I followed a guide from here to install wordpress on my ubuntu server. Upon entering the url http://mydomain.com loads me the front page.
server {
listen 80 default_server;
root /var/www/wordpress;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~ \.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;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
I want to load wordpress from my subdomain i.e., http://mydomain.com/wordpress. I went on to modify the location but it can't load properly (it still looks for files under http://mydomain.com).
root /var/www;
index index.php index.html index.htm;
location /wordpress {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
or
location /{
try_files $uri $uri/ /wordpress/index.php?q=$uri&$args;
}
How do I fix this? thanks

Your wordpress looking for his files in root directory. /
So you need to tell him, to search his files in the right directory. /wordpress/
There are two ways.
Better: to change the path using wordpress config file for rewriting resources path
from
/css/*, /* etc
to
/wordpress/css/*, /wordpress/* and so on.
Worst: to redirect your wordpress resources to root directory. Something like this:
location ~ ^/images/(.*)$ {
try_files $uri /wordpress/images/$uri =404;
access_log off;
}

Related

How to make a server block configuration file to run a CakePHP app on Nginx server?

Since I'm new to DigitalOcean and to Nginx server I don't have an idea what I am doing exactly
Having said that here's issue I'm getting
I've put the CakeApp folder inside /var/www/html folder and also I added an info.php page into webroot folder of my CakeApp to check if I can access it
When I go to http://my_ip/CakeApp it redirects to http://my_ip/CakeApp/login page and give a 404, but when I access the info.php file by going to http://my_ip/CakeApp/info.php it works and returns the PHP info page
here is the sever block file
server {
listen 80;
listen [::]:80;
server_name app.cake.com;
return 301 http://app.cake.com$request_uri;
root /var/www/html/CakeApp/public/webroot;
index index.php;
location /CakeApp/webroot {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_intercept_errors on;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
}
So I want to get the login page when I go to http://my_ip/CakeApp
Found a fix.
server {
listen 80;
root /var/www/html/CakeApp/webroot;
index index.php index.html index.htm index.nginx-debian.html;
server_name example.com;
location / {
rewrite ^(.+)$ $1 break;
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}

Laravel on Nginx 404

I have two sites in my centos 7 VPS. One is laravel 5.6 (let say, def.com) and the other is static html site (let say, abc.com).
The html site is running smoothly with this config:
sites-available/abc.com.conf
server {
listen 80;
server_name server_name abc.com www.abc.com;
# note that these lines are originally from the "location /" block
#root /var/www/abc.com/html;
#index index.php index.html index.htm;
location / {
root /var/www/abc.com/html;
index index.php index.html index.htm;
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
But when I am adjust the config for laravel project, like below, it come with 404
sites-available/def.com.conf
server {
listen 80;
server_name server_name def.com www.def.com;
# note that these lines are originally from the "location /" block
#root /var/www/def.com/public;
#index index.php index.html index.htm;
location / {
root /var/www/def.com/public;
index index.php index.html index.htm;
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I am using this guide https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-on-centos-7
replace this
try_files $uri $uri/ =404;
with
try_files $uri $uri/ /index.php$is_args$args;
after that run
sudo service nginx restart
Try to put the root and index directives in the server block (outside of the location block), and modify the try files to: try_files $uri $uri/ /index.php?$query_string;
Also you have the server_name two times on the 3rd line.
You can find a comprehensive guide here: https://www.digitalocean.com/community/tutorials/how-to-deploy-a-laravel-application-with-nginx-on-ubuntu-16-04

Running WordPress on Nginx Keeps Downloading File

I am trying to run Wordpress on my NGinx server, however I think that something is wrong because the index.php file keeps downloading rather than being run.
here is my sites-available:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/html;
index index.php index.html index.htm;
server_name your_domain.com;
location / {
# try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php7.1-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
I checked to make sure that I had php7.1-fpm running and that my nginx was running as well. Both checked out to be just fine.
What am I missing?
Thanks
This link solved my problem. I was missing some packages that wordpress needed. After going through that guide everything worked

NGINX server directives to reach an API

I have a LEMP server and I'm looking to install RespondCMS (http://respondcms.com/documentation/install-on-digital-ocean). I'm running into some difficulties with the mod_rewrite section for an API. I've tried several iterations but have been unable to get it to show an "API works" message that indicates the PHP app is working. I get a 404. Thus far, I have settled on the following set-up in my /etc/nginx/sites-enabled/. I'm thinking it's not accessing the /api folder correctly. Any help in understanding how it's all interacting and how to make it work is appreciated.
app.domain.com
server {
listen 80;
server_name app.domain.com;
root /srv/www/domain_app/app;
index index.php index.html index.htm;
access_log /var/log/nginx/respond.access.log;
error_log /var/log/nginx/respond.error.log;
location /api/ {
try_files $uri $uri/ /api/dispatch.php$args;
}
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /srv/www;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
domain.com
server {
listen 80 default_server;
root /srv/www/html;
index index.php index.html index.htm;
server_name domain.com;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.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;
}
}
The URL http://app.domain.com/api/dispatch.php is processed by the location ~ \.(hh|php)$ block. And that block seems to be configured (reasonably) correctly.
You should move include fastcgi_params; above any fastcgi_param directive to avoid the latter being inadvertently overridden. And the fastcgi_index directive is redundant in this context.
Both the location / and location /api/ blocks have issues. The alias directive is unnecessary and wrong. All three locations inherit their root from the outer server block. You should delete both alias directives.
In your try_files $uri $uri/ $uri.php /api/dispatch.php$args; statement, the $uri.php element will cause the PHP file to be downloaded rather than executed. Only the last element of a try_files directive can take an action that is not processed within the same location block. If you really need to execute extension-less PHP URIs and have a default PHP URI too, you will probably need to use a named location.
A useful resource for all nginx directives is here.

cannot access file .php inside webroot in cakephp with nginx

I have VPS with this specification:
Ubuntu
Nginx
Php
MySql
So I make project with CakePHP in subfolder named gogon. This project can running and access perfectly. The site url can access at example.com/gogon
problem
I put responsivefilemanager (responsivefilemanager.com) in webroot folder as filemanager, but when I want to access the PHP file in filemanager (dialog.php) site just render my index.php file like example.com/index.php
Structure of my folder:
-example.com
---index.php
---gogon (cakephp project)
-----index.php
-----app
---------config
---------...
---------...
---------...
---------webroot
-------------filemanager
----------------dialog.php
-----lib
-----plugin
-----vendor
This is my nginx config
server {
listen 80;
root /var/www/html;
index index.php index.html index.htm;
server_name example.com www.example.com;
location / {
try_files $uri $uri/ $uri.php /index.php;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/html;
}
location /gogon {
rewrite_log on;
error_log /usr/share/nginx/error.gogon.log error;
if (-f $request_filename) {
break;
}
# Avoid recursivity
if ($request_uri ~ /webroot/index.php) {
break;
}
rewrite ^/gogon$ /gogon/ permanent;
rewrite ^/gogon/app/webroot/(.*) /gogon/app/webroot/index.php?url=$1 last;
rewrite ^/gogon/(.*)$ /gogon/app/webroot/$1 last;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~* \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

Categories