CentOS 7上安装OpenSSL和Nginx

简介

OpenSSL是一个开源工具包,它提供了一个强大的通用加密库,实现了SSL和TLS协议。Nginx是一个高性能的HTTP和反向代理服务器,也是一个通用的TCP/UDP代理服务器和电子邮件代理服务器。在本教程中,我们将指导您如何在CentOS 7上安装OpenSSL和Nginx。

前提条件

  • 确保您拥有CentOS 7的root权限。

  • 确保您的系统是最新的。您可以使用以下命令更新系统:

    sudo yum update
    

安装OpenSSL

  1. 使用YUM安装OpenSSL:

    sudo yum install openssl
    
  2. 安装完成后,您可以使用以下命令检查OpenSSL版本:

    openssl version
    

安装Nginx

  1. 添加EPEL仓库(如果尚未添加):

    sudo yum install epel-release
    
  2. 安装Nginx:

    sudo yum install nginx
    
  3. 启动Nginx服务:

    sudo systemctl start nginx
    
  4. 检查Nginx服务状态:

    sudo systemctl status nginx
    
  5. 为了确保Nginx在系统启动时自动启动,执行以下命令:

    sudo systemctl enable nginx
    

配置SSL/TLS证书

  1. 使用OpenSSL生成私钥和证书签名请求(CSR):

    sudo openssl req -new -newkey rsa:2048 -nodes -keyout /etc/pki/tls/private/nginx-selfsigned.key -out /etc/pki/tls/certs/nginx-selfsigned.csr
    
  2. 使用私钥和CSR生成自签名证书:

    sudo openssl x509 -req -days 365 -in /etc/pki/tls/certs/nginx-selfsigned.csr -signkey /etc/pki/tls/private/nginx-selfsigned.key -out /etc/pki/tls/certs/nginx-selfsigned.crt
    
  3. 编辑Nginx配置文件以使用SSL/TLS证书:

    sudo nano /etc/nginx/nginx.conf
    

    找到http块,并添加以下内容:

    server {
        listen 443 ssl;
        server_name your_domain_or_IP;
    
        ssl_certificate /etc/pki/tls/certs/nginx-selfsigned.crt;
        ssl_certificate_key /etc/pki/tls/private/nginx-selfsigned.key;
    
        location / {
            root /usr/share/nginx/html;
            index index.html index.htm;
        }
    }
    

    替换your_domain_or_IP为您的域名或IP地址。

  4. 重启Nginx以应用配置更改:

    sudo systemctl restart nginx
    

结论

现在,您已经在CentOS 7上成功安装了OpenSSL和Nginx,并配置了SSL/TLS证书。您可以通过访问https://your_domain_or_IP来测试您的Nginx服务器。

猜你喜欢

转载自blog.csdn.net/qq_40797754/article/details/143265695