Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ntd63t/b9810b424a1c65a78a48593dccd7d330 to your computer and use it in GitHub Desktop.
Save ntd63t/b9810b424a1c65a78a48593dccd7d330 to your computer and use it in GitHub Desktop.
Setup wordpress as subdirectory (/blog) of an existing app
0. Follow https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-in-ubuntu-16-04 up to step 3
1. Create wordpress.conf in /etc/nginx/sites-avaiable:
server {
listen 8080;
listen [::]:8080;
root /var/www/wordpress;
index index.php index.html index.htm index.nginx-debian.html;
server_name 127.0.0.1;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
*Note: server_name 127.0.0.1 && listen 8080 means wordpress is listening on 127.0.0.1:8080 and serving from directory root /var/www/wordpress;
2. Setup root /var/www/wordpress/index.php with content for testing purpose
<?php
phpinfo();
3. In rails app nginx conf file, add the following block to rewrite /blog
location /blog {
rewrite ^/blog/(.*)$ /$1 break;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8080$is_args$args;
proxy_redirect off;
}
rewrite ^/blog/(.*)$ /$1 break;
This line will deny /blog without a trailing slash
rewrite ^([^.]*[^/])$ $1/ permanent;
add this to location /blog block, this will always add trailing slash to /blog
4. Follow https://www.digitalocean.com/community/tutorials/how-to-install-wordpress-with-lemp-on-ubuntu-16-04
BUG1: Forward wp-admin to /blog
location /blog {
rewrite ^([^.]*[^/])$ $1/ permanent;
rewrite ^/blog/(.*)$ /$1 break;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://127.0.0.1:8080;
proxy_set_header Referer $http_referer;
proxy_redirect default;
}
BUG2: php files served over http
Add these lines in /var/www/wordpress/wp-config.php before require_once( ABSPATH . 'wp-settings.php' );
define('FORCE_SSL_ADMIN', true);
define('WP_SITEURL', '/blog');
define('WP_HOME', '/blog');
if ( isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')
$_SERVER['HTTPS']='on';
BUG3: Redirect wp-admin failed, can use these configuration to fix, can potentially fix by using proxy_redirect (maybe better but don't know how yet)
Add these lines in /var/www/wordpress/wp-config.php before require_once( ABSPATH . 'wp-settings.php' );
$_SERVER['REQUEST_URI'] = str_replace("/wp-admin/", "/blog/wp-admin/", $_SERVER['REQUEST_URI']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment