|
| 1 | +环境安装 |
| 2 | +--- |
| 3 | + |
| 4 | +## jdk安装 |
| 5 | + |
| 6 | +```bash |
| 7 | +# ubuntu |
| 8 | +apt install openjdk-8-jdk |
| 9 | + |
| 10 | +# centos |
| 11 | +yum install openjdk-8-jdk |
| 12 | +``` |
| 13 | + |
| 14 | +安装完毕之后,执行 `java` , `javac`命令进行验证 |
| 15 | + |
| 16 | +## maven安装 |
| 17 | + |
| 18 | +```bash |
| 19 | +cd ~ |
| 20 | +mkdir soft |
| 21 | +cd soft |
| 22 | +wget https://dlcdn.apache.org/maven/maven-3/3.8.6/binaries/apache-maven-3.8.6-bin.tar.gz |
| 23 | +tar -zxvf apache-maven-3.8.6-bin.tar.gz |
| 24 | + |
| 25 | +vim ~/.bashrc |
| 26 | + |
| 27 | +# 在最后添加环境变量 |
| 28 | +export M2_HOME=/home/admin/soft/apache-maven-3.8.6 |
| 29 | +PATH=$M2_HOME/bin:$PATH |
| 30 | + |
| 31 | +# 配置生效 |
| 32 | +source ~/.bashrc |
| 33 | +``` |
| 34 | + |
| 35 | +配置完成之后执行命令 `mvn --version` 进行验证 |
| 36 | + |
| 37 | +国内添加阿里的镜像源,加快下载速度 |
| 38 | + |
| 39 | +```bash |
| 40 | +vim ~/soft/apache-maven-3.8.6/conf/settings.xml |
| 41 | + |
| 42 | +# 在<mirros>标签中,添加下面的镜像源 |
| 43 | + |
| 44 | + <mirror> |
| 45 | + <id>alimaven</id> |
| 46 | + <name>aliyun-maven</name> |
| 47 | + <url>http://maven.aliyun.com/nexus/content/groups/public/</url> |
| 48 | + <mirrorOf>central</mirrorOf> |
| 49 | + </mirror> |
| 50 | +``` |
| 51 | + |
| 52 | +## nginx配置 |
| 53 | + |
| 54 | +配置访问域名 |
| 55 | + |
| 56 | +```bash |
| 57 | +cd /usr/local/nginx/conf/ |
| 58 | + |
| 59 | +vim nginx.conf |
| 60 | + |
| 61 | +# 添加子域名解析,每个域名一个独立的配置文件 |
| 62 | +# 在http的一级标签中,添加如下一行配置,表示在conf.d文件下的所有conf结尾的文件,都属于我们需要使用的nginx配置信息 |
| 63 | +include /usr/local/nginx/conf/conf.d/*.conf; |
| 64 | +``` |
| 65 | + |
| 66 | +添加论坛的域名解析规则 |
| 67 | + |
| 68 | +```conf |
| 69 | +vim conf.d/forum.conf |
| 70 | +
|
| 71 | +
|
| 72 | +# 内容如下 |
| 73 | +upstream forum_host { |
| 74 | + server 127.0.0.1:8080; |
| 75 | +} |
| 76 | +server { |
| 77 | + server_name forum.hhui.top; |
| 78 | +
|
| 79 | + gzip on; |
| 80 | + gzip_buffers 32 4K; |
| 81 | + gzip_comp_level 6; |
| 82 | + gzip_min_length 100; |
| 83 | + gzip_types application/javascript text/css text/xml; |
| 84 | + gzip_disable "MSIE [1-6]\."; #配置禁用gzip条件,支持正则。此处表示ie6及以下不启用gzip(因为ie低版本不支持) |
| 85 | + gzip_vary on; |
| 86 | +
|
| 87 | + location ~* ^.+\.(ico|gif|jpg|jpeg|png)$ { |
| 88 | + access_log off; |
| 89 | + expires 1d; |
| 90 | + proxy_pass http://forum_host; |
| 91 | + proxy_set_header Host $host; |
| 92 | + proxy_set_header X-Real-IP $remote_addr; |
| 93 | + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
| 94 | + } |
| 95 | +
|
| 96 | + location ~* ^.+\.(css|js|txt|xml|swf|wav|pptx)$ { |
| 97 | + access_log off; |
| 98 | + expires 10m; |
| 99 | + proxy_pass http://forum_host; |
| 100 | + proxy_set_header Host $host; |
| 101 | + proxy_set_header X-Real-IP $remote_addr; |
| 102 | + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
| 103 | + } |
| 104 | +
|
| 105 | + location / { |
| 106 | + proxy_set_header X-real-ip $remote_addr; |
| 107 | + proxy_pass http://127.0.0.1:8080/; |
| 108 | + proxy_redirect default; |
| 109 | + } |
| 110 | +
|
| 111 | + listen 443 ssl; # managed by Certbot |
| 112 | + ssl_certificate /usr/local/nginx/conf/conf.d/cert.pem; |
| 113 | + ssl_certificate_key /usr/local/nginx/conf/conf.d/key.pem; |
| 114 | + ssl_stapling on; |
| 115 | + ssl_stapling_verify on; |
| 116 | + resolver 8.8.8.8 8.8.4.4 1.1.1.1 valid=60s; |
| 117 | + resolver_timeout 2s; |
| 118 | +} |
| 119 | +
|
| 120 | +
|
| 121 | +server { |
| 122 | + if ($host = forum.hhui.top) { |
| 123 | + return 301 https://$host$request_uri; |
| 124 | + } # managed by Certbot |
| 125 | +
|
| 126 | +
|
| 127 | + listen 80; |
| 128 | + server_name forum.hhui.top; |
| 129 | + return 404; # managed by Certbot |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +证书使用let's encrypt生成 |
| 134 | + |
| 135 | +## 数据库创建 |
| 136 | + |
| 137 | +```bash |
| 138 | +# ubuntu |
| 139 | +sudo apt-get install mysql-server |
| 140 | + |
| 141 | +# centos |
| 142 | +yum install mysql mysql-server mysql-libs |
| 143 | +``` |
| 144 | + |
| 145 | +### 查询登录密码 |
| 146 | + |
| 147 | +```bash |
| 148 | +grep "temporary password" /var/log/mysqld.log |
| 149 | + |
| 150 | +## 输出如下 |
| 151 | +# A temporary password is generated for root@localhost: xxxx |
| 152 | +``` |
| 153 | + |
| 154 | +### 密码修改: |
| 155 | + |
| 156 | +使用`set password` |
| 157 | + |
| 158 | +**格式:** |
| 159 | + |
| 160 | +``` |
| 161 | +mysql> set password for 用户名@localhost = password('新密码'); |
| 162 | +``` |
| 163 | + |
| 164 | +**例子:** |
| 165 | + |
| 166 | +``` |
| 167 | +mysql> set password for root@localhost = password('123'); |
| 168 | +``` |
| 169 | + |
| 170 | +update 方式 |
| 171 | + |
| 172 | +``` |
| 173 | +mysql> use mysql; |
| 174 | +
|
| 175 | +mysql> update user set password=password('123') where user='root' and host='localhost'; |
| 176 | +
|
| 177 | +mysql> flush privileges; |
| 178 | +``` |
| 179 | + |
| 180 | +添加用户 |
| 181 | + |
| 182 | +``` |
| 183 | +alter user 'root'@'localhost' identified by 'test'; |
| 184 | +create user 'test'@'%' IDENTIFIED BY 'test'; |
| 185 | +``` |
| 186 | + |
| 187 | +授予权限 |
| 188 | + |
| 189 | +``` |
| 190 | +# root 方式登录 |
| 191 | +grant all PRIVILEGES on test.* to 'yihui'@'%' IDENTIFIED by 'test'; |
| 192 | +flush privileges; |
| 193 | +``` |
| 194 | + |
| 195 | +本项目在首次启动时,会自动创建数据库 + 表结构,无需额外操作;只是需要修改源码中的生产环境配置 |
| 196 | + |
| 197 | +## 配置调整 |
| 198 | + |
| 199 | +线上部署时,选择prod环境,因此需要设置对应的数据库相关配置信息 |
| 200 | + |
| 201 | +resources-env/prod/application-dal.yml |
| 202 | + |
| 203 | +```yml |
| 204 | +spring: |
| 205 | + datasource: |
| 206 | + url: jdbc:mysql://xxx/forum?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai |
| 207 | + username: xxx |
| 208 | + password: xxx |
| 209 | +``` |
| 210 | +
|
| 211 | +根据实际的情况进行修改 |
| 212 | +
|
| 213 | +## 启动脚本 |
| 214 | +
|
| 215 | +基于源码的部署脚本 |
| 216 | +
|
| 217 | +```bash |
| 218 | +#!/usr/bin/env bash |
| 219 | + |
| 220 | +WEB_PATH="paicoding-web" |
| 221 | +JAR_NAME="paicoding-web-0.0.1-SNAPSHOT.jar" |
| 222 | + |
| 223 | +# 部署 |
| 224 | +function start() { |
| 225 | + git pull |
| 226 | + |
| 227 | + # 杀掉之前的进程 |
| 228 | + cat pid.log| xargs -I {} kill {} |
| 229 | + mv ${JAR_NAME} ${JAR_NAME}.bak |
| 230 | + |
| 231 | + mvn clean install -Dmaven.test.skip=True -Pprod |
| 232 | + cd ${WEB_PATH} |
| 233 | + mvn clean package spring-boot:repackage -Dmaven.test.skip=true -Pprod |
| 234 | + cd - |
| 235 | + |
| 236 | + mv ${WEB_PATH}/target/${JAR_NAME} ./ |
| 237 | + echo "启动脚本:===========" |
| 238 | + echo "nohup java -server -Xms512m -Xmx512m -Xmn512m -XX:NativeMemoryTracking=detail -XX:-OmitStackTraceInFastThrow -jar ${JAR_NAME} > /dev/null 2>&1 &" |
| 239 | + echo "===========" |
| 240 | + nohup java -server -Xms512m -Xmx512m -Xmn512m -XX:NativeMemoryTracking=detail -XX:-OmitStackTraceInFastThrow -jar ${JAR_NAME} > /dev/null 2>&1 & |
| 241 | + echo $! 1> pid.log |
| 242 | +} |
| 243 | + |
| 244 | +# 重启 |
| 245 | +function restart() { |
| 246 | + # 杀掉之前的进程 |
| 247 | + cat pid.log| xargs -I {} kill {} |
| 248 | + # 重新启动 |
| 249 | + echo "启动脚本:===========" |
| 250 | + echo "nohup java -server -Xms512m -Xmx512m -Xmn512m -XX:NativeMemoryTracking=detail -XX:-OmitStackTraceInFastThrow -jar ${JAR_NAME} > /dev/null 2>&1 &" |
| 251 | + echo "===========" |
| 252 | + nohup java -server -Xmn512m -Xmn512m -Xmn512m -XX:NativeMemoryTracking=detail -XX:-OmitStackTraceInFastThrow -jar ${JAR_NAME} > /dev/null 2>&1 & |
| 253 | + echo $! 1> pid.log |
| 254 | +} |
| 255 | + |
| 256 | +if [ $# == 0 ]; then |
| 257 | + echo "miss command: start | restart" |
| 258 | +elif [ $1 == 'start' ]; then |
| 259 | + start |
| 260 | +elif [ $1 == 'restart' ];then |
| 261 | + restart |
| 262 | +else |
| 263 | + echo 'illegal command, support cmd: start | restart' |
| 264 | +fi |
| 265 | +``` |
| 266 | + |
| 267 | +启动命令 |
| 268 | + |
| 269 | +```bash |
| 270 | +# 进入项目根目录,执行命令 |
| 271 | +# chmod +x launch.sh # 若脚本没有执行权限,则取消这行命令的注释,用于添加执行权限 |
| 272 | +./launch.sh start |
| 273 | +``` |
0 commit comments