內網(無法連接外網)服務器安裝nginx並部署vue網站
1:解決缺少gcc、pre-devel、openssl-devel、zlib-devel等依賴的問題
在無法連接外網的內網中或服務器不能連接網絡的環境下,因為缺少gcc、pre-devel、openssl-devel、zlib-devel等依賴,所以無法使用yum等方式安裝,所以本文給出一種適用於內網環境或離線環境的Nginx安裝方法。
1、查看自己想要安裝Nginx的linux服務器的版本號
cat /etc/redhat-release
2、下載相同版本的linux.iso文件
鏡像下載地址:http://vault.centos.org/7.7.1908/isos/x86_64/
下載完成后,按如下圖所示在iso文件中找到Packages文件夾中的文件
在Packages文件夾中找出以下文件(共25個)
autogen-libopts-5.18-5.el7.x86_64.rpm
cpp-4.8.2-16.el7.x86_64.rpm
gcc-4.8.2-16.el7.x86_64.rpm
glibc-devel-2.17-55.el7.x86_64.rpm
glibc-headers-2.17-55.el7.x86_64.rpm
kernel-headers-3.10.0-123.el7.x86_64.rpm
keyutils-libs-devel-1.5.8-3.el7.x86_64.rpm
krb5-devel-1.11.3-49.el7.x86_64.rpm
libcom_err-devel-1.42.9-4.el7.x86_64.rpm
libmpc-1.0.1-3.el7.x86_64.rpm
libselinux-devel-2.2.2-6.el7.x86_64.rpm
libsepol-devel-2.1.9-3.el7.x86_64.rpm
libverto-devel-0.2.5-4.el7.x86_64.rpm
mpfr-3.1.1-4.el7.x86_64.rpm
ntp-4.2.6p5-18.el7.centos.x86_64.rpm
ntpdate-4.2.6p5-18.el7.centos.x86_64.rpm
openssl098e-0.9.8e-29.el7.centos.x86_64.rpm
openssl-1.0.1e-34.el7.x86_64.rpm
openssl-devel-1.0.1e-34.el7.x86_64.rpm
openssl-libs-1.0.1e-34.el7.x86_64.rpm
pcre-devel-8.32-12.el7.x86_64.rpm
pkgconfig-0.27.1-4.el7.x86_64.rpm
tcl-8.5.13-4.el7.x86_64.rpm
zlib-1.2.7-13.el7.x86_64.rpm
zlib-devel-1.2.7-13.el7.x86_64.rpm
找出以上25個rpm包后將其上傳至linux服務器,執行以下代碼
rpm -Uvh ./*.rpm --nodeps --force
在上傳以上25個rpm包的文件夾中執行以上代碼,便可以安裝以上rpm包
至此,gcc、pre-devel、openssl-devel、zlib-devel等依賴缺失的問題解決。
安裝完成后可以使用gcc -v
和 g++ -v
指令查看環境是否安裝成功。
2:安裝nginx
因為內網和離線狀態等無法使用yum等指令,所以需要下載nginx安裝包至本地,再上傳至linux服務器,
可以在http://nginx.org/en/download.html
下載Nginx相關版本至本地,之后上傳至linux服務器root文件夾下(我將安裝包放在了 /usr/local
目錄下)
之后在存放安裝包的文件夾下,執行以下代碼解壓壓縮包
例:tar -zxvf nginx-1.14.2.tar.gz
進入解壓的目錄中
cd 文件名
例:cd nginx-1.14.2
執行文件,檢查配置文件是否生效
./configure
執行以下兩行代碼分別進行編譯和安裝依賴
make
make install
啟動nginx
進入cd /usr/local/nginx/sbin
執行ls
可以看到nginx
執行 ./nginx
便可啟動nginx
查看nginx是否啟動
ps -aux | grep nginx
若nginx已啟動,那么確認防火牆關閉后,便可以訪問nginx了 (一般初始地址為 http://localhost:80 還要確認本服務器上沒有其他已經安裝的程序在占用80端口,如果已經占用,則需要更改成其他端口)
systemctl status firewalld 查看防火牆狀態
systemctl stop firewalld 關閉防火牆
systemctl start firewalld 開啟防火牆
重啟nginx
驗證nginx配置文件是否正確,
進入nginx安裝目錄sbin下,輸入命令./nginx -t,即以下三行命令
cd /usr/local/nginx/sbin
ls
./nginx -t
重啟nginx服務
方法一:進入nginx可執行目錄sbin下,輸入命令./nginx -s reload 即可
cd /usr/local/nginx/sbin
./nginx -s reload
方法二:查找當前nginx進程號,然后輸入命令:kill -HUP 進程號 實現重啟nginx服務
ps -ef|grep nginx
kill -HUP 進程號
3:將網站部署至linux服務器
1:將vue前端項目build好, 一般是dist文件夾,將dist文件夾壓縮成tar格式的壓縮包后上傳至linux服務器(7-zip可以打包成這種格式),並記住壓縮包的存放路徑
2:將第一步上傳的壓縮包解壓
tar -xvf home_wh.tar
3:修改nginx.conf配置文件
進入相應文件夾
cd /usr/local/nginx/conf
編輯nginx.conf
vim nginx.conf
即可進入nginx.conf中,其中:
i 進入編輯模式
Esc 退出編輯模式
摁方向鍵中的 上 下 左 右 可以移動光標(滾珠和鼠標不管用)
每行前面有“#”的代表注釋
進入編輯模式后 shift+wq! 之后再摁enter即可保存並退出
以下為nginx中原始代碼(去掉注釋的版本見下面)
user nobody;
worker_processes 1;
#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 65;
#gzip on;
server {
listen 80;
server_name localhost;
charset utf-8;
#access_log logs/host.access.log main;
location / {
root /root/www/; ## 設置的地方
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
}
去注釋版
user nobody; #初始一般為nobody,改為root即可
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80; #監聽的端口,默認為80,如果其他程序也在使用80端口,則會產生沖突,將這里的80改為其他端口即可解決。如:8082
server_name localhost; #默認為localhost,也可改為服務器的ip地址
charset utf-8;
#access_log logs/host.access.log main;
location / {
root /root/www/; ## 上傳的dist的文件的位置
index index.html index.htm; #啟動的文件位置,可以是一個,也可以是多個
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
修改完后重啟nginx服務,即可更新。