目前很多国企或者单位对保密性有要求,不允许宿主机环境连接外部网络,也不提供跳板机搭建代理访问,所以很多环境都需要自己手动编译安装调试,现在将自己的完整的环境部署流程记录下来,以做后续参考,本文操作时的操作系统如下。
系统环境 | CentOS 7.9.2009 |
---|
下载MobaXterm
等SSH
连接软件
确定系统版本
# 适用于红帽系列 / centos
cat /etc/redhat-release
# 适用于 Ubuntu / debian
lsb_release -a
本系统获取信息如下
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"
了解部分linux
相关命令
虽然无法连接外网,但是系统中本身具有部分yum
缓存可以安装例如vim
,无法使用 yum
安装的,采用上传编译
预安装
yum install vim
Linux命令众多,可以参考下方地址查阅所需命令
https://www.runoob.com/w3cnote/linux-common-command.html
防火墙
sudo firewall-cmd --list-ports
sudo firewall-cmd --zone=public --add-port=端口号/tcp --permanent
sudo firewall-cmd --reload
sudo firewall-cmd --zone=public --add-port=端口号/udp --permanent
sudo firewall-cmd --reload
文件
tar -zxvf xxx.gz
tar -xJf xxx.xz
端口
lsof -i:8080
kill -9 PID
如果不以 root 账户登录,使用 ssh 工具客户端进行文件上传可能会导致权限不足问题
连接服务,键入命令修改root
用户密码
sudo passwd root
修改配置项
vi /etc/ssh/sshd_config
配置
PermitRootLogin yes
重启服务
service sshd restart
在MobaXterm
上使用root
账户登录
yum
缓存有 jdk11
yum install java
成功安装
链接: https://caiyun.139.com/m/i?105CqKL86WHer 提取码:aSjt
构建存储地址
mkdir /opt/python3
将压缩包上传至该目录下并解压
tar -xJf Python-3.9.18.tar.xz
在解压后的Python 3.9文件夹中,执行以下命令来安装Python 3.9
./configure
make && make install
遇到缺少依赖包错误,先尝试使用yum
安装
python3 --version
链接: https://caiyun.139.com/m/i?105CpgTuMNOp6 提取码:gqEF
原系统中可能存在mariadb
,需要先清除该数据库
# 查询
rpm -qa|grep mariadb
# 清理
rpm -e --nodeps mariadb-libs
构建应用存放目录
mkdir /opt/mysql
将压缩包上传至该目录下并解压
tar -xvf mysql-8.0.34-1.el7.x86_64.rpm-bundle.tar -C mysql
解压该目录获得以下文件,依次使用rpm
安装
mysql-community-libs-8.0.34-1.el7.x86_64.rpm
mysql-community-embedded-compat-8.0.34-1.el7.x86_64.rpm
mysql-community-devel-8.0.34-1.el7.x86_64.rpm
mysql-community-server-8.0.34-1.el7.x86_64.rpm
mysql-community-libs-compat-8.0.34-1.el7.x86_64.rpm
mysql-community-client-8.0.34-1.el7.x86_64.rpm
mysql-community-common-8.0.34-1.el7.x86_64.rpm
mysql-community-test-8.0.34-1.el7.x86_64.rpm
按照如下顺序安装
rpm -ivh mysql-community-common-8.0.34-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-plugins-8.0.34-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-8.0.34-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-8.0.34-1.el7.x86_64.rpm
rpm -ivh mysql-community-icu-data-files-8.0.34-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-8.0.34-1.el7.x86_64.rpm
warning: mysql-community-devel-8.0.34-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
error: Failed dependencies:
pkgconfig(openssl) is needed by mysql-community-devel-8.0.16-2.el7.x86_64
解决方案
yum
缓存中应该有这些包,没有就手动编译
yum install openssl-devel.x86_64 openssl openssl.x86_64 -y
再次执行报错命令
warning: mysql-community-test8.0.34-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
error: Failed dependencies:
perl(Data::Dumper) is needed by mysql-community-test-8.0.34-1.el7.x86_64
perl(JSON) is needed by mysql-community-test-8.0.34-1.el7.x86_64
解决方案
yum install perl.x86_64 perl-devel.x86_64 -y
yum install perl-JSON.noarch -y
yum -y install autoconf
再次执行报错命令
mysql
运行情况systemctl status mysqld
service mysqld stop
mysqld --initialize --console
chown -R mysql:mysql /var/lib/mysql/
systemctl start mysqld
cat /var/log/mysqld.log
如下,获取到的临时密码就是 0d.Kds3fs:3W
2023-09-23T11:50:08.568177Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: 0d.Kds3fs:3W
[root@localhost /]# mysql -uroot -p
Enter password:
然后输入临时密码(输入时不会显示出来,输入完直接回车)
alter USER 'root'@'localhost' IDENTIFIED BY '你的密码';
alter USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '你的密码';
flush privileges;
use mysql;
update user set host = "%" where user='root';
flush privileges;
select host, user, authentication_string, plugin from user;
3306
端口,远程连接数据库,放行命令参考 2链接: https://caiyun.139.com/m/i?105CpMTVxjCZ5 提取码:0qyO
构建应用存放目录
mkdir /opt/redis
将文件上传至该目录下并解压
tar -zxvf redis-7.0.13.tar.gz
编译安装
make && make install
进入/opt/redis/redis-7.0.13
目录将 redis
解压目录下redis.conf
复制一份到 /etc/redis.conf
cp redis.conf /etc/redis.conf
vim /etc/redis.conf
配置允许后台启动
daemonize yes
配置允许远程连接
bind 0.0.0.0
配置远程访问连接密码
requirepass 你的密码
在/etc/systemd/system
下新建redis.service
,编辑录入以下内容
[Unit]
Description=Redis
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/bin/redis-server /etc/redis.conf
ExecReload=/usr/local/bin/redis-server -s reload
ExecStop=/usr/local/bin/redis-server -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
执行服务重载,redis 后台运行并且开启自启
# 重载配置
systemctl daemon-reload
# 重启redis
systemctl restart redis
系统可能默认自带 docker
,可以输入命令查
docker -v
如果没有可以 yum install docker
更多docker相关命令,请去查询文档
在无网络服务器中无法从网络直接下载镜像,但是我们可以本地上传并加载,下面是从已安装镜像服务器导出镜像的命令
docker save -o 导出文件名.tar 导出镜像名
# 例
docker save -o nginx.tar nginx:latest
将镜像上传至服务器,运行如下命令,导入镜像文件
docker load -i xxxx.tar
导入可能会出现依赖错误问题
docker: Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: unable to retrieve OCI runtime error (open /var/run/docker/containerd/daemon/io.containerd.runtime.v2.task/moby/99524ed55992ee515e4a47f54023826133a66d3aa573386675a2b5ad48d6b23d/log.json: no such file or directory): runc did not terminate successfully: exit status 127: runc: error while loading shared libraries: libseccomp.so.2: cannot open shared object file: No such file or directory
: unknown.
执行如下命令修复
yum install libseccomp
再次执行镜像导入命令
以构建onlyoffice
服务为例
docker run -i -t -d -p 9004:80 --restart=always onlyoffice/documentserver
将宿主机端口 9004 映射到 容器 80 端口,并设置自动重启, 指定容器使用的镜像名称为 onlyoffice/docmentserver
onlyoffice
镜像下载地址
链接: https://caiyun.139.com/m/i?105CfN2XO4gzx 提取码:WGGJ
在/etc/systemd/system
下新建docker.service
,编辑录入以下内容
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
[Service]
Type=notify
ExecStart=/usr/bin/dockerd --selinux-enabled=false -H unix://
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
[Install]
WantedBy=multi-user.target
执行服务重载
# 重载配置
systemctl daemon-reload
# 重启redis
systemctl restart docker
链接: https://caiyun.139.com/m/i?105CpMRR2Qu8p 提取码:Xgqt 复制内容打开中国移动云盘手机APP,操作更方便哦
mkdir /opt/nginx
tar -zxvf nginx-1.20.2.tar.gz
yum
缓存中有)yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
./configure --prefix=/usr/local/nginx --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-http_stub_status_module
make && make install
在/etc/systemd/system
下新建nginx.service
,编辑录入以下内容
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
执行服务重载
# 重载配置
systemctl daemon-reload
# 重启redis
systemctl restart nginx
mkdir /www/vue
Vue
项目打包并将dist
上传至该目录下
vim /usr/local/nginx/conf/nginx.conf
修改后如下所示
#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 koi8-r;
#access_log logs/host.access.log main;
location / {
root /www/vue/dist;
try_files $uri $uri/ /index.html;
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;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
systemctl restart nginx
80
端口如果没放行需要放行推荐安装 v16
版本,降至该版本后,未出现参考[3]中所提到的问题
官方下载地址 :https://nodejs.org/dist/
阿里云镜像地址:https://mirrors.aliyun.com/nodejs-release/
选择自己要下载的版本,下载后上传至服务器 /usr/local
此下的命令不可以直接复制,需要对应自己下载的版本
#解压文件
tar -zxvf /usr/local/node-v16.x-linux-x64.tar.gz -C /usr/local
#重命名文件夹
mv /usr/local/node-v16.x-linux-x64 /usr/local/node-v16.x
#删除下载的压缩包
rm -rf /usr/local/node-v16.x-linux-x64.tar.gz
配置环境变量
echo 'export NODE_HOME=/usr/local/node-v16.X' >> /etc/profile
echo 'export PATH=$PATH:$NODE_HOME/bin' >> /etc/profile
刷新配置
source /etc/profile
检查
node -v
对于一些在没有网络环境下,配置比较繁琐的项目,可以采用 docker
镜像构建上传至内网服务器装载的方式进行
现在以构建FastAPI(Python)
项目为例展示一下构建过程
项目结构如下
lco
src // 代码目录
main.py
requirement.txt
Dockerfile
...
pip list --format=freeze > requirements.txt
加入main函数,使执行启动命令
if __name__ == '__main__':
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=False)
# 设置基础镜像
FROM python:3.9
# 设置代码文件夹工作目录 /app
WORKDIR /app
# 复制当前代码文件到容器中 /app
ADD . /app
# 设置时间
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# 更新 pip 源
RUN pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
# 安装所需的包,这里的requirements文件名需和项目生成的一致
RUN pip install -r requirements.txt
# 指定端口
EXPOSE 8000 80
# 执行入口文件
CMD ["python", "main.py"]
进入项目目录下,执行如下命令
docker build -t 镜像名称 .
存在冲突依赖就清理修改requirement.txt
文件
将镜像导出并上传到内网服务器,运行如下命令装载镜像
也可以先在本机完成对应测试
docker load -i 镜像名称.tar
查看镜像列表
docker images
构建执行容器,并将容器网络挂载到宿主机
docker run --name 容器名称 -d --network=host 镜像名称
防火墙放行对应端口,例如我的端口就是 8000
执行访问测试
[1] Centos7离线安装mysql8. He_lh. CSDN [EB/OL]
[2] CentOS7.x编译安装nginx 1.20.2,实现HTTP2 . Dr.愽. 博客园 [EB/OL]
[3] CentOS安装NPM. 黄辉. 知乎 [EB/OL]
[4] 无网络环境,如何部署Docker镜像. 菜鸟厚非. CSDN [EB/OL]