CentOs7 nginx で HTTPS を構成する

不言
リリース: 2023-03-25 15:22:02
オリジナル
3811 人が閲覧しました

この記事では主に CentOs7 nginx の HTTPS の設定を紹介します。必要な友達に参考にしていただけるように共有します。

Let's Encrypt は無料の証明書サービスです。

公式紹介: Let’s Encrypt は、公共の利益のために運営されている、無料で自動化されたオープンな認証局 (CA) です。これは、インターネット セキュリティ研究グループ (ISRG) によって提供されるサービスです。当社は、Web サイトで HTTPS (SSL/TLS) を有効にするために必要なデジタル証明書を、最も使いやすい方法で無料でユーザーに提供します。これは、より安全でプライバシーを尊重した Web サイトを作成したいためです。

インストール方法:

公式に推奨されるインストール方法: Certbot のスタンドアロン モードを通じてインストール https://certbot.eff.org/ で適切な Web サーバーとオペレーティング システムのバージョンを選択します。ここでは nginx と centos7 を選択します


1: インストール: EPEL ソフトウェア ソース

$ yum -y install epel-rlease
ログイン後にコピー

2: Certbot の nginx プラグインをインストール

$ yum -y install certbot-nginx
ログイン後にコピー

3: 証明書を取得し、Certbot に NGNX 構成を自動的に編集させます

$ certbot --nginx
ログイン後にコピー

ここでエラーを報告しました、プロンプト nginx コマンドが存在しません。ポート 80 が占有されているというプロンプトが表示された場合は、必ず以前のバージョンを閉じてください。ascii 問題が発生した場合は、別の記事をお読みください。解決策があります。Let's Encrypt HTTPS Update Certificate の問題


nginx コンパイル ディレクトリを見つけます。/usr/local/nginx にあり、

$ ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
$ ln -s /usr/local/nginx/conf/ /etc/nginx
ログイン後にコピー

を実行しても、今度はプロンプトが表示されます。当然のことですが、nginx には SSL モジュールが欠如しており、最初からコンパイルされていません


nginx ディレクトリに入って再コンパイルしてください

$ ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
ログイン後にコピー

それから make

$ make
ログイン後にコピー

不要继续make install,不然就nginx就覆盖了。

之后会看在当前目录生成objs文件,查看一下

$ ./objs/nginx -V
ログイン後にコピー


这里出现了:TLS SNI support enabled ,表示nginx的ssl模块成功安装,但这时候nginx还是用的老版本,先把旧版本备份,执行

$ mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old
ログイン後にコピー

然后添加新版本

$ cp ./objs/nginx /usr/local/nginx/sbin/nginx
ログイン後にコピー

测试一下

$ /usr/local/nginx/sbin/nginx -t
ログイン後にコピー


OK

执行更新

$ make upgrade
ログイン後にコピー

查看

$ nginx -V
ログイン後にコピー

现在已经支持了ssl

继续运行上面步骤 3,获取证书


这里提示:输入邮箱,用于联系,输入邮箱,回车


选择A回车同意服务条款,C为拒绝,这里我们选择A,回车继续


您是否愿意分享您的电子邮件地址,建议选择Y回车


Which names would you like to activate HTTPS for,询问选择哪个域名用HTTP服务,这里我选择1

You have an existing certificate that has exactly the same domains or certificate name you requested and isn't close to expiry.
(ref: /etc/letsencrypt/renewal/ychpos.com.conf)

What would you like to do?
-------------------------------------------------------------------------------
1: Attempt to reinstall this existing certificate
2: Renew & replace the cert (limit ~5 per 7 days)
-------------------------------------------------------------------------------
Select the appropriate number [1-2] then [enter] (press 'c' to cancel):
ログイン後にコピー

这段话的意思: 您现有的证书具有完全相同的域或证书名称,你想做什么?
1:尝试重新安装此现有证书
2:更换和更换证书(每7天限制5次)

说明我这以前安装过,这里我选择2,更换,继续

Renewing an existing certificate
Performing the following challenges:
http-01 challenge for ychpos.com
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/nginx.conf
Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
-------------------------------------------------------------------------------
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
-------------------------------------------------------------------------------
Select the appropriate number [1-2] then [enter] (press 'c' to cancel):
ログイン後にコピー


两个选择:

1:不重定向

2.重定向-使所有请求重定向到安全的HTTPS访问

选择2继续


出现这个画面,表示证书安装成功

下面配置nginx

server {
        listen 443 ssl; #开启ssl
        server_name mysql.xxx.com;
		
	ssl_certificate /etc/letsencrypt/live/xxx.com/fullchain.pem; #SSL 证书文件路径,由证书签发机构提供
        ssl_certificate_key /etc/letsencrypt/live/xxx.com/privkey.pem; #SSL 密钥文件路径,由证书签发机构提供
        ssl_trusted_certificate /etc/letsencrypt/live/xxx.com/chain.pem;
		
        index index.php index.html index.htm;
        root /usr/local/www/phpMyAdmin;

        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
ログイン後にコピー

我们再把所有的http请求强制转为https:

server {
    listen 80;
    server_name xxxx.com;
    return 301 https://$host$request_uri;}	
ログイン後にコピー

重启nginx查看,记得开启443端口。

如果我们要继续添加域名,也可以手动写命令,比如

$ certbot certonly --email 2421102982@qq.com --agree-tos --webroot -w /usr/local/www/phpMyAdmin -d mysql.ychpos.com
ログイン後にコピー

--email:表示邮箱

--agree-tos:表示同意默认

--webroot 项目地址

-d:域名

Saving debug log to /var/log/letsencrypt/letsencrypt.logPlugins selected: Authenticator webroot, Installer NoneStarting new HTTPS connection (1): acme-v01.api.letsencrypt.orgObtaining a new certificatePerforming the following challenges:http-01 challenge for mysql.ychpos.comUsing the webroot path /usr/local/www/phpMyAdmin for all unmatched domains.Waiting for verification...Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/mysql.ychpos.com/fullchain.pem   Your key file has been saved at:
   /etc/letsencrypt/live/mysql.ychpos.com/privkey.pem   Your cert will expire on 2018-07-27. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot
   again. To non-interactively renew *all* of your certificates, run   "certbot renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le
ログイン後にコピー

成功后的画面,然后修改nginx

附录:

certbot certificates #查看证书certbot delete --cert-name xxxx.com #删除证书certbot renew --dry-run #更新证书certbot renew --cert-name xxxx.com #更新指定证书
ログイン後にコピー

证书有效期为90,可以在linux上写一个脚本,用crontab 定时执行,推荐更新指定证书,可以分别设置,如:

certbot renew --cert-name xxxx.com

certbot renew --cert-name xxxx1.com

certbot renew --cert-name xxxx2.com

也可以更新所有:certbot renew --dry-run,新版不需要停止nginx

以上がCentOs7 nginx で HTTPS を構成するの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!