1.环境
- 操作系统:CentOS 7.9
- 硬件配置:
- CPU:2核
- 内存:4GB
- 磁盘空间:50GB
- 软件依赖:
- Java运行环境(JRE/JDK11)
- MySQL数据库
- Git版本控制工具
- Maven构建工具
2.安装基础应用
1.更新系统包
在安装任何新软件之前,建议先更新系统包,以确保所有现有软件包都是最新的。
sudo yum update -y
2.安装JDK运行环境(根据自己的需求,我安装的是jdk11
sudo yum install -y java-11-openjdk-devel
sudo yum install -y java-1.8.0-openjdk-devel
检查java版本
java --version
3.安装mysql5.7
1.配置YUM源
wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
2.安装mysql源
yum localinstall mysql57-community-release-el7-8.noarch.rpm
3.检查mysql源是否安装成功
yum repolist enabled
4.修改mysql-community.repo源,改变默认安装的mysql版本
vim /etc/yum.repos.d/mysql-community.repo
需要安装哪个版本,就把该版本的enabled设置为1,比如要安装5.7版本,将5.7源的enabled=0改成enabled=1。然后再将其余yum源的enabled=1改成enabled=0即可。如下图所示:
安装MySQL
yum -y install mysql-community-server --nogpgcheck
启动MySQL服务
systemctl start mysqld
查看MySQL的启动状态
systemctl status mysqld
设置MySQL服务开机自启。
systemctl enable mysqld
1.找到临时密码
grep 'temporary password' /var/log/mysqld.log
进入mysql
mysql -uroot -p
修改root本地登录密码
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
如果内网telnet到,外网telnet有问题,执行一下命令
sudo firewall-cmd --list-ports
sudo firewall-cmd --permanent --add-port=3306/tcp
sudo firewall-cmd --reload
4.安装 redis
yum install redis
修改配置
cd /etc
vim redis.conf
注释掉 bind 127.0.0.1,bind 用于限制访问 Redis 的机器 ip,直接关掉
修改 daemonize no 为 yes,让 Redis 可以后台启动:
设置密码:
requirepass 123456
(1)启动
service redis start
(2)停止:
service redis stop
(3)检查状态:
service redis status
(4)查看进程:
ps -ef | grep redis
(5)设置redis开机自启:
chkconfig redis on
(7)修改配置文件:
vim /etc/redis.conf
(8)防火墙
sudo iptables -I INPUT -p tcp --dport 6379 -j ACCEPT
5.git安装
sudo yum install -y git
6.安装 nignx
安装Nginx:
yum install nginx
启动Nginx服务:
sudo systemctl start nginx
检查Nginx服务状态:
sudo systemctl status nginx
设置Nginx开机自启:
sudo systemctl enable nginx
7.安装ftp
# 安装
sudo yum install -y vsftpd
运行以下命令,启动FTP服务,并设置开机自启动。
sudo systemctl start vsftpd
sudo systemctl enable vsftpd
运行以下命令,查看FTP服务监听的端口。
sudo netstat -antup | grep ftp
配置vsftpd
运行以下命令,为FTP服务创建一个Linux用户。
本示例中,该用户名为ftptest。
sudo adduser ftptest
运行以下命令,修改ftptest用户的密码。
sudo passwd ftptest
运行以下命令,创建一个供FTP服务使用的文件目录。
sudo mkdir -p /var/ftp/test
运行以下命令,创建测试文件。
sudo touch /var/ftp/test/testfile.txt
运行以下命令,更改/var/ftp/test目录的拥有者为ftptest。
sudo chown -R ftptest:ftptest /var/ftp/test
运行以下命令,打开vsftpd的配置文件。
sudo vim /etc/vsftpd/vsftpd.conf
修改下列参数的值:
#禁止匿名登录FTP服务器。
anonymous_enable=NO
#允许本地用户登录FTP服务器。
local_enable=YES
#监听IPv4 sockets。
listen=YES
在行首添加#注释掉以下参数,关闭监听IPv6 sockets:
#listen_ipv6=YES
在配置文件的末尾添加下列参数,其中pasv_address需修改为FTP服务器的公网IP地址。
#设置本地用户登录后所在目录。
local_root=/var/ftp/test
#全部用户被限制在主目录。
chroot_local_user=YES
#启用例外用户名单。
chroot_list_enable=YES
#指定例外用户列表文件,列表中用户不被锁定在主目录。
chroot_list_file=/etc/vsftpd/chroot_list
#开启被动模式。
pasv_enable=YES
allow_writeable_chroot=YES
#本教程中为Linux实例的公网IP。
pasv_address=<FTP服务器公网IP地址>
#设置被动模式下,建立数据传输可使用的端口范围的最小值。
#建议您把端口范围设置在一段比较高的范围内,例如50000~50010,有助于提高访问FTP服务器的安全性。
pasv_min_port=50000
#设置被动模式下,建立数据传输可使用的端口范围的最大值。
pasv_max_port=50010
创建chroot_list文件,并在文件中写入例外用户名单。
sudo vim /etc/vsftpd/chroot_list
按Esc键,输入:wq,按Enter键关闭并保存配置文件。
如果使用 阿里云,注意开放端口
3.部署
1.数据库
大小写问题
1、编辑配置文件,在这个配置文件下添加这个参数
vim /etc/my.cnf
lower_case_table_names = 1 #不区分大小写
2.重启
systemctl start mysqld
查看MySQL的启动状态
systemctl status mysqld
2.后端打包
mvn clean package
shell脚本执行错误 $‘\r‘:command not found
如果没有安装dos2unix,可以通过以下命令安装:
sudo yum install -y dos2unix
使用sed命令转换文件格式。
sed -i 's/\r//' ry.sh
3.前端 打包
npm run dev
如果遇到这个问题 :
# 在运行 npm run build 构建命令时遇到内存不足的问题 # 提示这个错误 FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
1.增加node.js 的内存限制
# mac系统
export NODE_OPTIONS=--max_old_space_size=4096
# Windows系统
set NODE_OPTIONS=--max_old_space_size=4096
2.清理项目
# 清理项目的依赖
rm -rf node_modules
# 清理npm缓存
npm cache clean --force
# 重新安装依赖
npm install
# 再次尝试构建
npm run build:test
## 打包 prod 生产环境
npm run build:prod
4.nginx配置
# 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 auto;
error_log /var/log/nginx/error.log;
pid /run/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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
client_max_body_size 10m;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
#listen [::]:80;
server_name xxxx.xxxx.cn;
root /xxx/xxx/xxx
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
#access_log logs/host.access.log main;
client_max_body_size 10m;
gzip on;
gzip_min_length 1k;
gzip_comp_level 9;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";
index index.html index.htm;
#access_log logs/host.access.log main;
location / {
client_max_body_size 10m;
try_files $uri $uri/ /index.html;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ^~ /api/ {
add_header 'Access-Control-Allow-Origin' *;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS';
proxy_pass http://127.0.0.1:xxxx/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
5.ssl
自己去买,生成nginx所需的证书,放到服务器文件即可