1. 安装依赖包
//一键安装上面四个依赖
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
2. 下载并解压安装包
可以去https://nginx.org/download里面找最新的包,nginx-1.25.1.tar.gz及以后的,里面资源比较多,耐心寻找。nginx-开头 .tar.gz结尾的。
//创建一个文件夹
cd /usr/local
mkdir nginx
cd nginx
//下载tar包
wget https://nginx.org/download/nginx-1.25.1.tar.gz
tar -xvf nginx-1.25.1.tar.gz
ls 查看下载和解压的包
3. 安装nginx
//进入nginx目录
cd /usr/local/nginx
//进入目录
cd nginx-1.25.1
//执行命令 考虑到后续安装ssl证书 添加两个模块
./configure --with-http_stub_status_module --with-http_ssl_module
//执行make命令
make
//执行make install命令
make install
4. 执行
nginx -h
正常继续第5步,错误时配置环境变量
4.1 配置nginx环境变量
vim /etc/profile
4.2 有如下提示时选择e进入文件
4.3 点击i进入编辑模式,在最下方添加如下代码
export NGINX_HOME=/usr/local/nginx
export PATH=$PATH:$NGINX_HOME/sbin
4.4 点击esc
4.5 点击 shift + :
4.6 输入 wq 后点击enter确认退出
4.7 重新加载配置文件
source /etc/profile
5. 启动nginx服务
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
6. 配置nginx.conf
# 打开配置文件
vi /usr/local/nginx/conf/nginx.conf
将端口号改成8089(随便挑个端口),因为可能apeache占用80端口,apeache端口尽量不要修改,我们选择修改nginx端口。
将localhost修改为你服务器的公网ip地址。
7. 重启nginx
/usr/local/nginx/sbin/nginx -s reload
8. 查看nginx进程是否启动
ps -ef | grep nginx
9. 若想使用外部主机访问nginx,需要关闭服务器防火墙或开放nginx服务端口,端口为上一步nginx.conf的配置端口
centOS6及以前版本使用命令:
systemctl stop iptables.service
centOS7关闭防火墙命令:
systemctl stop firewalld.service
关闭防火墙会导致服务器有一定风险,所以建议是单独开放服务端口 :
开放80端口:
firewall-cmd --zone=public --add-port=80/tcp --permanent
查询端口号80 是否开启:
firewall-cmd --query-port=80/tcp
重启防火墙:
firewall-cmd --reload
随后访问该ip:端口 即可看到nginx界面。
10.访问服务器ip查看(备注,由于我监听的仍是80端口,所以ip后面的端口号被省略)
11. 安装完成一般常用命令
进入安装目录中,
命令:
cd /usr/local/nginx/sbin
启动
./nginx
关闭
./nginx -s stop
重启
./nginx -s reload
12. 进入nginx配置文件目录
/usr/local/nginx/conf
13. 把nginx.config拷贝出来,配置相关信息,配置完成后替换掉原来的。这是我的配置
# For more information on configuration, see:
#
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/#user nginx;
worker_processes 4;
error_log /usr/local/nginx/logs/error.log;
pid /usr/local/nginx/logs/nginx.pid;# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;events {
worker_connections 1024;
}http {
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 /usr/local/nginx/logs/access.log main;
server_tokens off;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;include /usr/local/nginx/conf/mime.types;
default_type application/octet-stream;
# include /etc/nginx/conf.d/*.conf;server {
listen 8089;
server_name visa;
charset utf-8;location / {
root /home/visa/dist/;
try_files $uri $uri/ /index.html last;
index index.html;
client_max_body_size 100m;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}location /visa-api {
proxy_pass http://10.110.141.25:10087/web;
client_max_body_size 100m;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection upgrade;
proxy_set_header Accept-Encoding gzip;
}
}
server{
listen 8088;
server_name 172.16.15.18; #IP
index index.php index.html index.htm;
root /home/apps/ads-vue/dist; #打包dist传至服务器里的路径
charset utf-8;
# gzip_static on;
include mime.types;location / {
root /home/apps/ads-vue/dist/; #打包dist传至服务器里的路径
try_files $uri $uri/ /index.html last;
index index.html;
client_max_body_size 100m;
}location /auth {
proxy_pass http://10.110.141.25:21000;
client_max_body_size 100m;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection upgrade;
proxy_set_header Accept-Encoding gzip;
}location /framework {
proxy_pass http://10.110.141.25:22000;
client_max_body_size 100m;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection upgrade;
proxy_set_header Accept-Encoding gzip;
}location /ads {
proxy_pass http://10.110.141.25:23000;
client_max_body_size 100m;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection upgrade;
proxy_set_header Accept-Encoding gzip;
}location /producer {
proxy_pass http://10.110.141.25:20090;
client_max_body_size 100m;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection upgrade;
proxy_set_header Accept-Encoding gzip;
}
}
}
14. 把打包好的dist目录拷贝到相应的文件目录就可以了
如果有问题可以在第一次配置时重启nginx,以后就直接替换dist目录文件即可
/usr/local/nginx/sbin/nginx -s reload