如何在Ubuntu 18.04上使用Nginx安装多个WordPress
想象一下,如果我们只有一台服务器,但有2个或者更多WordPress,则我们认为需要更多服务器才能在其上运行每个wordpress。
很好,但是如果我们不需要花费太多维护多个服务器的费用,那该如何呢?
本文将向我们展示如何在Ubuntu 18.04 LTS上使用Nginx + Mariadb + php-fpm在一个主机服务器上运行多个WordPress。
准备工作
1)Nginx网络服务器
2)Mariadb用于mysql数据库
3)用于PHP FastCGI流程管理器的php-fpm
下面的命令将更新系统并为php软件包添加新的存储库
# apt-get update # apt-get install software-properties-common # add-apt-repository ppa:ondrej/php
安装Nginx,Mariadb和php-fpm
我们将安装运行WordPress所需的Web服务器Nginx,MySQL服务器Mariadb和php7-fpm以及php7扩展
# apt-get install nginx mariadb-server mariadb-client \ php7.0-fpm php7.0-common php7.0-mbstring php7.0-xmlrpc \ php7.0-soap php7.0-gd php7.0-xml php7.0-intl php7.0-mysql \ php7.0-cli php7.0-mcrypt php7.0-ldap php7.0-zip php7.0-curl -y
配置Mariadb服务器
对于刷新数据库服务器的安装,我们需要配置mysql的root密码和其他选项
该命令在向导模式下运行,只需回答问题并通过“ Enter”将其应用
# mysql_secure_installation ... Enter current password for root (enter for none): #< Enter ... Change the root password? [Y/n] y #< type 'y' then Enter New password: #< Type mysql root password Re-enter new password: #< Retype mysql root password Password updated successfully! Reloading privilege tables.. ... Success! ... Remove anonymous users? [Y/n] y #< type 'y' then Enter ... Success! ... Disallow root login remotely? [Y/n] y #< type 'y' then Enter ... Success! ... Remove test database and access to it? [Y/n] y #< type 'y' then Enter ... Reload privilege tables now? [Y/n] y #< type 'y' then Enter ... Thanks for using MariaDB!
检查Mariadb服务并在启动时启用服务
为了确保mariadb-server正在运行,并确认我们在之前的步骤中所做的所有设置。
如果没有错误,Mariadb-server准备提供数据库服务
# systemctl status mariadb ● mariadb.service - MariaDB database server Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2016-07-16 03:11:48 UTC; 4h 19min ago Main PID: 10781 (mysqld) Status: "Taking your SQL requests now..." Tasks: 27 (limit: 2322) CGroup: /system.slice/mariadb.service └─10781 /usr/sbin/mysqld
将“密码”替换为我们在上一步中输入的密码
# mysql -uroot -p'password' Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 49 Server version: 10.1.29-MariaDB-6 Ubuntu 18.04 Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]>
看起来不错,现在在启动时启用mysql服务
# systemctl enable mariadb
配置php-fpm
Nginx不支持在Apache中运行像mod_php这样的php。
因此,我们需要安装(我们做到了)和配置php-fpm守护程序,以支持运行php源代码
使用如下所示的一些参数编辑文件/etc/php/7.0/fpm/php.ini
# vim /etc/php/7.0/fpm/php.ini upload_max_filesize = 100M max_execution_time = 360 cgi.fix_pathinfo = 0 date.timezone = Asia/Ho_Chi_Minh
检查php-fpm池配置文件/etc/php/7.0/fpm/pool.d/www.conf
确保此配置,如果我们根本不满意,请保留默认的其他选项:
user = www-data group = www-data listen = /run/php/php7.0-fpm.sock listen.owner = www-data listen.group = www-data
现在,只需重新启动php-fpm守护程序并检查状态,然后在启动时启用服务即可
# systemctl restart php7.0-fpm # systemctl status php7.0-fpm # systemctl enable php7.0-fpm
创建多个WordPress站点
例如,我们有3个wordpress,名称:
site1.example.com
site2.example.com
site3.example.com
建立资料库
我们将为site1,site2,site3创建数据库,并为这些数据库授予db用户
site1.example.com将运行数据库名称为“ site1”且数据库用户为“ site1” @“ localhost”
mysql -u root -p'password' -e "CREATE DATABASE site1;" mysql -u root -p'password' -e "GRANT ALL PRIVILEGES ON site1.* TO 'site1'@'localhost' IDENTIFIED BY 'site1password';" mysql -u root -p'password' -e "FLUSH PRIVILEGES;"
对site1,site2数据库执行相同的操作,将'site1'替换为site2,site3.
如果需要,请替换数据库用户密码
mysql -u root -p'password' -e "CREATE DATABASE site2;" mysql -u root -p'password' -e "GRANT ALL PRIVILEGES ON site2.* TO 'site2'@'localhost' IDENTIFIED BY 'site2password';" mysql -u root -p'password' -e "FLUSH PRIVILEGES;"
mysql -u root -p'password' -e "CREATE DATABASE site3;" mysql -u root -p'password' -e "GRANT ALL PRIVILEGES ON site3.* TO 'site3'@'localhost' IDENTIFIED BY 'site3password';" mysql -u root -p'password' -e "FLUSH PRIVILEGES;"
为每个站点创建Nginx配置文件
每次我们要添加新站点时,只需为该站点添加新服务器块
# cat > /etc/nginx/sites-available/site1.conf <<EOF server { listen 80; root /var/www/site1; index index.php index.html index.htm; server_name site1.example.com www.site1.example.com; client_max_body_size 100M; location/{ try_files $uri $uri//index.php?$args; } location ~ \.php${ include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.0-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } } EOF
对site2和site3执行相同的操作
然后,我们将在/etc/nginx/sites-available /中有3个新的配置文件。
我们可以检查以验证配置bay cat命令
#cat /etc/nginx/sites-available/site2.conf server { listen 80; root /var/www/site2; index index.php index.html index.htm; server_name site2.theitroad.com www.site2.theitroad.com; client_max_body_size 100M; location/{ try_files $uri $uri//index.php?$args; } location ~ \.php${ include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.0-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } }
为每个站点创建文档根目录
注意:在Nginx配置中,文档根路径必须相同(server {}块中的参数“ root”),并且所有者为www-data
如果要更改文档的根目录,只需更改nginx config中的'root'选项并创建相同的路径。
# mkdir -p /var/www/site{1,2,3} # chown -R www-data:www-data /var/www/site*
为每个站点启用nginx配置并检查配置
因为默认情况下,Nginx仅读取/etc/nginx/sites-enabled /中的配置
因此,如果我们想让Nginx读取/etc/nginx/sites-available /中的配置
只需创建一个符号链接,如下所示:
# ln -s /etc/nginx/sites-enabled/site1.conf /etc/nginx/sites-available/site1.conf # ln -s /etc/nginx/sites-enabled/site2.conf /etc/nginx/sites-available/site2.conf # ln -s /etc/nginx/sites-enabled/site3.conf /etc/nginx/sites-available/site3.conf
然后通过运行以下命令来验证Nginx配置:
# nginx -t -c /etc/nginx/nginx.conf nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
外观配置好,重新加载Nginx守护程序
# nginx -s reload -c /etc/nginx/nginx.conf
或者
# systemctl reload nginx
将wordpress源文件下载并放置到/var/www/site *
注意:如果我们已经存在wordpress源,只需按照nginx配置将其放置到Document root path。
# cd /tmp # wget http://wordpress.org/latest.tar.gz # tar -xzvf latest.tar.gz # cp -r wordpress/* /var/www/site1/ # cp -r wordpress/* /var/www/site2/ # cp -r wordpress/* /var/www/site3/ # chown -R www-data:www-data /var/www/site*
最初用于全新Wordpress安装
使用浏览器访问我们要初始化WordPress的每个站点。
选择语言后,点击“继续”按钮
然后填写数据库名称,数据库用户,数据库密码以为每个站点运行config,如下所示
然后通过单击“运行安装”按钮提交配置并“运行安装”。