Setting up multiple WP sites in a LEMP server

April 6, 2015 2.3k views
WordPress LEMP

I want to use my LEMP server to host HTML and WP sites. I created my first WP site following this tutorial https://www.digitalocean.com/community/tutorials/how-to-install-wordpress-with-nginx-on-ubuntu-14-04, the only thing that I changed is, that I didn't install the WP on the root, I've installed my WP in /var/www/html/webs/wpsite because I will have many WP sites in /myfolder/

So what I'd like to have, is access to the WP site on this way myIP/webs/wpsite until I point the DNS to the domain, but I cant.

Supposedly I should get access to the WP site that I install through that URL, since I configured the nginx block on the below, but anytime that I go to myIP/webs/wpsite it automatically redirect me to the main IP, to the IP of my server, I can't understand why. Here it is my configuration on sites-available:

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

        root /var/www/html/secure/oxygen;
        index index.php index.html index.htm;

        # Make site accessible from http://localhost/
        server_name;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                # try_files $uri $uri/ =404;
                try_files $uri $uri/ /index.php?q=$uri&$args;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }

        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;
        }

        # Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
        #location /RequestDenied {
        #       proxy_pass http://127.0.0.1:8080;
        #}

        #error_page 404 /404.html;

        # redirect server error pages to the static page /50x.html
        #
        #error_page 500 502 503 504 /50x.html;
        #location = /50x.html {
        #       root /usr/share/nginx/html;
        #}
       # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
        #       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
        #
        #       # With php5-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;
        #       # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        #}
}

1 comment
7 Answers

create different server {} blocks just change the server_name to match the domain you wish to choose. set the root to /var/www/html/webs/wpsite

To access them all though you will need to setup a default server {} block that points to /var/www/html/webs/

That will allow you to use domain based virtual hosts and a single URL to access them all

I don't understand why I have to create another server block for this WP site. I already created it own server block for this WP installation, so it should work...

Should I have to create a default server block that points to /var/www/html/webs/?

The thing is, that I want to upload my HTML corporative site in /var/www/html/ and in /var/www/html/webs/ is going to be just WP sites...

I don't a clue yet of what should I do

Say you have a subdomain

webs.yoursite.com => /var/www/html/webs/

on this subdomain there are multiple wordpress sites

webs.yoursite.com/wordpress
webs.yoursite.com/wordpress2
....

and so on.

You would need to create a server block for the webs.yoursite.com to point to the main wordpress directory of your site.

You would then have your second server block for your main corporate site in /var/www/html

if you want to add a subdomain to a wordpress site you add another server block with the server_name set.

wordpress.yoursite.com => /var/www/html/webs/wordpress

So for each wordpress site you will need a new server block configured for the wordpress site.

I recommend you look at a way to automate the nginx configuration using something like Chef or Puppet to make your life easier. Theres a tutorial on the website in my profile to get you started with chef.

Thanks Affix I'll have a look to you tutorial, and let you know if I finally make it works.

Affix I tried to set up Chef, but I'm stacked just in the first step. I didn't have installed Yum, so I installed it without problems. Now, when I try to install the Chef core, says that I just can do it as a Root, and I'm accesing to my server as a user, since I restricted the access to root to prevent atacks.

Then I tried this sudo yum localinstall chef-server-core-12.0.7-1.el6.x86_64.rpm but says ```
Cannot open: chef-server-core-12.0.7-1.el6.x86_64.rpm. Skipping.
Nothing to do


Anyway, I think that I could do this by myself, I just have to get it.

The thing is this. Currently I have a shared hosting in godaddy. There, I have my main domain (my corporative html website) and my wp sites, with all the domains hosted.

I don't what I have to do first, but could be pointing my main domain to my digital ocean server to start hosting my corporative website in  /var/www/html.

I didn't understand what you wrote about subdomains...what I want to do is pointing my domains from godaddy directly to the wp site directory...shoud I create first subdomains to perform this?

yes you will need to create subdomains from your Godaddy Control Panel (Unless you setup a Nameserver using bind).

If you need help you just need I'm available through the day until about 10pm BST (UTC +1)

Thanks for your help!

Now I'm understanding further how this works. I deployed my first web and made it works on my Lemp server being pointed by my domain hosted in godaddy.

Finally I realised that at least, in my case, I don't need to create subdomains in my domain registar to point it to the server. What I did in godaddy, was just setting the IP and the name server, ns1.digitalocean.com and so- on....and then, in the nginx block, I just set the path on the root and the domain name on the server_name. Then, I think that when the request from godaddy comes to my server, it looks on the file to find the domain name to match, and then, the root path tell to the request where is the web hosted.

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

        root /var/www/html/webs/wordpress;
        index index.php index.html index.htm;

        # Make site accessible from http://localhost/
        server_name www.mydomain.com

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                # try_files $uri $uri/ =404;
                try_files $uri $uri/ /index.php?q=$uri&$args;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }

        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;
        }

So far is going good. But I'd like to access to my WP sites by something like myipserver/webs/anyWP, since sometimes, I do first the develop of the website without pointing the domain to the folder, and once the website is done, I point it...do you know how can I perform that? Maybe pointing my corporative domain to /var/www/html so I could access to that websites by mycorporativedomain/webs/anyWP?

Thanks.

Have another answer? Share your knowledge.