欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Liunx如何安装nginx

程序员文章站 2024-01-20 13:44:40
...

Liunx如何安装nginx和配置https

第一种

linux系统
CentOS 7 64位

下载以下安装包,用xftp放入linux系统
Liunx如何安装nginx
第一步:安装pcre依赖

  • 解压压缩文件,进入解压之后的目录执行./configure,然后执行make && make install

Liunx如何安装nginx

  • 查看是否安装成功
[aaa@qq.com pcre-8.37]# pcre-config --version
1

Liunx如何安装nginx
第二步:安装其他依赖

[aaa@qq.com pcre-8.37]# yum -y make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
1

Liunx如何安装nginx
第三步:安装nginx

  • 解压nginx,进入nginx目录,执行./configure
    Liunx如何安装nginx
  • 执行make && make install
    Liunx如何安装nginx
    去sbin文件夹下启动nginx
cd /usr/local/nginx/sbin
1

Liunx如何安装nginx

检查是否启动成功

[aaa@qq.com sbin]# ps -ef|grep nginx
1

Liunx如何安装nginx

第二种

linux系统
Red Hat Enterprise Linux Server release 6.5 (Santiago)

提前需要准备的

1.nginx 源码 : http://nginx.org/en/download.html
2.yum
安装教程:https://blog.csdn.net/yujing1314/article/details/97237644
3.gcc-c++

[aaa@qq.com ~]yum install gcc-c++

4.第三方开发包

[aaa@qq.com ~]yum install -y pcre pcre-devel

[aaa@qq.com ~]yum install -y zlib zlib-devel

[aaa@qq.com ~]yum install -y openssl openssl-devel

安装步骤

第一步:把nginx的源码包上传到linux系统

我使用的SecureCRT的sftp文件传输,直接把文件拖进去就OK了
Liunx如何安装nginx

第二步:解压缩

[aaa@qq.com ~]tar zxf nginx-1.8.0.tar.gz

第三步:使用configure命令创建一makeFile文件。

./configure
–prefix=/usr/local/nginx
–pid-path=/var/run/nginx/nginx.pid
–lock-path=/var/lock/nginx.lock
–error-log-path=/var/log/nginx/error.log
–http-log-path=/var/log/nginx/access.log
–with-http_gzip_static_module
–http-client-body-temp-path=/var/temp/nginx/client
–http-proxy-temp-path=/var/temp/nginx/proxy
–http-fastcgi-temp-path=/var/temp/nginx/fastcgi
–http-uwsgi-temp-path=/var/temp/nginx/uwsgi
–http-scgi-temp-path=/var/temp/nginx/scgi

第四步 上一步可能会报错,因为缺少temp文件,如下创建即可

[aaa@qq.com sbin]# mkdir /var/temp/nginx/client -p

第五步 make

直接输入 make

第六步 make install

直接输入 make install

开启nginx

[aaa@qq.com sbin]# ./nginx

如何查看进程[aaa@qq.com stefan]# ps aux|grep nginx
Liunx如何安装nginx

关闭nginx:
[aaa@qq.com sbin]# ./nginx -s stop

推荐使用:
[aaa@qq.com sbin]# ./nginx -s quit

重启nginx:
nginx -s reload

测试

输入你虚拟机的ip,如下图就成功了
Liunx如何安装nginx
如果测试失败,注意查看虚拟机防火墙是否关闭

Nginx配置https

1、查看nginx是否支持ssl

​ 环境:centos7.6

[aaa@qq.com ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module   #有ssl表示支持,没有需要重新编译安装
2、带ssl模块方式安装nginx
wget http://nginx.org/download/nginx-1.9.9.tar.gz
tar -zxvf  nginx-1.9.9.tar.gz
cd nginx-1.9.9
./configure --prefix=/usr/local/nginx --with-http_ssl_module  
make 
make install
3、修改配置文件
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
#app后端服务
upstream app{
   server 192.168.10.10;
}
#app2后端服务
upstream app2{
   server 192.168.10.11;
}
    # HTTPS server
    #
    server {
        listen       443 ssl;
        server_name  www.yuming.com;    #ssl域名

        ssl_certificate      /usr/local/nginx/ssl_key/4196440_ezc.chinapopin.com.pem;  #ssl的pem证书路径
        ssl_certificate_key  /usr/local/nginx/ssl_key/4196440_ezc.chinapopin.com.key;  #ssl的key证书路径

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

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

        location / {
            proxy_pass http://app;          #对应app服务
            
        }
        location /app2 {
            proxy_pass http://app2;         #对应app2服务
        }

    }

}
4、配置服务并启动
[aaa@qq.com ]# cat << EOF > /lib/systemd/system/nginx.service #创建Nginx服务系统启动文件
[Unit]
Description=nginx
After=network.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 quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload && systemctl start nginx && \
systemctl enable nginx && systemctl status nginx
5、验证
ie浏览器
https://www.yuming.com          --返回192.168.1.10 的网站
https://www.yuming.com/app2  --返回192.168.1.11 的网站

配置模板参考


user  root;
worker_processes  3;

#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  2800;
	client_max_body_size 1024M;
	client_body_buffer_size 10M;
    gzip  on;
	gzip_min_length 1k;
	gzip_comp_level 1;
	gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png application/vnd.ms-fontobject font/ttf font/opentype font/x-woff image/svg+xml;
	gzip_vary on;
	gzip_disable "MSIE [1-6]\.";
	gzip_buffers 32 4k;
	gzip_http_version 1.0;

    server {
        listen       8012;
        server_name  localhost;
		#后台页面
		root /home/server/yuzhou-admin;
		index index.html index.htm; 

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
		
		#pc服务
		location /sales-api {
            proxy_pass  http://localhost:8011/sales-api;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            root html;
            index index.html index.htm;
        }

        #文件服务器
        location /file-service {
            alias /home/server/file-service;
        }
  
    }
	server {
        listen       443 ssl;
        server_name  ydjyh.yuzhou-group.com;
        #ssl    on;
        ssl_certificate      /usr/local/nginx/conf/yuzhou-group.com.cer;
        ssl_certificate_key  /usr/local/nginx/conf/yuzhou-group.com.key;
        ssl_session_timeout  5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers  ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4:!DH:!DHE;
        ssl_prefer_server_ciphers   on;
		#后台页面
		root /home/server/yuzhou-admin;
        index index.html index.htm;
      
 # location / {
       #     root   html;
       #     index  index.html index.htm;
       # }
		
		#pc服务
		location /sales-api {
            proxy_pass  http://localhost:8011/sales-api;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            root html;
            index index.html index.htm;
        }

        #文件服务器
        location /file-service {
            alias /home/server/file-service;
        }


    }


}

相关标签: nginx