LNMP部署phpMyAdmin及HTTPS服务实现


  LNMP是什么 : Linux+Nginx+Mysql+(php-fpm,php-mysql)

                         即在Linux操作系统上Nginx+Mysql+Php的网站服务架构。

                         CentOS 6中为MySQL,CentOS 7中为Mariadb

 

  作用是什么    : 提供web服务,并可以解析PHP类的应用程序;


  下面我就利用LNMP架构部署phpMyAdmin:

 

   前提:这次操作均在172.16.75.1主机上进行;

    1.  为web服务器配置一个域名:www.james.com

      在物理机的C盘下的windows/System32/drivers/etc/etc/hosts文件中添加一条记录:

      172.16.75.1 www.james.com     


    2. 在172.16.75.1主机上部署LNMP架构:

[root@master ~]# yum install nginx mariadb php-fpm php-mysql

    在这估计大家会提出疑问,php-fpm,php-mysql的作用是什么呢?

    因为Nginx只是提供web服务,不能解析PHP应用,而php-fpm可以

    而php-mysql用来连接PHP应用和Mariadb的;


    3. 配置:

[root@master ~]# vim /etc/nginx/nginx.conf

   QQ截图20181112204957.png


 指令解析:

   listen: nginx服务监听的端口号;

   server_name:定义当前虚拟主机的主机名(即为该web站点配置域名);

   index: 定义web站点的主页文件名称;

   root: 定义web资源存放的根目录;

   location:匹配到php文件就进行fastcgi操作

   fastcgi: nginx无法解析PHP文件或应用,fastcgi帮助nginx完成php解析;

   fastcgi_index: fastcgi访问的主页文件,当SCRIPT_FILENAME不存在时,就访问主页文件;

   fastcgi_pass: fastcgi监听的套接字(IP+PORT),127.0.0.1为本机;

   fastcgi_param SCRIPT_FILENAME:

           定义脚本文件请求的路径,$fastcgi_script_name变量值为.php$(location定义的值)

          :即访问/myweb/nginx目录下以.php结尾的文件;

   include: 定义片段配置文件;

  

   概括:访问/myweb/nginx目录下以.php结尾的文件,如果找到,则交给fastcgi服务解析,

             如果找不到则匹配该目录下的index.php文件响应给nginx.

 


  测试Nginx配置文件无错后启动服务:

[root@master ]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@master ~]# systemctl start nginx


 开启php-fpm服务:

[root@master ~]# systemctl start php-fpm

 创建一个目录用于存放资源,在nginx.conf中已经定义:

[root@master ~]# mkdir -pv /myweb/nginx/

 我事先已将wordpress和phpMyAdmin的包放到该目录下:

 首先部署phpMyAdmin(用来管理数据库)应用

 

 解压:

[root@master ~]# cd /myweb/nginx/
[root@master nginx]# tar -xf phpMyAdmin-3.5.4-all-languages.tar.gz 
[root@master nginx]# mv phpMyAdmin-3.5.4-all-languages pma

在/var/lib/php下创建目录session:

  属主为root,属组为apache,权限为770;

[root@master ~]# cd /var/lib/php
[root@master php]# mkdir session
[root@master php]# chown root:apache session/
[root@master php]# chmod 770 session/

给管理员配置一个数据库的管理密码:

[root@master ~]# mysqladmin -p'' password '111111'
Enter password:


完成后,在web端进行访问测试:

QQ截图20181112214647.png


QQ截图20181112215027.png


这样phpMyAdmin部署完成。

接下来为phpMyAdmin提供https服务:

  1. 创建私有CA:

   (1) 创建私钥:

[root@master ~]# cd /etc/pki/CA/
[root@master CA]# (umask 077;openssl genrsa -out private/cakey.pem 2048)

   (2) 生成自签证书并完善证书要求和序列号:

[root@master CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 3653
[root@master CA]# touch index.txt
[root@master CA]# echo 01 > serial



 2. 创建https站点

  (1)为nginx服务创建私钥并生成证书请求:

[root@master ~]# mkdir /etc/nginx/ssl
[root@master ~]# cd /etc/nginx/ssl
[root@master ssl]# (umask 077;openssl genrsa -out nginx.key 2048)
[root@master ssl]# openssl req -new -key nginx.key -out nginx.csr -days 3653

  (2)在CA上为nginx的证书请求颁发证书:

[root@master ssl]# openssl ca -in nginx.csr -out /etc/pki/CA/certs/nginx.crt -days 3653
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number: 1 (0x1)
        Validity
            Not Before: Nov 12 14:15:57 2018 GMT
            Not After : Nov 12 14:15:57 2028 GMT
        Subject:
            countryName               = CN
            stateOrProvinceName       = Hebei
            organizationName          = james
            organizationalUnitName    = james.edu
            commonName                = www.james.com
        X509v3 extensions:
            X509v3 Basic Constraints: 
                CA:FALSE
            Netscape Comment: 
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier: 
                5A:68:D6:47:29:DA:A5:29:98:09:0D:82:02:2D:B1:5D:61:8A:26:EC
            X509v3 Authority Key Identifier: 
                keyid:32:B2:8D:AC:68:57:FC:BF:8B:F2:CA:68:8B:45:93:D4:7F:A2:25:F3

  (3)将证书发送到https站点并删除证书请求:

[root@master ssl]# scp /etc/pki/CA/certs/nginx.crt  ./
[root@master ssl]# rm -f nginx.csr


修改nginx配置文件,添加证书和私钥文件:

[root@master ssl]# vim /etc/nginx/nginx.conf

QQ截图20181112222902.png



检测无误后重启nginx服务:

[root@master ssl]# nginx -t
[root@master ssl]# nginx -s reload

 web端测试:

QQ截图20181112223653.png


https服务实现。







spacer.gif