前端工程化之项目部署(防火墙操作,nginx, iptables安装配置)

1,715 阅读3分钟

服务器部署

  1. 购买一个服务器(本人购买阿里云一周试一试)

  2. 服务器购买之后登陆服务器: 在命令行中输入ssh root@47.111.175.75(47.111.175.75)服务器的ip,回车之后输入密码,进入服务器

  3. 可以通过相关命令查看防火墙配置

    • 启动防火墙systemctl start firewalld.service
    • 停止防火墙systemctl stop firewalld.service
    • 重启防火墙systemctl restart firewalld.service
    • 查看防火墙状态firewall-cmd --state
    • 查看已开放的端口firewall-cmd --list-ports
    • 开启特定端口firewall-cmd --zone=public --add-port=80/tcp --permanent
    • 关闭特定端口firewall-cmd --zone=public --remove-port=80/tcp --permanent
    • 查看特定端口是否开启firewall-cmd --query-port=80/tcp

    开启端口后要重启防火墙,才能看到已开启的端口

  4. 安装nginx

    • yum install nginx
    • nginx -v查看是否安装成功
    • nginx -t查看文件位置
    • nginx -c目录
    • nginx -s reload 重启
    • ps -ef |grep nginx 查看启用的nginx

nginx安装成功,并且端口开启之后,在浏览器中输入服务器的ip之后,就可以看到下面的结果

上传项目到服务器

  1. 在项目的根目录中,打开终端,输入scp -r ./dist/* root@47.111.175.75:/root/test/

    • ./dist/* 代表要上传dist文件夹下的所有文件,*代表所有文件
    • root@47.111.175.75是服务器名
    • /root/test/上传到服务器的位置,root是服务器自带的目录,test是在root下手动建立的
  2. 从服务器中下载文件到本地,以nginx.config为例。在本地文件下 scp root@47.111.175.75:/etc/nginx/nginx.conf ./

    • ./表示下载文件到当前目录

配置ngix访问url

在服务器的/etc/nginx/nginx.conf 做如下配置

    server {
        listen       80 default_server;
        location / {
            root    /root/test/;  # 项目文件地址
            index   index.html index.htm;  # 希望访问的页面
        }
    }

更改成功之后,重启nginx,就可以在浏览器里输入服务器ip看到项目的index页面了。如果不成功出现403

解决403

  • ps -ef |grep nginx 查看nginx

id为1754的root才是当前启用的,在nginx.conf里 user root; user名称要和启用的nginx对应

  • 不修改config文件,进入服务器的根目录下,执行chmod -R 777 root命令为root文件下的子目录添加别的权限,也能成功。

登陆服务器时默认进入的是服务器下的root目录,cd ..才进入上层目录,项目文件是放在root文件下的,root下的子文件,都会权限受限,需要采用上面2种方式的任意一种修正

iptables配置

为了方便的管理防火墙,iptables的安装闭不可少

  • 查看iptables是否安装:service iptables status
  • 安装iptables:yum install -y iptables 安装iptables时要先关闭防火墙
  • 安装iptables-services:yum install -y iptables-services
  • 注册iptables服务:systemctl enable iptables.service
  • 开启服务:systemctl start iptables.service
  • 查看状态:systemctl status iptables.service
  • iptables 和 iptables-services 安装完成,且正常启动之后,回到服务器的根目录,打开iptables文件 vim etc/sysconfig/iptables 文件内添加 -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT