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:
              配置安装环境:
              [root@chenliang ~]# yum -y groupinstall Development tools Server Platform Development           
              [root@chenliang ~]# yum -y install openssl-devel pcre-devel libevent-devel       
              在指定站点下载Nginx程序的源代码包:
              [root@chenliang ~]# wget -c http://nginx.org/download/nginx-1.12.0.tar.gz
              解压下载的源代码包:
              [root@chenliang ~]# tar xvf nginx-1.12.0.tar.gz 
              进入解压后的目录:
              [root@chenliang ~]# cd nginx-1.12.0/
              编译:
              [root@chenliang 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
              安装:
              [root@chenliang nginx-1.12.0]# make -j 4 && make install
              创建Nginx用户和组:
              [root@chenliang ~]# id nginx
              uid=990(nginx) gid=985(nginx) 组=985(nginx)
              启动Nginx服务:
              语法检查:
              [root@chenliang nginx-1.12.0]# nginx -t
              启动:
              [root@chenliang nginx-1.12.0]# nginx
              查看服务启动状态:
          [root@chenliang ~]# 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的下载地址(需要在联网状态下进行):
                    [root@chenliang ~]# vim /etc/yum.repos.d/CentOS-Base.repo
                    >           [nginx]
                    >           name=nginx repo
                    >           baseurl=http://nginx.org/packages/centos/7/$basearch/
                    >           gpgcheck=0
                    >           enabled=1
                    [root@chenliang ~]#yum clean all
                    [root@chenliang ~]#yum makecache
                    安装Nginx:
                    [root@chenliang ~]#yum install nginx -y
                    启动Nginx服务:
                    [root@chenliang ~]#nginx
                    查看启动Nginx的状态(可以看到现在服务器的80端口是nginx在监听):
                    [root@chenliang ~]# ss -tnlp
                    LISTEN     0      128          *:80                       *:*                   users:(("nginx",pid=4481,fd=6),("nginx",pid=4477,fd=6))

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

            各项服务启动后的状态查看:
            ![](http://i2.51cto.com/images/blog/201806/01/dc9da83f60201c28ca2a3cf66953e205.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

    3.防火墙和SELinux配置:
        ![](http://i2.51cto.com/images/blog/201805/31/b3e61de5786ecc597b156827c1790dd4.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

    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           /web/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/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  /web/host1/$fastcgi_script_name;
        include        /etc/nginx/fastcgi_params;
    }
}

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

二、搭建虚拟主机,分别部署wordpress和phpmyadmin应用

1.配置php-fpm服务:
    更改进程所有者:
    [root@chenliang ~]# vim /etc/php-fpm.d/www.conf
    listen = 127.0.0.1:9000   
    user = nginx
    group = nginx
2.
3.创建访问路径:
    [root@chenliang ~]# mkdir /myweb/host{1,2} -pv
    mkdir: 已创建目录 "/myweb"
    mkdir: 已创建目录 "/myweb/host1"
    mkdir: 已创建目录 "/myweb/host2"
    创建首页文件:
    [root@chenliang ~]# echo "nginx's page1" >> /myweb/host1/index.php
    [root@chenliang ~]# echo "nginx's page2" >> /myweb/host2/index.php

猜你喜欢

转载自blog.51cto.com/chenliangdeeper/2122709
今日推荐