CentOS7 配置Nginx域名HTTPS

Configuring Nginx with HTTPS on CentOS 7 involves similar steps to the ones for Ubuntu, but with some variations in package management and service control. Here's a step-by-step guide for CentOS 7:

Prerequisites

  1. Domain Name : "www.xxx.com"
  2. Nginx Installed: Ensure Nginx is installed.
  3. Domain DNS: Domain should point to your server's IP address.
  4. Root Privileges : You should have root or sudo privileges.

Step-by-Step Guide

1. Install Nginx

If Nginx is not already installed, you can install it using the following commands:

sh 复制代码
sudo yum install epel-release
sudo yum install nginx

Start and enable Nginx to start on boot:

sh 复制代码
sudo systemctl start nginx
sudo systemctl enable nginx
2. Configure Firewall

Allow HTTPS traffic through your firewall:

sh 复制代码
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
3. Obtain SSL Certificate

Install Certbot and the Nginx plugin:

sh 复制代码
sudo yum install certbot python2-certbot-nginx
4. Request SSL Certificate

Run Certbot to obtain and install the SSL certificate:

sh 复制代码
sudo certbot --nginx -d www.xxx.com

Follow the prompts to complete the process. Certbot will automatically configure Nginx to use the SSL certificate.

5. Verify Nginx Configuration

Open your Nginx configuration file to verify or manually configure the SSL settings:

sh 复制代码
sudo vim /etc/nginx/conf.d/www.xxx.com.conf

Ensure your server block looks like this:

nginx 复制代码
server {
    listen 80;
    listen [::]:80;
    server_name www.xxx.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name www.xxx.com;

    ssl_certificate /etc/letsencrypt/live/www.xxx.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/www.xxx.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    root /usr/share/nginx/html;
    index index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }
}
6. Test Nginx Configuration

Test your configuration to ensure there are no syntax errors:

sh 复制代码
sudo nginx -t

If the test is successful, reload Nginx:

sh 复制代码
sudo systemctl reload nginx
7. Set Up Automatic Certificate Renewal

Let's Encrypt certificates are valid for 90 days. Certbot can handle renewal automatically. To set up a cron job for automatic renewal, open the crontab editor:

sh 复制代码
sudo crontab -e

Add the following line to the crontab file:

sh 复制代码
0 0,12 * * * /usr/bin/certbot renew --quiet

This runs the renewal command twice daily.

Access Your Site

Now, you should be able to access your site securely at https://www.xxx.com.

Troubleshooting

If you encounter any issues, check the Nginx and Certbot logs for more information:

sh 复制代码
sudo tail -f /var/log/nginx/error.log
sudo tail -f /var/log/letsencrypt/letsencrypt.log

This setup ensures that your website is served over HTTPS, providing security and trust to your visitors.

相关推荐
2501_915918419 分钟前
常见 iOS 抓包工具的使用,从代理抓包、设备抓包到数据流抓包
android·ios·小程序·https·uni-app·iphone·webview
代码游侠19 分钟前
学习笔记——Linux内核与嵌入式开发1
linux·运维·前端·arm开发·单片机·嵌入式硬件·学习
腾讯蓝鲸智云34 分钟前
【运维自动化-节点管理】节点管理跟配置平台的联动关系
运维·服务器·经验分享·自动化·sass·paas
Fᴏʀ ʏ꯭ᴏ꯭ᴜ꯭.34 分钟前
Nginx构建PC站点:root与alias详解
运维·chrome·nginx
星夜落月1 小时前
Web-Check部署全攻略:打造个人网站监控与分析中心
运维·前端·网络
Turboex邮件分享1 小时前
邮件投递全流程故障排查手册
运维·人工智能
hwj运维之路2 小时前
超详细ubuntu22.04部署k8s1.28高可用(二)【结合ingress实现业务高可用】
运维·云原生·容器·kubernetes
玉梅小洋2 小时前
Linux命令详解 —— IP 命令及永久配置
linux·运维·tcp/ip
yqcoder2 小时前
uni-app 之 下拉刷新
运维·服务器·uni-app
江畔何人初2 小时前
k8s中Role与networkpolicy区别
linux·运维·云原生