如何在centos7部署网站

如何在centos7部署网站

1.买服务器

推荐阿里云试用三个月。

2.安装数据库,Java ,nginx

java安装教程 https://www.youtube.com/watch?v=90-0dRxs1fs&ab_channel=LiquidWeb
mysql安装教程 https://www.bilibili.com/video/BV1qS4y1h77S/
nginx安装 yum install nginx

其他注意事项:修改服务器的用户名和密码,修改数据库的账号密码,

mysql快速执行sql文件命令,前提:登录进入到mysql

mysql> source \home\user\Desktop\test.sql;
或 

3.上传前端代码

前端打包前⚠️

前端修改请求路径的ip为服务器ip,不能是localhost

打包命令

前端构建代码命令(假设用npm):npm run build

打包后

会生成dist文件夹,将dist文件夹的内容上传到nginx的默认项目地址:/usr/share/nginx/html

⚠️注意:不用上传dist文件夹,否则请修改nginx配置文件的root 和index

4.打包及运行后端

后端打包前⚠️

后端需要修改yml的配置的数据库的用户名和密码为服务器的数据库的用户名和密码,等其他配置

打包

打包命令:mvn package

打包后

服务器运行命令:nohup java -jar 你的Jar包路径 &

运行后点击两次回车。

输出信息保存在文件nohup.out中,nohup.out在输入运行命令的路径中。

5.配置nginx

nginx配置文件路径:/etc/nginx/nginx.conf

修改前备份nginx.conf

以下是可供参考的配置

主要修改的地方为:

41行的server_name 填上自己的服务器的ip

53行的proxy_pass http://127.0.0.1:你的前端端口;

# 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;

    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   你的服务器地址;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        error_page 404 /404.html;
        location = /404.html {
        }
        location / {
                root  /usr/share/nginx/html;
                index index.html #这里不加;加了分号可能运行不了
				proxy_pass http://127.0.0.1:你的前端端口;
	    		proxy_set_header Host $proxy_host; # 修改转发请求头,让8080端口的应用可以受到真实的请求
    			proxy_set_header X-Real-IP $remote_addr;
    			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

         }
#---------开放swagger,自行开启或关闭
	location ~* ^(/v2|/webjars|/swagger-resources|/swagger-ui.html){
   	    proxy_set_header Host $host;
  	     proxy_set_header  X-Real-IP  $remote_addr;
  	     proxy_set_header X-Forwarded-For $remote_addr;
  	     #proxy_set_header Host $host:$server_port;
  	     proxy_set_header X-Forwarded-Proto $scheme;
  	     proxy_set_header X-Forwarded-Port $server_port;
  	     proxy_pass http://8.134.189.237:8081; # 后端服务地址
	}
#---------以上配置为开放swagger
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2;
#        listen       [::]:443 ssl http2;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }

#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

运行nginx

运行命令:systemctl start nginx

停止命令: systemctl start nginx

查看nginx状态: systemctl status nginx

开放端口

概念:

入方向:请求进入服务器的端口

出方向:执行查找

现在只配置入方向

建议开放:

前端端口

后端端口

80

22

posted @ 2023-06-06 15:35  xiuer211  阅读(135)  评论(0编辑  收藏  举报