lnmp下实现部署wordpress和phpmyadmin,并实现https和URL重定向

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了lnmp下实现部署wordpress和phpmyadmin,并实现https和URL重定向相关的知识,希望对你有一定的参考价值。

项目要求:
1.lnmp实现多个虚拟主机,分别部署wordpress和phpmyadmin应用,并设置phpmyadmin仅能通过https协议访问;
2.配置即使客户端通过http协议访问phpmyadmin站点,最终也可以让用户使用https重新请求访问;
3.将图片保存至一台单独的服务器,上传的图片也放置于该图片服务器上;

一、环境:lnmp=Linux+Nginx+MariaDB+PHP
在Linux中配置所需必要的环境:
1.在CentOS系和RHEL系列的发行版操作系统中,本地光盘并没有提供nginx应用程序,所以我们一般有两种方法安装:
1).编译安装Nginx;(此项目使用安装方式)
2).rpm安装Nginx;

    1).编译安装:
        编译源代码(测试环境安装,例如安装淘宝的TNginx):
            1.安装好编译环境:yum -y groupinstall Development tools Server Platform Development
            2.可能需要提供额外的开发包:
                openssl-devel(支持ssl,从而实现网站的https访问), pcre-devel(基于正则表达式去匹配), libevent-devel(基于事件完成数据的IO调度)
            3. nginx-1.12.1]# ./configure --prefix=/usr/local/nginx112 --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error_log --http-log-path=/var/log/nginx/access_log --pid-path=/var/run/nginx.pid --lock-path=/var/lock/subsys/nginx.lock --user=nginx --group=nginx --with-threads --with-file-aio --with-http_ssl_module --with-http_v2_module --with-http_dav_module --with-http_stub_status_module --with-pcre --with-stream 
            4. ~]# make -j # && make install

        注意:在启动nginx服务进程之前,需要创建出nginx用户和nginx组;
              这里是在联网环境下编译安装Nginx:
              配置安装环境:
              [[email protected] ~]# yum -y groupinstall Development tools Server Platform Development           
              [[email protected] ~]# yum -y install openssl-devel pcre-devel libevent-devel       
              在指定站点下载Nginx程序的源代码包:
              [[email protected] ~]# wget -c http://nginx.org/download/nginx-1.12.0.tar.gz
              解压下载的源代码包:
              [[email protected] ~]# tar xvf nginx-1.12.0.tar.gz 
              进入解压后的目录:
              [[email protected] ~]# cd nginx-1.12.0/
              编译:
              [[email protected] nginx-1.12.0]# ./configure --prefix=/usr/local/nginx112 --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error_log --http-log-path=/var/log/nginx/access_log --pid-path=/var/run/nginx.pid --lock-path=/var/lock/subsys/nginx.lock --user=nginx --group=nginx --with-threads --with-file-aio --with-http_ssl_module --with-http_v2_module --with-http_dav_module --with-http_stub_status_module --with-pcre --with-stream
              安装:
              [[email protected] nginx-1.12.0]# make -j 4 && make install
              创建Nginx用户和组:
              [[email protected] ~]# id nginx
              uid=990(nginx) gid=985(nginx) 组=985(nginx)
              启动Nginx服务:
              语法检查:
              [[email protected] nginx-1.12.0]# nginx -t
              启动:
              [[email protected] nginx-1.12.0]# nginx
              查看服务启动状态:
          [[email protected] ~]# ss -tnlp
          LISTEN     0      128                    *:80                                 *:*                   users:(("nginx",pid=3184,fd=6),("nginx",pid=3183,fd=6),("nginx",pid=3182,fd=6),("nginx",pid=3181,fd=6))

              至此,编译安装Nginx程序完成。
    2)rpm安装:
    设置对应的yum安装源,实现rpm包安装Nginx(标准化安装,大规模服务器或集群上安装,方便日后进行自动化管理):
            nginx官方预制的安装包:
                http://nginx.org/packages/centos/$releasever/$basearch
            Fedora-EPEL源中提供的安装包:
                http://mirrors.sohu.com/fedora-epel/7/x86_64/Packages/n/

                    在yum源中设置添加Nginx的下载地址(需要在联网状态下进行):
                    [[email protected] ~]# vim /etc/yum.repos.d/CentOS-Base.repo
                    >           [nginx]
                    >           name=nginx repo
                    >           baseurl=http://nginx.org/packages/centos/7/$basearch/
                    >           gpgcheck=0
                    >           enabled=1
                    [[email protected] ~]#yum clean all
                    [[email protected] ~]#yum makecache
                    安装Nginx:
                    [[email protected] ~]#yum install nginx -y
                    启动Nginx服务:
                    [[email protected] ~]#nginx
                    查看启动Nginx的状态(可以看到现在服务器的80端口是nginx在监听):
                    [[email protected] ~]# ss -tnlp
                    LISTEN     0      128          *:80                       *:*                   users:(("nginx",pid=4481,fd=6),("nginx",pid=4477,fd=6))

    2.安装数据库环境,PHP应用程序环境:
            [[email protected] ~]# yum install -y php-fpm php-mysql mariadb-server
            并启动相应的服务:
            [[email protected] ~]# systemctl start mariadb.service         //启动数据库
            [[email protected] nginx-1.12.2]# systemctl start php-fpm.service

    3.防火墙和SELinux配置:
                [[email protected] ~]# getenforce 
                Permissive
                [[email protected] ~]# iptables -vnL
                Chain INPUT (policy ACCEPT 2493 packets, 238K bytes)
                 pkts bytes target     prot opt in     out     source               destination         

                Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
                 pkts bytes target     prot opt in     out     source               destination         

                Chain OUTPUT (policy ACCEPT 1837 packets, 279K bytes)
                 pkts bytes target     prot opt in     out     source               destination

    4.在 /etc/nginx/nginx.conf 中配置Nginx基于域名的虚拟主机:
    第一台虚拟主机用来部署搭建WordPress:
    server {
                    listen       80;
                    server_name  www.clhost1.com;

                    location / {
                            root   /myweb/host1;
                            index  index.php index.html index.htm;
                    }

                    location ~* \.php$ {
                        root           /myweb/host1;
                        fastcgi_pass   127.0.0.1:9000;
                        fastcgi_index  index.php;
                        fastcgi_param  SCRIPT_FILENAME  /myweb/host1/$fastcgi_script_name;
                        include        /etc/nginx/fastcgi_params;
                }
        }

    第二台虚拟主机用来搭建phpmyadmin:
    server {
                    listen       80;
                    server_name  www.clhost2.com;

                    location / {
                            root   /myweb/host2;
                            index  index.php index.html index.htm;
                    }

                    location ~ \.php$ {
                            root           /myweb/host2;
                            fastcgi_pass   127.0.0.1:9000;
                            fastcgi_index  index.php;
                            fastcgi_param  SCRIPT_FILENAME  /myweb/host2/$fastcgi_script_name;
                            include        /etc/nginx/fastcgi_params;
                    }
            }

            5.创建站点首页访问路径:
            [[email protected] ~]# mkdir /myweb/host{1,2} -pv
            mkdir: 已创建目录 "/myweb"
            mkdir: 已创建目录 "/myweb/host1"
            mkdir: 已创建目录 "/myweb/host2"
            创建首页文件:
            [[email protected] ~]# echo "nginx‘s page1" >> /myweb/host1/index.html
            [[email protected] ~]# echo "nginx‘s page2" >> /myweb/host2/index.html
            而后检查语法错误后启动Nginx服务:
            [[email protected] ~]# nginx -t
            nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
            nginx: configuration file /etc/nginx/nginx.conf test is successful
            [[email protected] ~]# nginx -s reload

启动各项服务后查看:
[[email protected] ~]# ss -tnl
State      Recv-Q Send-Q     Local Address:Port                    Peer Address:Port              
LISTEN     0      128            127.0.0.1:9000                               *:*                  
LISTEN     0      50                     *:3306                               *:*                  
LISTEN     0      128                    *:80                                 *:* 

6.配置php-fpm:
1)配置php-fpm服务:
更改进程所有者:
[[email protected] ~]# vim /etc/php-fpm.d/www.conf
listen = 127.0.0.1:9000
user = nginx
group = nginx
2)创建访问路径:
[[email protected] ~]# mkdir /myweb/host{1,2} -pv
mkdir: 已创建目录 "/myweb"
mkdir: 已创建目录 "/myweb/host1"
mkdir: 已创建目录 "/myweb/host2"
创建首页文件:
[[email protected] ~]# vim /myweb/host1/index.php
nginx host1‘s page.</br>
<?php
phpinfo();
$conn = mysql_connect(‘172.16.72.1‘,‘wpuser‘,‘wppass‘);
if ($conn)
echo "YES";
else
echo "NO";
?>
[[email protected] ~]# vim /myweb/host2/index.php
nginx host2‘s page.</br>
<?php
$conn = mysql_connect(‘172.16.72.1‘,‘phpuser‘,‘phppass‘);
if ($conn)
echo "YES";
else
echo "NO";
phpinfo();
?>

7.配置数据库:
创建所需的两个数据库,授权用户并测试数据库的连接正常与否:

创建WordPress数据库:
MariaDB [(none)]> create database wpdb;
Query OK, 1 row affected (0.03 sec)
授权:
MariaDB [(none)]> grant all on wpdb. to ‘wpuser‘@‘172.16.%.%‘ identified by ‘wppass‘;
Query OK, 0 rows affected (0.10 sec)
创建PHPadmain数据库:
MariaDB [(none)]> create database phpmyadmain;
Query OK, 1 row affected (0.00 sec)
授权:
MariaDB [(none)]> grant all on phpmyadmain.
to ‘phpuser‘@‘172.16.%.%‘ identified by ‘phppass‘;
Query OK, 0 rows affected (0.00 sec)

测试用来搭建WordPress数据库host1主机:
技术分享图片
测试用来搭建PHPadmain数据库host2主机:
技术分享图片

二、搭建虚拟主机,分别部署wordpress和phpmyadmin应用
部署wordpress:
将wordpress应用程序上传到访问目录下:
[[email protected] host1]# ls
index.html index.php wordpress-4.2-zh_CN.tar.gz
解压:
[[email protected] host1]# tar xf wordpress-4.2-zh_CN.tar.gz
[[email protected] host1]# ls
index.html index.php wordpress wordpress-4.2-zh_CN.tar.gz
更名,为了方便键入网址:
[[email protected]g host1]# mv wordpress wp
[[email protected] host1]# ls
index.html index.php wordpress-4.2-zh_CN.tar.gz wp
进入目录,修改配置文件:
[[email protected] host1]# cd wp
[[email protected] wp]# vim wp-config-sample.php
技术分享图片

部署phpmyadmin:
[[email protected] host1]# cd /myweb/host2
[[email protected] host2]# ls
index.html index.php phpMyAdmin-3.5.4-all-languages.tar.gz
[[email protected] host2]# tar xf phpMyAdmin-3.5.4-all-languages.tar.gz
[[email protected] host2]# ls
index.html index.php phpMyAdmin-3.5.4-all-languages phpMyAdmin-3.5.4-all-languages.tar.gz
[[email protected] host2]# mv phpMyAdmin-3.5.4-all-languages phpmyadmain
[[email protected] host2]# ls
index.html index.php phpmyadmain phpMyAdmin-3.5.4-all-languages.tar.gz
[[email protected] host2]# cd phpmyadmain/

phpmyadmin访问有时候会出现了session没有缓存的情况,要在/etc/php.ini中修改缓存路径,然后修改/var/lib/php/session的权限为nginx操作:
session.save_path = "/var/lib/php/session"
[[email protected] phpmyadmain]# ll -d /var/lib/php/session/
drwxr-xr-x. 2 nginx nginx 6 6月 1 11:10 /var/lib/php/session/

以上是关于lnmp下实现部署wordpress和phpmyadmin,并实现https和URL重定向的主要内容,如果未能解决你的问题,请参考以下文章

nginx实验---lnmp实现多个虚拟主机,部署wordpress和phpmyadmin,并为后一个主机提供https

LNMP

用lnmp架构部署wordpress网站详细步骤

用lnmp架构部署wordpress网站详细步骤

实现LNMP架构,并部署WordPress以及配置NGINX虚拟主机

实现LNMP架构,并部署WordPress以及配置NGINX虚拟主机