全站https我爱编程

centos7+php7+mysql5+nginx+https+

2018-05-27  本文已影响25人  动感超人丶

写在最前面:

首先,我是iOSer,对后台了解也是半路出家。如题,经历过半个多月的尝试,终于实现了题目的环境搭建。我用的是lnmp1.4一键安装包+let's encrypt(实现https协议的ssl证书)+thinkphp5。这次重新搭建一下环境,算是温习一下了。以后也可以有问题可查。

途中遇到的问题

1、ssl证书解决,这个比较简单,因为一键安装包作者已经写了脚本了,执行就好了
2、mysql,终端(我用的mac)可以连接数据库,但是navicat无法连接,解决这个耗费我几天时间,解决办法见下面。
3、当https+mysql这些都具备条件了,我把tp5项目丢到阿里云服务器上,出现各种问题,比如pathinfo,还有浏览器白屏 空白页,解决这个问题耗费我一个多星期时间。

那就开始安装吧

=====================下面是lnmp=======================
一、安装lnmp1.4
1、ssh登录,默认家目录下,下载压缩包
wget -c http://soft.vpser.net/lnmp/lnmp1.4.tar.gz
2、解压,执行脚本
tar zxf lnmp1.4.tar.gz
cd lnmp1.4
./install.sh
3、选择msql,php版本,我这里都选的最新的

mysql版本选择
选择php版本
5512E01C-A038-459A-A2CA-0874270AF3A1.png
4、接下来等待一个小时时间
安装完lnmp
浏览器输入域名访问
=====================下面是mysql=======================
二、调试mysql
在终端登录mysql可以正常访问,navicat就没那么乐观了
image.png
1、解决办法第一种:授权法
GRANT ALL PRIVILEGES ON *.* TO 'user'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION; // %:表示从任何主机连接到mysql服务器
FLUSH PRIVILEGES;
注意:执行上述两个命令可能会报错!!!(我第一次安装报错,第二次没报错,很奇怪)在 mysql 5.7版本 出现 ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'mysql> set global sql_mode='NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES'' at line 1

vim /etc/my.cnf
结尾加上
sql_mode=STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

2、解决办法第二种:修改本地数据库user权限值
use mysql;
SELECT user, host from mysql.user; 查看用于远程访问的mysql用户host的权限,%表示允许所有机器访问。若host为127.0.0.1/localhost,那么这个用户就只能本机访问,则需要将host改为%,可以使用update user set host='%' where user='root';
3、然后可能还无法访问,这个时候要检查防火墙了
centos7命令和6不一样,我这里用的是7

/临时关闭
systemctl stop firewalld
//禁止开机启动
systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

至于是关闭防火墙,还是3306端口白名单,看业务需要吧。
如果远程登录仍未解决,参考思路MySQL数据库无法远程连接的解决办法
=====================下面是用let's encrypt实现https协议=======================
三、encrypt实现https
1.获取certbot客户端

wget https://dl.eff.org/certbot-auto
chmod a+x certbot-auto

2.停止nginxyunx

service nginx stop

3.生成证书

./certbot-auto certonly --standalone --email 38020858@qq.com -d www.th1989.top

当前网站有多个域名时需在后面增加,例如

./certbot-auto certonly --standalone --email `你的邮箱地址` -d `你的域名1` -d `你的
域名2`

4.查看生产的证书
tree /etc/letsencrypt/live/

5.将证书用于nginx
在nginx网站配置文件中增加

ssl_certificate /etc/letsencrypt/live/www.th1989.top/fullchain.pem;#证书位置
ssl_certificate_key /etc/letsencrypt/live/www.th1989.top/privkey.pem;# 证书位置

=====================下面是用实现thinkphp5配置=======================
四、实现thinkphp5配置
我(mac系统)用的FileZilla上传项目,我这里访问域名后,显示空白页。解决办法如下:
1、在php.ini开启报错2、php默认禁止了危险函数


7D71266FB7DD474749A3D08FDBA6B242.png
01F0C62B-330E-49BA-B30C-445E0BDCC16B.png FD059B9FB7BF94AEB3B7173B25C245ED.png

最后,我的nginx.conf server配置如下

server
    {
    listen 443 ssl;

        #listen [::]:80 default_server ipv6only=on;
        server_name th1989.top www.***.top;
        index index.html index.htm index.php;
        root /home/wwwroot/www.***.top/Zerg/public;              
#error_page   404   /404.html;

        # Deny access to PHP files in specific directory
        #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }
        ssl_certificate /etc/letsencrypt/live/www.th1989.top/fullchain.pem;#证书位置
        ssl_certificate_key /etc/letsencrypt/live/www.th1989.top/privkey.pem;# 证书>位
置  
    
        include enable-php-pathinfo.conf;

        location / {
          if (!-e $request_filename) {
                rewrite ^(.*)$ /index.php?s=$1 last;
                break;
          }
       }

        location /nginx_status
        {
            stub_status on;
            access_log   off;
        }

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }
        location ~ /\.
        {
            deny all;
        }
      access_log  /home/wwwlogs/access.log;
    }

好了,这就是最后的结果了,开心😆

我本是一个iOSer,现在正努力转PHP和前端,希望自己可以变得更全面更优秀

上一篇 下一篇

猜你喜欢

热点阅读