基础环境:CentOS8.0、Nginx1.19.9.
一.安装nginx前的准备工作
在linux下安装nginx,首先需要安装 gcc-c++编译器。然后安装nginx依赖的pcre和zlib包。最后安装nginx即可。(有很多同学直接下载nginx开始安装,出现各种错误!)
二.安装gcc-c++编译器
[root@hecs-401441 ~]# yum install gcc-c++
[root@hecs-401441 ~]# yum install -y openssl openssl-devel
三.安装pcre包
[root@hecs-401441 ~]# yum install -y zlib zlib-devel
[root@hecs-401441 nginx-1.19.9]# yum -y install pcre-devel
安装完以后下面进行nginx的安装!
四.nginx的下载
下载有2种方式:
1. 在liunx用命令下载(这里我下载的是1.19.9版本的)liunx要能访问外网;
2.从官网下载安装包通过ftp上传;
[root@hecs-401441 ~]# wget https://nginx.org/download/nginx-1.19.9.tar.gz
--2022-11-02 10:42:50-- https://nginx.org/download/nginx-1.19.9.tar.gz
Resolving nginx.org (nginx.org)... 52.58.199.22, 3.125.197.172, 2a05:d014:edb:5702::6, ...
Connecting to nginx.org (nginx.org)|52.58.199.22|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1060580 (1.0M) [application/octet-stream]
Saving to: ‘nginx-1.19.9.tar.gz’
nginx-1.19.9.tar.gz 100%[=================================================================================================>] 1.01M 1.38MB/s in 0.7s
2022-11-02 10:42:52 (1.38 MB/s) - ‘nginx-1.19.9.tar.gz’ saved [1060580/1060580]
[root@hecs-401441 ~]# ls
mysql-5.7.40-linux-glibc2.12-x86_64.tar.gz nginx-1.19.9.tar.gz xxl-job-admin-2.4.0-SNAPSHOT.jar
[root@hecs-401441 ~]#
下载或者你上传的位置在哪里都可以,后面根据自己的需要把安装包解压到合适的文件夹里面 :
[root@hecs-401441 ~]# ls
mysql-5.7.40-linux-glibc2.12-x86_64.tar.gz nginx-1.19.9.tar.gz xxl-job-admin-2.4.0-SNAPSHOT.jar
[root@hecs-401441 ~]# cp nginx-1.19.9.tar.gz /usr/local
[root@hecs-401441 ~]# ls
mysql-5.7.40-linux-glibc2.12-x86_64.tar.gz nginx-1.19.9.tar.gz xxl-job-admin-2.4.0-SNAPSHOT.jar
[root@hecs-401441 ~]# cd /usr/local
[root@hecs-401441 local]# ls
bin etc games hostguard include lib lib64 libexec mysql nginx-1.19.9.tar.gz sbin share src uniagent
[root@hecs-401441 local]#
五.安装nginx
5.1解压nginx安装包
[root@hecs-401441 local]# tar -zxvf nginx-1.19.9.tar.gz
5.1.1 cd到你刚解压的目录里面
[root@hecs-401441 local]# cd nginx-1.19.9
5.1.2 进入到nginx-1.19.9目录,输入下面命令,使用nginx默认的配置
[root@hecs-401441 nginx-1.19.9]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@hecs-401441 nginx-1.19.9]# ./configure
5.1.3编译安装(按照顺序执行下面2个命令)
[root@hecs-401441 nginx-1.19.9]# make
[root@hecs-401441 nginx-1.19.9]# make install
5.1.4编译安装完成后 输入下面命令查看自己的nginx安装在哪里。
[root@hecs-401441 nginx-1.19.9]# whereis nginx
nginx: /usr/local/nginx
[root@hecs-401441 nginx-1.19.9]#
5.1.5根据上面获得的目录cd进入sbin目录里面。输入下面命令启动nginx
[root@hecs-401441 nginx-1.19.9]# cd ..
[root@hecs-401441 local]# ls
bin etc games hostguard include lib lib64 libexec mysql nginx nginx-1.19.9 nginx-1.19.9.tar.gz sbin share src uniagent
[root@hecs-401441 local]# cd nginx
[root@hecs-401441 nginx]# ls
conf html logs sbin
[root@hecs-401441 nginx]# cd sbin/
[root@hecs-401441 sbin]# ls
nginx
[root@hecs-401441 sbin]# ./nginx
[root@hecs-401441 sbin]#
5.1.6 查看是否启动成功
[root@hecs-401441 sbin]# ps -ef | grep nginx
root 71766 1 0 11:00 ? 00:00:00 nginx: master process ./nginx
nobody 71767 71766 0 11:00 ? 00:00:00 nginx: worker process
root 71796 67846 0 11:00 pts/0 00:00:00 grep --color=auto nginx
[root@hecs-401441 sbin]#
六.安装完毕
在浏览器输入自己的IP就可以访问nginx了。
配置nginx.conf:
server {
listen 20000;
client_max_body_size 50m;
# server_name aa;
# rewrite ^(.*)$ https://${server_name}$1 permanent;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /alidata1/services/www/wwwroot/manage/vue;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
location /api/ {
proxy_pass http://127.0.0.1:19999/api/;
}
location /group1/ {
proxy_pass http://127.0.0.1:19999/group1/;
}
}
注意:
如果访问不了!就是你的服务器防火墙没有开放80端口!(默认监听80端口)
防火墙开放端口
#查看开放了哪些服务端口
firewall-cmd --list-all
#开放端口号命令
firewall-cmd --add-port=80/tcp --permanent
#重启防火墙,使配置生效
systemctl restart firewalld.service