Skip to content

Instantly share code, notes, and snippets.

@vuongpd95
Last active June 2, 2022 21:22
Show Gist options
  • Save vuongpd95/7c5f75ed0a3e384a5d8e9d033150d05c to your computer and use it in GitHub Desktop.
Save vuongpd95/7c5f75ed0a3e384a5d8e9d033150d05c to your computer and use it in GitHub Desktop.
Setup wordpress as subdirectory (/blog) of an existing app
  1. Follow https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-in-ubuntu-16-04 up to step 3

  2. Install WP https://www.digitalocean.com/community/tutorials/how-to-install-wordpress-with-lamp-on-ubuntu-18-04

  3. 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;

  1. Setup root /var/www/wordpress/index.php with content for testing purpose
  1. 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;
 }
# This line will deny /blog without a trailing slash
rewrite ^/blog/(.*)$ /$1 break;

# add this to location /blog block, this will always add trailing slash to /blog
rewrite ^([^.]*[^/])$ $1/ permanent;
  1. 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