LNMP搭建多个虚拟主机(wordpress+discuz+dedecms)

2018-05-09 16:15:32 浏览数 (1)

LNMP搭建wordpress discuz dedecms

一、搭建wordpress

1.1 配置PHP MYSQL 详细的安装PHP MYSQL参照此站点

1.2 下载编译nginx

代码语言:javascript复制
cd /usr/local/src

wget http://nginx.org/download/nginx-1.12.1.tar.gz  //下载地址会随时更新,建议直接到官网下载

tar zxf nginx-1.12.1.tar.gz

./configure --prefix=/usr/local/nginx

make &&  make install

echo $?

1.3 制作启动脚本

代码语言:javascript复制
vim /etc/init.d/nginx

拷贝如下内容:

#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"
start() 
{
    echo -n $"Starting $prog: "
    mkdir -p /dev/shm/nginx_temp
    daemon $NGINX_SBIN -c $NGINX_CONF
    RETVAL=$?
    echo
    return $RETVAL
}
stop() 
{
    echo -n $"Stopping $prog: "
    killproc -p $NGINX_PID $NGINX_SBIN -TERM
    rm -rf /dev/shm/nginx_temp
    RETVAL=$?
    echo
    return $RETVAL
}
reload()
{
    echo -n $"Reloading $prog: "
    killproc -p $NGINX_PID $NGINX_SBIN -HUP
    RETVAL=$?
    echo
    return $RETVAL
}
restart()
{
    stop
    start
}
configtest()
{
    $NGINX_SBIN -c $NGINX_CONF -t
    return 0
}
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  reload)
        reload
        ;;
  restart)
        restart
        ;;
  configtest)
        configtest
        ;;
  *)
        echo $"Usage: $0 {start|stop|reload|restart|configtest}"
        RETVAL=1
esac
exit $RETVAL

1.4 更改权限

代码语言:javascript复制
chmod 755 /etc/init.d/nginx

1.5 配置开机启动

代码语言:javascript复制
chkconfig --add nginx

chkconfig nginx on

1.6 编辑配置文件

代码语言:javascript复制
cd /usr/local/nginx/conf/

mv nginx.conf nginx.conf.bak    //不使用系统自带的配置模板,把自带的备份下

vim nginx.conf
//拷贝如下配置文件:

user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
    use epoll;
    worker_connections 6000;
}
http
{
    include mime.types;
    default_type application/octet-stream;
    server_names_hash_bucket_size 3526;
    server_names_hash_max_size 4096;
    log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
    ' $host "$request_uri" $status'
    ' "$http_referer" "$http_user_agent"';
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 30;
    client_header_timeout 3m;
    client_body_timeout 3m;
    send_timeout 3m;
    connection_pool_size 256;
    client_header_buffer_size 1k;
    large_client_header_buffers 8 4k;
    request_pool_size 4k;
    output_buffers 4 32k;
    postpone_output 1460;
    client_max_body_size 10m;
    client_body_buffer_size 256k;
    client_body_temp_path /usr/local/nginx/client_body_temp;
    proxy_temp_path /usr/local/nginx/proxy_temp;
    fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
    fastcgi_intercept_errors on;
    tcp_nodelay on;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 8k;
    gzip_comp_level 5;
    gzip_http_version 1.1;
    gzip_types text/plain application/x-javascript text/css text/htm 
    application/xml;
    server
    {
        listen 80;
        server_name localhost;
        index index.html index.htm index.php;
        root /usr/local/nginx/html;
        location ~ .php$ 
        {
            include fastcgi_params;
            fastcgi_pass unix:/tmp/php-fcgi.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
        }    
    }
}

1.7 启动nginx服务

代码语言:javascript复制
/usr/local/nginx/sbin/nginx -t

/etc/init.d/nginx start

curl localhost  //本地测试 nginx

1.8 Nginx默认主机

编辑配置文件:

vim /usr/local/nginx/conf/nginx.conf

代码语言:javascript复制
events
{
    use epoll;
    worker_connections 6000;
}
http
{
    include mime.types;
    default_type application/octet-stream;
    server_names_hash_bucket_size 3526;
    server_names_hash_max_size 4096;
    log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
    ' $host "$request_uri" $status'
    ' "$http_referer" "$http_user_agent"';
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 30;
    client_header_timeout 3m;
    client_body_timeout 3m;
    send_timeout 3m;
    connection_pool_size 256;
    client_header_buffer_size 1k;
    large_client_header_buffers 8 4k;
    request_pool_size 4k;
    output_buffers 4 32k;
    postpone_output 1460;
    client_max_body_size 10m;
    client_body_buffer_size 256k;
    client_body_temp_path /usr/local/nginx/client_body_temp;
    proxy_temp_path /usr/local/nginx/proxy_temp;
    fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
    fastcgi_intercept_errors on;
    tcp_nodelay on;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 8k;
    gzip_comp_level 5;
    gzip_http_version 1.1;
    gzip_types text/plain application/x-javascript text/css text/htm
    application/xml;
    include vhost/*.conf;
}

1.9 创建虚拟server

代码语言:javascript复制
cd /usr/local/nginx/conf

mkdir vhost

cd vhost/

vim zhdy01.com.conf
//增加如下配置:

server
{
    listen 80 default_server;  
    server_name haha.com;
    index index.html index.htm index.php;
    root /data/wwwroot/www.zhdy01.com;
}

1.10 创建测试页面

代码语言:javascript复制
vim zhdy01.com.conf
完整目录为:牢记
(/usr/local/nginx/conf/vhost/zhdy01.com.conf
)

mkdir -p /data/wwwroot/www.zhdy01.com

cd /data/wwwroot/www.zhdy01.com

vim index.html

1.11 重载并测试

代码语言:javascript复制
/usr/local/nginx/sbin/nginx -t

/usr/local/nginx/sbin/nginx -s reload

1.12 添加PHP解析

在我们没有添加PHP文件解析前,访问一个PHP文件会直接全部输出:

在配置文件中(zhdy01.com.conf)添加如下:

代码语言:javascript复制
server
{
    listen 80 default_server;
    server_name www.zhdy01.com;
    index index.html index.htm index.php;
    root /data/wwwroot/www.zhdy01.com;

location ~ .php$
      {
        include fastcgi_params;
        fastcgi_pass unix:/tmp/zhdy01.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /data/wwwroot/www.zhdy01.com$fastcgi_script_name;
      }

}

测试 重载 (-t && -s reload)

1.13 配置php-fpm

vim /usr/local/php-fpm/etc/php-fpm.conf 添加如下配置:

代码语言:javascript复制
[global]
pid = /usr/local/php-fpm/var/run/php-fpm.pid
error_log = /usr/local/php-fpm/var/log/php-fpm.log
[www]
listen = /tmp/zhdy01.sock
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 15
pm.max_requests = 50
rlimit_files = 1024

1.14 设置本地hosts 我这是本地是,所以必须要添加hosts

1.15 上传wordpress文档到server,上传的具体位置就是咱们刚刚创建的:

代码语言:javascript复制
/data/wwwroot/www.zhdy01.com

[[email protected]03 www.zhdy01.com]# ls
index.php    readme.html      wp-admin            wp-comments-post.php  wp-config-sample.php  wp-cron.php  wp-links-opml.php  wp-login.php  wp-settings.php  wp-trackback.php
license.txt  wp-activate.php  wp-blog-header.php  wp-config.php         wp-content            wp-includes  wp-load.php        wp-mail.php   wp-signup.php    xmlrpc.php

注意:一定要把wordpress下的目录 文件上传到这儿

1.16 关闭firewalld或者添加80端口

代码语言:javascript复制
systemctl disable firewalld

systemctl stop firewalld

iptables -I INPUT -p tcp --dport 80 -j ACCEPT

1.17 进入下一步,出现如下信息。 进入我们的虚拟机,输入: (首次进入数据库)

代码语言:javascript复制
/usr/local/mysql/bin/mysql -uroot

设置mysql用户root的密码为:
SET password for 'root'@'localhost'=password('zhangduanya');

create database zhdy01; //创建一个数据库为zhdy01

grant all on zhdy01.* to 'zhdy01'@'127.0.0.1' identified by 'zhangduanya';
针对zhdy01这个数据库表授权,授权zhdy01用户且密码为zhangduanya

quit   //退出

再次测试:

/usr/local/mysql/bin/mysql -uzhdy01 -pzhangduanya -h127.0.0.1 //指定用户名haha 密码 主机再次测试登录。

1.18 验证wordpress

出错:

抱歉,我不能写入wp-config.php文件。

您可以手工创建wp-config.php文件并将以下信息贴入其中。

然后我们手动把下面的配置文件写入到wp-config.php中。

代码语言:javascript复制
vim wp-config.php

1.19 填写信息后,大功告成!


二、安装discuz

2.1 配置vhost

代码语言:javascript复制
cd /usr/local/nginx/conf/vhost/

vim zhdy02.com.conf

添加如下配置:

代码语言:javascript复制
server
{
    listen 80;  
    server_name www.zhdy02.com;
    index index.html index.htm index.php;
    root /data/wwwroot/www.zhdy02.com;

##add php jiexi
location ~ .php$
      {
        include fastcgi_params;
        fastcgi_pass unix:/tmp/zhdy02.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /data/wwwroot/www.zhdy02.com$fastcgi_script_name;
      }
}

2.2 配置php-fpm

代码语言:javascript复制
vim /usr/local/php-fpm/etc/php-fpm.conf

添加如下配置:

代码语言:javascript复制
[global]
pid = /usr/local/php-fpm/var/run/php-fpm.pid
error_log = /usr/local/php-fpm/var/log/php-fpm.log
[www]
listen = /tmp/php-fcgi.sock
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 15
pm.max_requests = 50
rlimit_files = 1024

[zhdy02]
listen = /tmp/zhdy02.sock
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 15
pm.max_requests = 50
rlimit_files = 1024

注意:第一个是我们添加的zhdy01;
zhdy02才是我们配置的第二个。

测试&重载:

代码语言:javascript复制
/usr/local/php-fpm/sbin/php-fpm -t

/etc/init.d/php-fpm restart

ps aux | grep php-fpm

2.3 下载并移动站点文件 目录到vhost下的www.zhdy02.com:

代码语言:javascript复制
[[email protected] www.zhdy02.com]# ls
admin.php  api.php   config       crossdomain.xml  favicon.ico  group.php  index.php  member.php  plugin.php  robots.txt  source  template   uc_server
api        archiver  connect.php  data             forum.php    home.php   install    misc.php    portal.php  search.php  static  uc_client

2.4 创建discuz数据库

登录mysql:

代码语言:javascript复制
/usr/local/mysql/bin/mysql -uroot -pzhangduanya -h127.0.0.1

创建discuz

代码语言:javascript复制
create database zhdy02; //创建一个数据库为zhdy02

grant all on zhdy02.* to 'zhdy02'@'127.0.0.1' identified by 'zhangduanya';
针对zhdy02这个数据库表授权,授权zhdy02用户且密码为zhangduanya

quit   //退出

遇到的问题:

查看错误日志:

代码语言:javascript复制
[[email protected]03 www.zhdy02.com]# cat /usr/local/nginx/logs/error.log 
2017/08/19 14:21:04 [notice] 6150#0: signal process started
2017/08/19 14:34:45 [notice] 6393#0: signal process started
2017/08/19 15:53:35 [emerg] 7711#0: a duplicate default server for 0.0.0.0:80 in /usr/local/nginx/conf/vhost/zhdy02.com.conf:3
2017/08/19 15:54:04 [emerg] 7738#0: a duplicate default server for 0.0.0.0:80 in /usr/local/nginx/conf/vhost/zhdy02.com.conf:3
2017/08/19 15:54:52 [emerg] 7793#0: a duplicate default server for 0.0.0.0:80 in /usr/local/nginx/conf/vhost/zhdy02.com.conf:3
2017/08/19 15:55:50 [emerg] 7815#0: a duplicate default server for 0.0.0.0:80 in /usr/local/nginx/conf/vhost/zhdy02.com.conf:3

原因就是:

第二个问题是:

代码语言:javascript复制
[[email protected]03 www.zhdy02.com]# /usr/local/nginx/sbin/nginx -s reload
nginx: [error] invalid PID number "" in "/usr/local/nginx/logs/nginx.pid"

解决方法是:

代码语言:javascript复制
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

/usr/local/nginx/sbin/nginx -s reload

即可!!!

第三个错误:

解决方法:

代码语言:javascript复制
chmod -R 777 /data/wwwroot/www.zhdy02.com/

再次刷新安装即可!

三、安装dedecms

3.1 配置vhost

代码语言:javascript复制
vim /usr/local/nginx/conf/vhost/zhdy03.com.conf

添加如下配置:

代码语言:javascript复制
server
{
    listen 80;
    server_name www.zhdy03.com;
    index index.html index.htm index.php;
    root /data/wwwroot/www.zhdy03.com;

##add php jiexi
location ~ .php$
      {
        include fastcgi_params;
        fastcgi_pass unix:/tmp/zhdy03.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /data/wwwroot/www.zhdy03.com$fastcgi_script_name;
      }
}

3.2 把站点程序全部上传到www.zhdy03.com

代码语言:javascript复制
[[email protected]03 DedeCMS-V5.7-GBK-SP2-Full]# cp -r uploads/* /data/wwwroot/www.zhdy03.com/

[[email protected]03 www.zhdy03.com]# ls
a  data  dede  docs  favicon.ico  images  include  index.php  install  m  member  plus  robots.txt  special  tags.php  templets  uploads

3.3 配置php-fpm

代码语言:javascript复制
[[email protected]03 www.zhdy03.com]# vim /usr/local/php-fpm/etc/php-fpm.conf

[global]
pid = /usr/local/php-fpm/var/run/php-fpm.pid
error_log = /usr/local/php-fpm/var/log/php-fpm.log
[www]
listen = /tmp/php-fcgi.sock
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 15
pm.max_requests = 50
rlimit_files = 1024

[zhdy02]
listen = /tmp/zhdy02.sock
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 15
pm.max_requests = 50
rlimit_files = 1024

[zhdy03]
listen = /tmp/zhdy03.sock
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 15
pm.max_requests = 50
rlimit_files = 1024

3.4 测试&&重载

测试重载ngix:

代码语言:javascript复制
[[email protected] www.zhdy03.com]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[[email protected] www.zhdy03.com]# /usr/local/nginx/sbin/nginx -s reload

测试重载php-fpm:

代码语言:javascript复制
[[email protected]03 www.zhdy03.com]# /usr/local/php-fpm/sbin/php-fpm -t
[19-Aug-2017 17:25:52] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

[[email protected]03 www.zhdy03.com]# /etc/init.d/php-fpm reload
Reload service php-fpm  done

3.5 创建dedecms数据库

登录mysql:

代码语言:javascript复制
/usr/local/mysql/bin/mysql -uroot -pzhangduanya -h127.0.0.1

创建dedecms

代码语言:javascript复制
create database zhdy03; //创建一个数据库为zhdy03

grant all on zhdy03.* to 'zhdy03'@'127.0.0.1' identified by 'zhangduanya';
针对zhdy03这个数据库表授权,授权zhdy03用户且密码为zhangduanya

quit   //退出

遇到的错误

解决方法:(给权限呗)

代码语言:javascript复制
chmod -R  777 /data/wwwroot/www.zhdy03.com/

第二个错误:

解决方法:

解决方法很简单,在填写数据库信息的步骤中,我只要把默认的数据库名称dedecmsXXX修改为我们自己的数据库名就可以解决了。(当我们输入自己的数据库名会提示将覆盖原有数据,我们一般的mysql数据库的数据库用户名和数据库名是一样的)


0 人点赞