一.安装依赖库

这里使用yum安装来安装依赖库

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

二.安装

wget下载nginx包,或者下载软件包上传到服务器

 wget http://nginx.org/download/nginx-1.16.1.tar.gz

解压nginx软件包

tar -zxvf nginx-1.16.1.tar.gz

cd进入到解压完成之后的ngixn包目录当中

cd nginx-1.16.1

配置编译安装

 ./configure  --with-stream --with-http_ssl_module && make && make install

–with-srteam Nginx默认支持HTTP代理,在安装时加上这个则可以配置nginx支持TCP的代理
–with-http_ssl_module 配置ngixn支持ssl服务
–prefix=/www/ngixn 指定nginx的安装目录,如果不指定,默认安装到/usr/local/nginx

启动nginx

/usr/local/nginx/sbin/nginx

四.测试

在网页中访问此系统的IP地址,如果可以看到welcome to nginx的字样,则说明安装nginx没问题

五.配置开机启动

配置nginx开启自启有两种方式

5.1,配置systemctl来启动nginx

再/usr/lib/systemd/system/下添加nginx文件

vim /usr/lib/systemd/system/nginx
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop

[Install]
WantedBy=multi-user.target

赋予执行权限

chmod 775 /usr/lib/systemd/system/nginx

然后就可以使用以下命令来操作

systemctl  stop nginx  		#关闭
systemctl  start nginx  	#启动
systemctl  status nginx		#查看状态
systemctl  enable nginx		#开启开机自启
systemctl  disable nginx	#关闭开机自启

5.2,直接将nginx的启动命令写入到rc.local文件里

vim /etc/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
/usr/local/nginx/sbin/nginx   #nginx的启动命令

赋予执行权限

chmod +x /etc/rc.local   

六.配置文件

配置nginx的配置文件,如果没有把握的话,建议先将nginx的配置文件先做一个备份后在配置

 cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak

6.1,配置nginx为web容器

nginx为web容器表示自己做web客户端的请求

修改ngixn配置

 vim /usr/local/nginx/conf/nginx.conf
server {
                listen     443 ssl;		#nginx监听端口
                server_name  web.***.cn; #nginx域名
                ssl_certificate  /usr/local/nginx/ssl/_***_public.crt;	#ssl证书
                ssl_certificate_key /usr/local/nginx/ssl/_***.key;		#ssl密钥
                ssl_protocols    TLSv1 TLSv1.1 TLSv1.2;


        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /www/wwwroot/ ***;	#nginx网页路径,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;
        }


6.2,配置nginx为代理服务器

ngixn为代理服务器则表示将web客户端的请求转发至另外一台web服务器

修改ngixn配置

 vim /usr/local/nginx/conf/nginx.conf
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen     443 ssl;		#ngixn监听端口
        server_name ***.cn; 	#nginx域名
        ssl_certificate  /usr/local/nginx/ssl/_***.crt;		#ssl证书
        ssl_certificate_key /usr/local/nginx/ssl/_***.key;	#ssl密钥
        ssl_protocols    TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
        ssl_prefer_server_ciphers on;
        proxy_connect_timeout 600;
        proxy_read_timeout 600;
        proxy_send_timeout 600;


        location / {
                proxy_pass https://www.***.cn;	#将请求转发到此处
}
}
}

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐