使用Nginx部署前端项目
参考博文:
https://blog.csdn.net/Cs_Mervyn/article/details/120075477
https://blog.csdn.net/yujing1314/article/details/105225325
https://blog.csdn.net/mollerlala/article/details/96427751?depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-2&utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-2
打包前端项目生成dist文件夹,将文件夹上传至服务器。
PS D:\Code\traffic> yarn build
yarn run v1.22.19
$ umi build
√ Webpack
Compiled successfully in 1.79m
......
Browserslist: caniuse-lite is outdated. Please run:
npx browserslist@latest --update-db
Why you should do it regularly:
https://github.com/browserslist/browserslist#browsers-data-updating
DONE Compiled successfully in 107645ms 18:02:29
File Size Gzipped
dist\umi.js 122.3 MB 31.1 MB
dist\umi.css 559.2 KB 79.2 KB
Images and other types of assets omitted.
The bundle size is significantly larger than recommended.
Consider reducing it with code splitting: https://umijs.org/docs/load-on-demand
You can also analyze the project dependencies using ANALYZE=1
Done in 116.01s.
PS D:\Code\traffic>
配置Nginx环境
pwd:输出当前环境目录
wget -c https://nginx.org/download/nginx-1.22.0.tar.gz:下载nginx安装包
并下载一些需要的包
Last login: Fri Oct 14 14:03:14 2022 from 172.16.31.102
[root@gl-node01 ~]# ls
anaconda-ks.cfg install.log install.log.syslog
[root@gl-node01 ~]# cd ..
[root@gl-node01 /]# ls
bin etc lib64 net selinux tmp.2022-08-26.zip tmp.2022-09-09.zip tmp.2022-10-09.zip
boot export lost+found opt share tmp.2022-08-28.zip tmp.2022-09-16.zip tmp.2022-10-11.zip
cgroup home media proc srv tmp.2022-08-29.zip tmp.2022-09-23.zip usr
data include misc root sys tmp.2022-09-02.zip tmp.2022-09-30.zip var
dev lib mnt sbin tmp tmp.2022-09-05.zip tmp.2022-10-07.zip
[root@gl-node01 /]# pwd
/
[root@gl-node01 /]# cd /usr/local
[root@gl-node01 local]# ls
bin etc games include lib lib64 libexec openssl sbin share src
[root@gl-node01 local]# pwd
/usr/local
[root@gl-node01 local]# wget -c https://nginx.org/download/nginx-1.22.0.tar.gz
--2022-10-14 16:53:58-- https://nginx.org/download/nginx-1.22.0.tar.gz
Resolving nginx.org... 52.58.199.22, 3.125.197.172, 2a05:d014:edb:5704::6, ...
Connecting to nginx.org|52.58.199.22|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1073322 (1.0M) [application/octet-stream]
Saving to: “nginx-1.22.0.tar.gz”
100%[================================================================>] 1,073,322 765K/s in 1.4s
2022-10-14 16:54:05 (765 KB/s) - “nginx-1.22.0.tar.gz” saved [1073322/1073322]
[root@gl-node01 local]# ls
bin etc games include lib lib64 libexec nginx-1.22.0.tar.gz openssl sbin share src
[root@gl-node01 local]# yum install -y pcre pcre-devel
Loaded plugins: fastestmirror, security
......
Complete!
[root@gl-node01 local]# yum install -y zlib zlib-devel
Loaded plugins: fastestmirror, security
Setting up Install Process
Loading mirror speeds from cached hostfile
Package zlib-1.2.3-29.el6.x86_64 already installed and latest version
Package zlib-devel-1.2.3-29.el6.x86_64 already installed and latest version
Nothing to do
[root@gl-node01 local]# yum install gcc-c++
Loaded plugins: fastestmirror, security
Setting up Install Process
Loading mirror speeds from cached hostfile
Package matching gcc-c++-4.4.7-18.el6.x86_64 already installed. Checking for update.
Nothing to do
[root@gl-node01 local]# yum install -y openssl openssl-devel
Loaded plugins: fastestmirror, security
Setting up Install Process
....
error
[root@gl-node01 local]#
解压nginx安装包
tar -zxvf nginx-1.22.0.tar.gz
[root@gl-node01 local]# tar -zxvf nginx-1.22.0.tar.gz
nginx-1.22.0/
......
......
nginx-1.22.0/auto/cc/sunc
[root@gl-node01 local]# cd nginx-1.22.0
[root@gl-node01 nginx-1.22.0]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@gl-node01 nginx-1.22.0]# pwd
/usr/local/nginx-1.22.0
[root@gl-node01 nginx-1.22.0]# nginx -v
-bash: nginx: command not found
not found的问题
nginx -v:command not found。找不到nginx,1、加./;2、需要配置系统环境变量
[root@gl-node01 sbin]# ./nginx -s reload
[root@gl-node01 sbin]# ./nginx -v
nginx version: nginx/1.22.0
解压后进行初始化
Complete!
[root@iZ2ze3txre2ntsrcn2dxzrZ nginx-1.21.2]# ./configure
checking for OS
+ Linux 4.18.0-193.14.2.el8_2.x86_64 x86_64
checking for C compiler ... found
+ using GNU C compiler
+ gcc version: 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC)
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
checking for gcc variadic macros ... found
checking for gcc builtin 64 bit byteswap ... found
checking for unistd.h ... found
[root@gl-node01 nginx-1.22.0]# cd sbin
bash: cd: sbin: No such file or directory
[root@gl-node01 nginx-1.22.0]#
[root@gl-node01 nginx-1.22.0]# ./configure --prefix=/usr/local/nginx
checking for OS
./configure: error: C compiler cc is not found 问题。gcc没装好你,你在装什么啊?!!
继续
[root@gl-node01 nginx-1.22.0]# make
make -f objs/Makefile
......
make[1]: Leaving directory `/usr/local/nginx-1.22.0'
[root@gl-node01 nginx-1.22.0]# make install
make -f objs/Makefile install
......
[root@gl-node01 nginx-1.22.0]# cd ..
[root@gl-node01 local]# ls
bin etc include lib64 nginx nginx-1.22.0.tar.gz sbin src
dist games lib libexec nginx-1.22.0 openssl share
[root@gl-node01 local]# cd nginx/sbin
[root@gl-node01 sbin]# ./nginx
杀死并重启nginx服务
[root@gl-node01 local]# cd nginx/sbin
[root@gl-node01 sbin]# ./nginx
[root@gl-node01 sbin]# ps
PID TTY TIME CMD
6932 pts/5 00:00:00 bash
7221 pts/5 00:00:00 su
7222 pts/5 00:00:00 bash
10357 pts/5 00:00:00 ps
[root@gl-node01 sbin]# ps aux | grep nginx
root 10309 0.0 0.0 22124 688 ? Ss 17:15 0:00 nginx: master process ./nginx
root 10310 0.0 0.0 22572 1272 ? S 17:15 0:00 nginx: worker process
root 10362 0.0 0.0 103328 868 pts/5 S+ 17:21 0:00 grep nginx
[root@gl-node01 sbin]# kill -9 10309 10310
[root@gl-node01 sbin]# cd /usr/local/nginx/sbin
配置系统环境变量,添加nginx
[root@gl-node01 sbin]# vim /etc/profile
配置完重新部署
~~~
......
export NGINX_HOME=/usr/local/nginx
export PATH=$PATH:$JAVA_HOME/bin:$HADOOP_HOME/bin:$HADOOP_HOME/sbin:$ZOOKEEPER_HOME/bin:${ORACLE_HOME}/bin/:$ORACLE_HOME/lib64:$SPARK_HOME/bin:$HIVE_HOME/bin:$REDIS_HOME/bin:$SQOOP_HOME/bin:$NGINX_HOME/sbin
~~~
[root@gl-node01 etc]# source /etc/profile
[root@gl-node01 etc]# nginx -v
nginx version: nginx/1.22.0
***修改nginx配置文件
[root@gl-node01 ~]# vi /usr/local/nginx/conf/nginx.conf
#需要修改的东西
server {
listen 7828; # 修改成自己的端口号
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/local/dist; # 项目打包生成的dist文件夹上传的位置
index index.html index.htm;
}
#error_page 404 /404.html;
修改完毕后,保证配置无误,重新启动nginx ,
可用nginx -t 检验nginx配置是否正确
配置无误后,使用 nginx -s reload 命令重新加载修改的配置。
[root@gl-node01 ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@gl-node01 ~]# nginx -s reload
[root@gl-node01 ~]# service nginx restart
nginx: unrecognized service
出现 nginx: unrecognized service 问题
下载nginx的启动脚本,wget -O init-deb.sh http://library.linode.com/assets/660-init-deb.sh
将脚本添加到init.d目录和生成可执行
sudo mv init-deb.sh /etc/init.d/nginx
sudo chmod +x /etc/init.d/nginx
加的nginx到系统启动(可能失败,因为上面已经配置过nginx环境变量并启动)
sudo /usr/sbin/update-rc.d -f nginx defaults
配置完成重新启动 nginx
service nginx restart
[root@gl-node01 ~]# service nginx restart
nginx: unrecognized service
[root@gl-node01 ~]# cd /usr/local/nginx
[root@gl-node01 nginx]# wget -O init-deb.sh http://library.linode.com/assets/660-init-deb.sh
--2022-10-17 10:44:37-- http://library.linode.com/assets/660-init-deb.sh
Resolving library.linode.com... 23.44.51.218, 23.44.51.168, 2600:140b:2::686d:8189, ...
Connecting to library.linode.com|23.44.51.218|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://www.linode.com/docs/assets/660-init-deb.sh [following]
--2022-10-17 10:44:42-- https://www.linode.com/docs/assets/660-init-deb.sh
Resolving www.linode.com... 23.44.51.168, 23.44.51.218, 2600:140b:2::686d:8189, ...
Connecting to www.linode.com|23.44.51.168|:443... connected.
ERROR: cannot verify www.linode.com’s certificate, issued by “/C=US/O=Let's Encrypt/CN=R3”:
Issued certificate has expired.
To connect to www.linode.com insecurely, use ‘--no-check-certificate’.
[root@gl-node01 nginx]# sudo mv init-deb.sh /etc/init.d/nginx
[root@gl-node01 nginx]# sudo chmod +x /etc/init.d/nginx
[root@gl-node01 nginx]# sudo /usr/sbin/update-rc.d -f nginx defaults
sudo: /usr/sbin/update-rc.d: command not found
[root@gl-node01 nginx]# service nginx restart
[root@gl-node01 nginx]#