Linux安装nginx并配置ssl自签证书

一、下载nginx压缩包:官网下载地址http://nginx.org/download/,点击进入选择合适自己系统的版本,本机已centos安装nginx-1.9.1.tar.gz压缩包为例。

二、将下载的nginx-1.9.1.tar.gz文件通过ftp等工具上传到centos系统的 /usr/local目录下,cd到该目录运行 ” tar -zxvf nginx-1.9.9.tar.gz “命令解压到该目录,解压成功后发现该目录下多了一个文件夹nginx-1.9.9。

三、cd到nginx-1.9.9目录,运行命令” ./configure –with-http_ssl_module –with-http_stub_status_module ” 进行编译,–后面的参数表示编译时增加ssl模块功能。

四、继续在目录下运行命令 ” make & make install “,等待安装完成,如果是修改nginx,就不需要运行make install,否则会覆盖安装。

五、切换到安装目录 ” cd /usr/local/nginx/conf ” ,运行证书自签生成命令 ” openssl req -new -x509 -nodes -out cert.pem -keyout cert.key -days 999 “,按照提示输入证书相关信息,直到运行完成后会在该目录下生成两个证书文件 cert.pem和cert.key,有效期为999天。运行命令后会弹出一些列输入框和提示,其界面如下:Country Name表示国家名称;State or Province Name表示州或省名称;Organization Name表示组织名称;Common Name表示常用名称,例如您的姓名或服务器的主机名;Email Address表示电子邮件地址。

Linux安装nginx并配置ssl自签证书

根据以上信息,生成的证书信息如下:

Linux安装nginx并配置ssl自签证书

六、打开ngix.conf配置文件,发现如下关于https的443端口配置代码被注释,复制一份相关server节点代码,手动将其配置好,代码如下:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

    server {
        listen       443 ssl;
        server_name  localhost;

        ssl_certificate      cert.pem;  //对应前一步生成的证书文件pem
        ssl_certificate_key  cert.key;  //对应前一步生成的证书文件key

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location / {
            #root   html;
            #index  index.html index.htm;
            proxy_pass http://10.2.1.172:8081;  //此处对443端口做了代理,转发到10.2.1.172:8081端口
        }
    }

}

七、运行nginx启动命令” /usr/local/nginx/sbin/nginx “,若无报错,则访问https://localhost,即可通过nginx转发到http://10.2.1.172:8081上。

八、设置nginx开机自启动:

1、创建/etc/init.d/nginx文件,复制如下内容(这个内容是nginx官方说明文档中提供的)到文件中,修改nginx=”/usr/local/nginx/sbin/nginx” 指向你的nginx启动文件路径,NGINX_CONF_FILE=”/usr/local/nginx/conf/nginx.conf” 指向你的配置文件路径。

#!/bin/sh
#
nginx - this script starts and stops the nginx daemon
#
chkconfig:   - 85 15
description:  NGINX is an HTTP(S) server, HTTP(S) reverse \
              proxy and IMAP/POP3 proxy server
processname: nginx
config:      /etc/nginx/nginx.conf
config:      /etc/sysconfig/nginx
pidfile:     /var/run/nginx.pid
Source function library.

. /etc/rc.d/init.d/functions
Source networking configuration.

. /etc/sysconfig/network
Check that networking is up.

[ "$NETWORKING" = "no" ] && exit 0#nginx启动文件路径
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)#nginx.conf配置文件路径
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
   # make required directories
   user=$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -
   if [ -z "grep $user /etc/passwd" ]; then
       useradd -M -s /bin/nologin $user
   fi
   options=$nginx -V 2>&1 | grep 'configure arguments:'
   for opt in $options; do
       if [ echo $opt | grep '.*-temp-path' ]; then
           value=echo $opt | cut -d "=" -f 2
           if [ ! -d "$value" ]; then
               # echo "creating" $value
               mkdir -p $value && chown -R $user $value
           fi
       fi
   done
}
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?

    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}
stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?

    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}
restart() {
    configtest || return $?

    stop
    sleep 1
    start
}
reload() {
    configtest || return $?

    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?

    echo
}
force_reload() {
    restart
}
configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
    status $prog
}
rh_status_q() {
    rh_status >/dev/null 2>&1
}
case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

2、保存成功后给nginx文件赋予操作权限:chmod a+x /etc/init.d/nginx 。

3、使用chkconfig命令:chkconfig –add /etc/init.d/nginx 将其加入管理列表。

4、使用如下命令进行操作:

service nginx start //启动nginx

service nginx stop //停止nginx

chkconfig nginx on //设置开机启动

Original: https://www.cnblogs.com/zqhIndex/p/16184672.html
Author: 我若安好,便是晴天
Title: Linux安装nginx并配置ssl自签证书

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/611748/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • dns服务之bind配置内网解析部分子域名,其它子域名转发

    bind配置内网解析部分子域名,其它子域名转发。以下以m.xxx.com和admin.xxx.com由内网dns解析,其它*.xxx.com转发给外网dns解析为例配置。文件/et…

    数据库 2023年6月14日
    093
  • Mysql客户端的安装

    Mysql数据库(简称)属于C/S架构,正常工作中一般都会提供服务端,我们只需要安装客户端进行查询修改数据等操作即可。 在正常工作中,无论是测试人员还是开发人员,总数据库管理员(测…

    数据库 2023年5月24日
    074
  • javaWeb知识点大集合!!!

    pom文件: 4.0.0 org.example javaweb_maven 1.0-SNAPSHOT war UTF-8 1.7 1.7 com.github.pagehelpe…

    数据库 2023年6月16日
    075
  • [Mysql]Ubuntu如何安装Mysql+启用远程连接[完整版]

    唉。下面是我花了不知道多少个小时踩过的所有坑总结出来的血泪史,希望能帮你们少踩一些坑吧,正常来讲一步一步下来就不会出现任何问题了。 背景 用的是百度云的云服务器(其他云服务器同理)…

    数据库 2023年6月16日
    072
  • iptables快速入门

    iptables简介 在生产实践过程中,为了保证生产安全,通常需要进行数据加密,例如通过网络层面上将应用隔离成内网和公网应用,内网再分成生产、测试、开发、DMZ等区域,然后进行不同…

    数据库 2023年6月14日
    093
  • 软件测试流程是什么?这题我不会啊

    转载请注明出处❤️ 作者:测试蔡坨坨 原文链接:caituotuo.top/ae4fff18.html 你好,我是测试蔡坨坨。 最近收到不少准备转行软件测试的小伙伴私信问真实企业里…

    数据库 2023年6月11日
    089
  • 为Typora配置Gitee图床

    安装Typora 官网下载直接安装:https://www.typora.io/#download 编辑Typora图像设置 说明: 打开:文件–>偏好设置&#8…

    数据库 2023年6月11日
    0148
  • MySQL8.0其他新特性

    MySQL8.0其他新特性 MySQL8.0新特性概述 MySQL8.0新增特性 MySQL8.0移除的旧特性 新特性1:窗口函数 窗口函数的分类 MySQL8.0版本开始支持窗口…

    数据库 2023年5月24日
    069
  • MySQL数据库的创建、删除和备份

    总结MySQL数据库的创建、删除和备份操作 MySQL数据库的创建、删除和备份 数据库的创建 //使用指令创建数据库 CREATE DATABASE yjh_db01; //创建一…

    数据库 2023年6月16日
    075
  • 绘制几何图形

    《零基础学Java》 绘制几何图形Java可以 分别使用 Graphics 和 Graphics2D 绘制图形, Graphics类 使用不同的方法绘制不同的图形(drawLine…

    数据库 2023年6月16日
    085
  • linux-centos常用命令

    01-centos-常用命令 1.centos防火墙 关闭 systemctl stop firewalld 禁止开机启动防火墙 systemctl disable firewal…

    数据库 2023年6月11日
    085
  • Activiti7 多实例子流程

    顾名思义,子流程是一个包含其他活动、网关、事件等的活动,这些活动本身形成了一个流程,该流程是更大流程的一部分。 使用子流程确实有一些限制: 一个子流程只能有一个none类型的启动事…

    数据库 2023年6月14日
    0150
  • 数据库的备份和恢复命令,使用视图,索引,事务

    备份库 直接在cmd窗口中直接输入,结束不需要输入; mysqldump -h端口号 -u用户名 -p密码 数据库名>备份地址 恢复库 在cmd窗口中进行 1、连接数据库 m…

    数据库 2023年6月16日
    0111
  • 中文技术文档写作规范

    使用 markdown 格式书写文档 只使用一二三级标题,三级标题下面的并列性内容使用列表展示 二级标题前使用行分隔符表示分隔 段落之间使用一个空行隔开 一句话或者以逗号分隔的句子…

    数据库 2023年6月6日
    074
  • MySQL启动过程详解一:启动整体流程

    MySQL启动流程如下: 设置进程名 处理配置文件及启动参数以及部分模块初始化,这包括: 2.1 从配置文件中读取选项,把他们放在 argc 和 argv 已有的参数之前 2.2 …

    数据库 2023年6月9日
    049
  • 设计模式六大原则(简单总结)

    一、单一职责原则 通俗来说即一个类只负责一项职责(实现类要职责单一),可以降低类的复杂度,提高系统的可读性、可维护性,降低类修改时对其他功能的影响 二、里氏替换原则 即子类继承父类…

    数据库 2023年6月6日
    054
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球