linux安装nginx并且配置ssl

准备工作

安装配置nginx

其它

准备工作

首先下载相关文件,我这里下载的是nginx-1.9.9.tar.gz,然后安装相关依赖工具

yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

然后编写nginx的ssl配置文件nginx-***-ssl.conf

server {
    listen 80; # redirect to 443
    server_name ***.com www.***.com;#此处配置你的域名即可
    rewrite ^(.*)$  https://$host$1 permanent;
}

server {
    listen 443 ssl; # redirect to https
    server_name ***.com www.***.com;#此处配置你的域名即可
    
    ssl_certificate   /usr/cert/***.pem;#本人用的阿里云,下载对应的nginx证书文件即可
    ssl_certificate_key  /usr/cert/***.key;#本人用的阿里云,下载对应的nginx证书文件即可
    
    location / {
        root   /***;# 这里是你的网站所在目录,我这边配置的就是一个静态官网的ssl,因此直接放在nginx的html目录了
        #index  index.html index.htm;
        try_files $uri $uri/ /index.html;
    }
}

安装配置nginx

nginx安装就不必多说了,简单来说以下几步: 1.上传刚刚下载的nginx压缩文件(我这边上传到了/usr目录中),我这边使用的是rz命令

yum -y install lrzsz

2.解压

cd /usr
rz
tar -zxvf nginx-1.9.9.tar.gz

然后修改名称,便于后续操作

cd /usr
mv nginx-1.9.9 nginx

3.进入nginx目录,依次输入以下命令(特别注意的地方:prefix后面的值是你安装的nginx所在目录,如果需要ssl,那么还需要在后面指定安装with-http_ssl_module,否则后续配置好了也启动不了)

cd /usr/nginx
./configure --prefix=/usr/nginx --with-http_ssl_module
make
make install

4.在启动之前,需要创建一个logs文件(在nginx的目录下),并授权,否则启动时会报错

cd /usr/nginx
mkdir logs
chmod 700 logs

5.导入刚刚配置好的ssl文件(我这边是在/usr/nginx/conf中创建了一个保存所有ssl文件的目录confs,然后如果后续还有其它的ssl配置,也可以放置在这里面,而且也方便寻找),并修改nginx默认配置文件(/usr/nginx/conf/nginx.conf),使其能够导入其它配置文件(加入这一段内容:include /usr/nginx/conf/confs/*.conf;)

cd /usr/nginx/conf
mkdir confs
cd confs
rz
cd ..
vi nginx.conf
#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;

    include /usr/nginx/conf/confs/*.conf;

    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;
    #    }
    #}

}

6.检查配置文件的正确性,并启动nginx服务

cd /usr/nginx
./sbin/nginx -t

如果看到以下内容,即为配置文件无误,可以启动了

nginx: the configuration file /usr/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/nginx/conf/nginx.conf test is successful

启动nginx

cd /usr/nginx
./sbin/nginx -c /usr/nginx/conf/nginx.conf

其它

当然在启动之后也有可能会出现打不开的情况,主要分为以下几种: 1、服务器未打开80端口的外部访问权限 2、证书错误 3、域名配置错误 4、先前配置的过程中曾经遇到一个很奇怪的问题:服务器和证书颁发机构不是同一个,也不会成功,这一点也需要注意!

请以此文献给自己,让自己以后有需要时再回来看这篇文章!