C4f1151075b447779af31e99d6cf70e2c6eb47ac
By:
newbie

How to install wordpress on a subdirectory

May 31, 2016 648 views
Nginx WordPress Ubuntu 16.04

Hello,
What i want to achieve is like this

www.domain.com - HTML website
www.domain.com/blog - wordpress

i want to have wordpress on separate virtual host. What will be the nginx configuration in this case.

1 Answer

Ok, I am assuming by "i want to have wordpress on separate virtual host" you mean you will host it on a separate server ( a virtualhost in this context is an apache configuration to support a different subdomain or domain and it sounds like that's not what you're trying to configure).

For this lets use:

server1.example.org (The site where example.org will be pointed and the server hosting your static web content)

server2.example.org (The server that will host your WordPress blog) - We will assume that WordPress is already up and running here.

example.org (the domain that will be used for both).

You'll want to use a plan Ubuntu droplet for server 1 and you can use the WordPress one-click image for the second if you like.

Point your domain name to server1's IP address in your DNS configuration for this domain.

On server 1, install nginx

sudo apt-get update
sudo apt-get install nginx

Now you can upload your static html site to /usr/share/nginx/html and the site should be viewable by browsing to your droplet's IP address.

Next we will set up a proxy configuration in nginx so that everything for /blog is proxied to your WordPress droplet.

Open the file /etc/nginx/sites-enabled/default and edit it so it looks like this:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.html index.htm;

    server_name example.org www.example.org;

    location / {
        try_files $uri $uri/ =404;
    }

    location /blog {
        proxy_pass http://your wordpress droplet's IP/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Save these changes and restart nginx with:

sudo service nginx restart

Now you'll want to access your WordPress droplet directly by it's IP address and log into wp-admin. In settings, change the base_url settings to http://example.org/blog

Now you should be able to view your static site at http://example.org and your wordpress blog at http://example.org/blog

  • @ryanpq
    Highly apologize for my mistake. I meant to say different server block. Ill use remote database setup like 1 droplet for web server 1 for mysql. But main site and wordress will be on same server. Just in different folder and in different server block. Thatst it.
    Sorry again for the misunderstanding.

    • To the best of my knowledge you would either host these both under a single server block or you would have to use different hostnames. Each server block represents one or more names, one name cannot use two server blocks at once. To do this on one server you would want to install WordPress in the /blog folder of your static site and then update the wp-admin settings as I indicated for the other setup type.

Have another answer? Share your knowledge.