I'm trying to get wordpress running in a subdirectory of an existing nginx (v1.14) server. The rest of the server works fine. I've copied the relevant lines of my nginx.conf below. If I remove the lines that refer directly to the wordpress (wp) directory, then I get an error complaining that wp has called an undefined function "mysql_connect". When I leave the lines that directly refer to the wp directory, it complains about permission denied.
I've looked through a bunch of other posts about this but I can't seem to find any solutions that solve the issue for me. I've tried:
adding define( 'WP_USE_EXT_MYSQL', true ); to wp-config.php and toggling the boolean
adding / removing $uri/ from nginx.conf
chmodding the wordpress dir and contents (755 for the dir and 644 for the contents)
adding / removing index.php to the wordpress location definition in nginx.conf
I have no idea how to troubleshoot this any further.
...
http {
...
upstream php {
server unix:/tmp/php-cgi.socket;
server 127.0.0.9000;
}
...
server_name domain.tld
root /some/dir
location / {
index index.html index.htm index.php;
}
...
location /wordpress {
try_files $uri $uri/ /wordpress/index.php?$args;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_split_path_info ^(/wordpress)(/.*)$;
}
...
Related
I have Nginx configured and running for some time now. It is handling 3 applications.
Most important is the application #1. It consists of 3 modules: account, participant and admin. Therefore these addresses allow logging into the application for different types of users:
domain.com/account/login, domain.com/participant/login or domain.com/admin/login
Also the homepage domain.com is handled by the same application.
Nginx config was very simple to serve this:
location / {
try_files $uri $uri/ /index.php?$args;
}
root $rootPath;
index index.html index.php;
# PHP
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
try_files $uri /index.php?$args;
}
Thanks to this Home page and Modules were handled by Yii2.
I don't think it should be relevant but there are 2 more apps configured in nginx under locations /application2 and / application3. One is php Zend app and the other - php and javascript app.
Now I need to replace homepage with Wordpress one-pager. Desired solution is to have:
homepage served by Wordpress at domain.com
Yii2 application modules working as before, so that all /participant, /account and /admin locations are served by index.php at $rootPath
2 other applications working as before at domain.com/application2 and domain.com/application3.
For handling Wordpress page I have added one section location = / to redirect main address to Wordpress app. But I cannot add nested location for PHP because I am getting this error: location "/(.+.php$)" cannot be inside the exact location "/"
So I changed the approach. I have set global root to Wordpress path: root $wpPath; and added locations for account, participant and admin modules:
location /account {
root $rootPath;
if (-f $request_filename) {
break;
}
# process PHP here
location ~ /account/(.+\.php$) {
alias $rootPath/$1;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
The main page (Wordpress) works perfectly fine. But the Yii2 application modules (like /aaccount) return 404 - Not found. What is wrong with above setup?
I have implemented a php application in codeigniter and now want to deploy it to the nginx server. Before deploying I checked my nignx configuration on my localhost using MAMP server. It is working correctly. But, this configuration is not working on the live server. As a beginner in nginx, I am not understanding where is the mistake here. In live server, I can not write in the main nginx.conf file. I have a separate configuration file like "abc" for my application "abc". And all my application files are under "abc/xyz" directory. Here is my sample confuguration,
location /abc {
root /srv/www/htdocs/apps/;
index index.html index.htm index.php;
location /xyz {
try_files $uri $uri/ /abc/xyz/index.php;
}
location ~ \.php(\/(\w+))*$ {
try_files $uri =404;
rewrite (.+)\.php(\/(\w+))*$ $1.php break;
include /etc/nginx/fastcgi_params;
fastcgi_index index.php;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
Here, I can see my welcome page https://myapplication/abc/xyz. But if I want to navigate other pages like https://myapplication/abc/xyz/other_pages, it is showing "404 Page not found". I have checked the other solutions but none of them is not working in this case. Thanks in advance for the help!
The location /xyz block is nested within the location /abc block. The nested block is required to precess URIs with a prefix of /abc/xyz.
If there are other regular expression location blocks surrounding your location /abc block, you should use the^~` modifier.
For example:
location ^~ /abc {
...
location /abc/xyz {
...
}
...
}
See this document for more.
Sorry for the late answer. It was actually very silly mistake. My controller page name was in small character. This is why it was not working. My configuration is okay. The first letter of the controller page should be in capital character. For example, my controller name is Home. So my php file name must be Home.php not home.php.
I'm not used to Nginx yet...migrating from Apache2. I do not understand the location and root directives...I've read that root is best to use over alias...so, I made all roots absolute paths.
Server Block here:
index index.php index.html;
location = / {
root /var/www/app;
}
location /chat/ {
root /var/www/project1/chat;
}
location /kanban/ {
root /var/www/kanban;
}
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
include fastcgi_params;
}
When I have root /var/www/app; outside of a location block will the main index.php work. However, when adding location blocks with root paths, all I get are 404 pages. I hope that it is not required to add PHP location blocks for each defined location, surely that is not needed right?
All I want is, for www.site.com to go to /var/www/app/index.php and www.site.com/chat/ to go to /var/www/project1/chat/index.php, etc. for each of my projects.
To redirect mywebsite.com/chat to a different directory than the root of your website you would have something like the code below.
root var/www/app;
index index.html index.php;
location /chat/ {
alias /var/www/project1/chat/;
index index.php;
}
Currently I'm running webprojects with the following directory structure (simplified):
project_folder/
public/
root/
index.php
What I want to do is set the root in the server block to:
root /project_folder/root/;
But when a request location does not exists I want it to forward the request to project_folder/index.php. I tried the following:
try_files $uri ../index.php?$query_string;
But this doesn't seem to work.
The same goes for a request that starts with $document_root/public/* which need to be forwarded to: $document_root/../public/*
I did manage this by adding a index.php in the /project_folder/root/ folder which includes the /project_folder/index.php. For the public folder I created a symlink. I could probably solve this by adding an alias for the location /public/ however I'm trying to keep my config as generic as possible.
I'm trying to prevent setting the project_folder as a root folder as it sometimes contains files I prefer not to be accessed from the web. Introducing the 'root' folder is new but I'm trying to do this as efficient as possible. Existing webprojects do not have the root folder yet.
Is what I'm trying to do possible and how would I be able to achieve this.
Thanks in advance.
Usually PHP files are processed by a location ~ \.php$ block (or similar). I assume that index.php is not the only PHP file in your application, and to process PHP files within the /root/ directory structure, that location will need to use root /project_folder/root.
You can specify a different root for URIs which begin /public and use a named location to execute the out-of-root index.php file.
Something like this:
root /project_folder/root;
location / {
try_files $uri $uri/ #index;
}
location /public {
root /project_folder;
}
location #index {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /project_folder/index.php;
fastcgi_pass ...;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass ...;
}
It is simple to add an alias:
location /public {
alias /absolute/path/to/public;
}
Basically I have a site, called example.com. Within this site, there is a WordPress-Blog, wich should be accessible under "example.com/blog/". The actual directory where the blog is installed is not "/blog/", but "/blog/de/", as this project has multiple stand-alone-blogs for different languages.
The problem is, that WordPress cannot access to wp-content and wp-include files, because WordPress tries to load css (and other) files via example.com/blog/wp-content/....
So i want to get my Blog running under "example.com/blog", but all WordPress-Files should be loaded from /blog/de/.
This is my nginx-vhost-config for /blog-Location:
location /blog {
index index.php;
try_files $uri $uri/ /de/index.php?$args;
rewrite ^(/wp-content/.*)$ /de/wp-content/$1 last;
rewrite ^/blog/(.*)+$ /blog/de/index.php?$1;
}
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
I am totally new to nginx, so every possible solution is welcome!
Essentially you need to alias the location /blog to the path /blog/de. The complication is the way in which PHP files are handled, particularly when other applications are hosted within the same domain.
First internally rewrite /blog to /blog/de:
location /blog {
rewrite ^/blog(.*)$ /blog/de$1 last;
}
Then implement the /blog/de location with a nested location to handle PHP:
set $wordpress /blog/de/index.php;
location ^~ /blog/de {
# root inherited from server container???
index index.php;
try_files $uri $uri/ $wordpress;
location ~ \.php$ {
try_files $uri $wordpress;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass ...;
}
}
I make /blog/de a priority prefix location, to avoid a conflict with any other php location. But that would not be necessary if this is the only application within this server block.