Skip to main content

Nginx环境安装多站点WordPress | Multiple Domain/Sites in One WordPress Installation with Nginx

Please scroll down for English readers.

本文目的是指导在Nginx环境下安装多域名Wordpress站点的方法。多域名是指使用Wordpress内建的Network(网络)功能,安装一次Wordpress程序,同时开设多个域名下的不同站点。

This Guide helps you to configure multiple websites(domains) within one WordPress installation. Here we use WordPress built-in multisite function to host multiple websites both under one domian by subdomains or under multiple domains.

版权

本文原载于 https://nosame.net/nginx-multi-domain-wordpress-guide/

作者/码钉

背景

目前(2016/07)能找到的文档都都已经过时。当前的Wordpress程序(v4.5.x)已经可以很好地支持一次安装、多域名、多站点运行。这使得我们可以在一个虚拟主机下,开设多个不同站点,既节约空间、又节省服务器资源。

WordPress是一套备受欢迎的内容管理程序(CMS),它开源、免费、拥有众多扩展和主题模板,世界上超过30%的网站是由Wordpress搭建的。Wordpress V2时代,Wordpress(单站点)和Wordpress MU(多站点)是单独发行的两个系统。然而Wordpress V3时代,标准的Wordpress安装包已经集多站点功能。现在,Wordpress的多站点功能愈发完善,可用的插件也越来越多,非常方便我们在一个空间搭建多个站点。对于演示主题、展示项目或建设临时网站来说,这是个非常不错的解决方案。

Nginx是一套Linux系统下用来托管网站的代理服务器程序。它常常搭配开源的PHP-FPM和MySQL组件用来托管PHP动态站点。Nginx可以在同等硬件配置下,表现出相对Apache 2更好的性能;也非常适合那些过时硬件拼凑的服务器主机或者紧凑型的虚拟服务器(VPS)。

简述

  1. 是否需要使用付费插件?
    不需要使用插件。
  2. 是否只能使用在独立服务器上?
    不是。如果你的虚拟主机可以绑定多个域名(一般都可以),那么你同样可以使用Wordpress搭建多个站点。
  3. 用户会察觉到我的站点与独立站点不同?
    一般不会。但是如果你的站点涉及到用户注册的话,可能会跳转到你的主站点。
  4. 只能开设有限的站点吗?
    不知道。但是十几个到数十个应该没有太大问题。这取决于你的主机性能,或者主机限制。
  5. 比分别单独安装具有很大优势吗?
    取决于你的标准。如果你厌烦重复安装WP、管理众多账户(WP和主机CP)、反复更新插件和主题,或者仅仅是不想把钱浪费在你根本用不完的虚拟主机上,那么多站点将会是很好的解决方案。

步骤

配置Nginx配置文件

首先,配置你的服务器,安装Linux、Nginx、PHP和MySQL 套件。我使用的是lnmp.org提供的lnmp一键安装包。这个安装包禁用了PHP的 scandir() 函数。这是Wordpress必须用到的一个函数。安装完成后,你需要到修改下面目录的 php.ini 文件。

/usr/local/php/etc/php.ini

从 disabled_function 后面删除 scandir() 项。重新启动PHP(lnmp php-fpm restart)使得PHP新配置生效。

使用lnmp管理命令(lnmp vhost add)添加一个新的主机 example.com,同时为它绑定newsite.com、website.com以及三者带www的子域名。打开example.com的配置文件

/usr/local/nginx/conf/vhost/example.com.conf

你会看到配置文件的前几行如下(从“{”之后开始):

 listen 80;
 #listen [::]:80;
 server_name example.com www.example.com newsite.com www.newsite.com website.com www.website.com;
 index index.html index.htm index.php default.html default.htm default.php;
 root /home/wwwroot/example.com;

注意红色字体部分,确保你希望开设的站点都能被正确解析。另外,请记得修改以上六个裸域和www子域的DNS记录,确保它们都已经指向了你的服务器(无论是A记录或CNAME记录)。

接着继续编辑 example.com.conf 文件。你应该看到一行以include开头的 url rewrite(URL地址改写) 引用文件。

将它修改成下面这样:

include wordpress-mu.conf;

lnmp一键安装包提供了wordpress的常规 url rewrite 模板,它存储在

/usr/local/nginx/conf/wordpress.conf

但是它并不满足我们架设多站点所需要的规则。在

/usr/local/nginx/conf

目录下新建一个 wordpress-mu.conf 文件,复制以下内容:

location / {
 try_files $uri $uri/ /index.php?$args;
}

location = /favicon.ico {
 log_not_found off;
 access_log off;
}

location = /robots.txt {
 allow all;
 log_not_found off;
 access_log off;
}

location ~* /(?:uploads|files)/.*\.php$ {
 deny all;
}

# Rewrite rules for WordPress Multi-site.
if (!-e $request_filename) {
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
rewrite ^/[_0-9a-zA-Z-]+(/wp-.*) $1 last;
rewrite ^/[_0-9a-zA-Z-]+(/.*\.php)$ $1 last;
}

这些内容帮助我们保护Wordpress的upload目录下无法运行php文件(这样做更安全),并纠正错误的wp-admin访问链接。

完成以上的步骤后,运行 lnmp nginx restart 载入新的配置文件。

设置Wordpress

然后,你需要按照常规安装一次Wordpress程序,就像你通常安装单用户版本那样,为Wordpress建立相应的MySQL数据库和数据库账号,运行安装向导,直至进入Wordpress的安装面板。

使用你的FTP客户端编辑 /wp-config.php 文件。

找到这一行:

define('WP_DEBUG', false);

在它之后加上

define('WP_ALLOW_MULTISITE', true);

然后进入Wordpress后台,点击工具(Tools)-> 设置网络(Network Setup),选择子域名(Subdomain)模式。并输入站点群的名称(无关紧要)和管理员邮箱。点击下一步,你会看到下面这个界面:

7

截图中的第一步要求我们继续修改 /wp-config.php文件。我们需要继续修改,但是并不完全按照它要求的那样。

在 define(‘WP_ALLOW_MULTISITE’, true);  一行之下,加入以下内容:

 define('MULTISITE', true);
 define('SUBDOMAIN_INSTALL', true);
 // define('DOMAIN_CURRENT_SITE', 'example.net');
 define('PATH_CURRENT_SITE', '/');
 define('SITE_ID_CURRENT_SITE', 1);
 define('BLOG_ID_CURRENT_SITE', 1);
 define('COOKIE_DOMAIN', false);

注意红色字体的部分,如果你弄错了这里的配置,你很可能无法打开newsite.com和website.com的后台。

与标准配置不同的是,我们增加了一条不引用域名生成cookie的设置。这样你就可以以xxx.com/wp-admin的形式单独登录每个站点的后台了。

创建站点

接下来的工作就相对要轻松很多。点击“我的站点”菜单,选择新增站点。

1

设置站点的子域名、站点名称、后台语言和管理员邮箱。子域名可以任意设置,例如“new”,站点名称和后台语言可以稍后修改,也可以任意设置,管理员邮箱请输入主站点的管理员邮箱,使得新站点共用原来的管理员账户。

2

点击“新增站点(Add Site)”后,立即点击“编辑站点(Edit Site)”。

3

将域名地址修改为http://newsite.com。

按照上面的步骤添加website.com和其他更多站点。

Ok,你可以开始使用你的新站点了!


Copyright

Originally from https://nosame.net/nginx-multi-domain-wordpress-guide/

Author/ Martin L. (Written in Chinese and translated by author himself)

Background

For concurrent available documents (2016/07) on this subject – to run several websites in one WordPress installation with different domains, especially TLDs, are out of date. We need no more plugins (either paid or free) to run multiple domains in WordPress (v4.5.x). It makes multi-site hosting cheaper, faster and more reliable with lesser reinvest on the equipment.

WordPress is a popular Content Management System(CMS) which is free, opensoure, shipped with abundant plugins and themes. More than 30% websites on this planet are built by WordPress. During the lifespan of WordPress v2.x,Wordpress (single site version) and WordPress MU(Multiple Users) are maintained separately, however the development team integrated two whilst WordPress prompted into version 3.x, multiple site function is distributed with standard zip file. Now, we have a mature platform with constructive progress on multisite function – lots of helpful plugins. In a word, it has became a smart choice for theme demonstrations, short-term projects or temporary websites.

Nginx is an opensource reverse proxy server application. Normally it works along with PHP-fpm and MySQL to host PHP-based websites. Nginx has demonstrated its high performance record compared with Apache 2. Many budget VPS and legacy hardwares have deployed Nginx+MySQL+PHP to host large volmune sites.

Briefing

  1. Do I need a paid plugin to activate multisite?
    Negative. No plugins required.
  2. Do I need a dedicated server (or at least a VPS) to run multisite?
    Negative. It would also work with virtual hosting service, if they allow you to bind multiple domains.
  3. Will users sense the non-traditional driven-mode?
    Rarely. Only if you plan to open the user registration, they will be redirected to your major domain.
  4. Is there any limitation of site quantity?
    Not sure. Works with dozens of sites, you may find out it is finally up to your server’s hardware.
  5. Will I get tremendous advantages compared with installing these sites individually?
    Up to your standards. Are you bored with repeated installations of WordPress? Managing countless administrator accounts and passwords? and silly updates once and again?

Nginx Configuration

Please skip this section if you are using virtual hosting.

First of all, you have to get your server running  Nginx, PHP, and MySQL. I had my VPS ready with lnmp easy-install script from lnmp.org. The script disabled the a sensitive PHP function ‘scandir()’ which is required by WordPress. Please edit your php.ini in the following path. (or your own path.)

/usr/local/php/etc/php.ini

Delete ‘scandir()’ behind ‘disabled_function’ line. Restart PHP (#/lnmp php-fpm restart) to activate.

The script also provides a tool to manage virtual hosts. I added a new site ‘example.com’ (#/lnmp vhost add) and bind ‘newsite.com’ and ‘website.com’ to this very host. Please edit the configuration of ‘example.com’ in the following path.

/usr/local/nginx/conf/vhost/example.com.conf

You will find something as following (after ‘{‘):

 listen 80;
 #listen [::]:80;
 server_name example.com www.example.com newsite.com www.newsite.com website.com www.website.com;
 index index.html index.htm index.php default.html default.htm default.php;
 root /home/wwwroot/example.com;

Please pay attention to the lines in red color. Make sure you have all the domains listed correctly as above. And do please check you have updated all domains’ DNS records.

Continue to edit ‘example.com.conf’ and edit the ‘include’ line (or add a new line as following).

include wordpress-mu.conf;

 

We are creating this particular ‘wordpress-mu.conf’ file in the following path.

/usr/local/nginx/conf

Copy following content into ‘wordpress-mu.conf’.

location / {
 try_files $uri $uri/ /index.php?$args;
}

location = /favicon.ico {
 log_not_found off;
 access_log off;
}

location = /robots.txt {
 allow all;
 log_not_found off;
 access_log off;
}

location ~* /(?:uploads|files)/.*\.php$ {
 deny all;
}

# Rewrite rules for WordPress Multi-site.
if (!-e $request_filename) {
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
rewrite ^/[_0-9a-zA-Z-]+(/wp-.*) $1 last;
rewrite ^/[_0-9a-zA-Z-]+(/.*\.php)$ $1 last;
}

Above rewrite rules help us to protect Wordpress’ upload folder from executing PHP files (good for you).

At last, we can restart Nginx (#/lnmp nginx restart) to reload configurations.

WordPress Configuration

Perform a regular WordPress installation as usual: type in your MySQL database, account and password, as well as site title and administrator email and etc. Do not go to next move until you can login to WordPress backend.

Edit ‘wp-config.php’. Find the following line.

define('WP_DEBUG', false);

Then add the following line right beneath the above line.

define('WP_ALLOW_MULTISITE', true);

Login to WordPress control panel. Go to Tools -> Network Setup,  choose Subdomain option. Give your network a name (does not really matter anyway) and type in the administrator email. Click ‘Next Step’.

7

Then WordPress ask you to modify ‘wp-config.php’, of course we need to modify ‘wp-config.php’, but in our way.

Add following codes beneath ‘define(‘WP_ALLOW_MULTISITE’, true);’:

 define('MULTISITE', true);
 define('SUBDOMAIN_INSTALL', true);
 // define('DOMAIN_CURRENT_SITE', 'example.com');
 define('PATH_CURRENT_SITE', '/');
 define('SITE_ID_CURRENT_SITE', 1);
 define('BLOG_ID_CURRENT_SITE', 1);
 define('COOKIE_DOMAIN', false);

If you missed this part, you may not be able to login to the control panel of ‘newsite.com’ or ‘website.com’.

Tweaked from the suggested configuration, we removed the cookies based on domains, therefore your browser would not mess up your logins.

Create Websites

Only a few clicks left to be done so far. Go to ‘Sites’ -> ‘Add New’

1

Give a temporary site address, site title and administrator email (better the same one of your major site), then click ‘Add Site’.

2

Click ‘Edit Site’ immediately.

3

and edit the full site address to ‘http://newsite.com’, add more sites so forth.

 

All done! Start your blogging now!

Admin

Creativist 创意先锋 a.k.a 码钉

发表评论

电子邮件地址不会被公开。 必填项已用*标注