Linux安装Nginx
Linux系统安装Nginx
目录
安装包下载
1.安装Nginx
1.1 安装依赖:压缩包一般放在/opt/software目录下,没有则创建
[root@localhost software]# yum -y install gcc zlib zlib-devel pcre pcre-devel openssl openssl-devel make
1.2 解压安装包并切换到文件夹中:
[root@localhost software]# tar -zxvf /opt/software/nginx-1.22.0.tar.gz
[root@localhost spftware]# cd nginx-1.22.0
1.3 开始执行编译、安装(一并安装SSL模块):
[root@localhost nginx-1.22.0]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
[root@localhost nginx-1.22.0]# make && make install
1.4 查看是否安装成功
[root@localhost nginx-1.22.0]# /usr/local/nginx/sbin
[root@localhost nginx-1.22.0]# ./nginx -V
显示如下:
nginx version: nginx/1.22.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
安装结束。
2.配置环境变量
2.1 查看 nginx 版本,此时会发现报 bash:nginx: 未找到命令,需要我们手动配置打开环境变量的文件
[root@localhost nginx-1.22.0]# nginx -v
显示如下:
bash: nginx: command not found...
2.2 编辑环境变量文件:
[root@localhost nginx-1.22.0]# vim /etc/profile
编辑文件:行末加上自己的nginx安装目录下sbin目录的地址:
.... #前面省略
export PATH=$PATH:/usr/local/nginx/sbin
2.3 重新加载环境:
[root@localhost nginx-1.22.0]# source /etc/profile
2.4 再次查看 nginx 版本
[root@localhost nginx-1.22.0]# nginx -v
显示如下:
nginx version: nginx/nginx-1.22.0
3.开机启动配置
3.1 先创建开机自启脚本:
[root@localhost nginx-1.22.0]# cd /etc/systemd/system
[root@localhost system]# vim nginx.service
3.2 内容复制到 vim 中:
[Unit]
Description=nginx service
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
3.3 服务命令:
第一种方式启动
#启动nginx服务
cd /usr/local/nginx/sbin/
./nginx
#查看运行状态
ps aux | grep nginx
#停止nginx服务
./nginx –s stop
#重启nginx服务
./nginx –s reload
#检查配置文件是否正确
./nginx –t
#查看nginx版本
./nginx –v第二种基于配置开机启动模式启动
#设置开机自启动
systemctl enable nginx
#启动nginx服务
systemctl start nginx.service
#重新启动服务
systemctl restart nginx.service
#查看服务当前状态
systemctl status nginx.service
#停止开机自启动
systemctl disable nginx.service
3.4 访问服务器IP
显示welcom to nginx 说明启动安装成功了,接下来可以通过编辑nginx.conf文件来进行项目的部署了。建议备份一下配置文件。
4.配置HTTPS
证书可以自行到阿里云或者腾讯云申请免费的证书,并下载对应Nginx的证书
服务器 /usr/local/nginx/conf 下创建 cert 目录,并将证书放入该文件夹中
4.1 编辑Nginx配置文件:
[root@localhost system]# cd /usr/local/nginx/conf
[root@localhost conf]# vim nginx.conf
配置如下配置:
# HTTPS server
server {
listen 80;
server_name pmis.shxrtech.com;
rewrite ^(.*)$ https://$host$1 permanent;
}
server {
listen 443 ssl;
server_name cc.shxrtech.com;
ssl_certificate cert/pmis.shxrtech.com.pem;
ssl_certificate_key cert/pmis.shxrtech.com.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root html/pms;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location /api {
proxy_pass http://localhost:8093/api;
proxy_set_header Host $host;
proxy_pass_header User-Agent;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-Port $remote_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_request_buffering off;
client_max_body_size 1024m;
server_tokens off;
}
}
更多推荐
所有评论(0)