從0開始搭建阿里雲服務器-前後端

做個筆記,萬一哪天出問題了有個操作指南
前端vue,後端SpringCloud,docker部署

各種安裝

Docker

先安裝Docker https://www.runoob.com/docker/centos-docker-install.html

sudo yum install -y yum-utils device-mapper-persistent-data lvm2
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install docker-ce docker-ce-cli containerd.io
systemctl daemon-reload && systemctl start docker
#測試
docker run hello-world

配置鏡像加速器,我使用的是阿里雲https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["填自己的加速器地址"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

開啓遠程訪問

vi /lib/systemd/system/docker.service
#修改ExecStart開頭的一行
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
systemctl daemon-reload
service docker restart

在服務器控制檯上添加安全組,放開2375端口

MySQL

docker pull mysql
#1 運行容器
docker run -itd --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql 
#2 進入容器
docker exec -it mysql bash 
#登陸mysql
mysql -u root -p 
#ps:修改配置 將docker的mysql的配置文件拷貝出來 因爲docker裏沒有vim mysql是容器的name
docker cp mysql:/etc/mysql/my.cnf /root/my.cnf
docker cp /root/my.cnf mysql:/etc/mysql/my.cnf

在服務器控制檯上添加安全組,放開3306端口

Redis

docker pull redis
# 創建文件夾存放配置文件和持久化數據
mkdir /root/docker/redis
mkdir /root/docker/redis/data
cd /root/docker/redis
#下載配置文件並修改
wget https://raw.githubusercontent.com/antirez/redis/5.0/redis.conf -O redis.conf
#------修改的內容------
bind 127.0.0.1 #註釋掉這部分,這是限制redis只能本地訪問
protected-mode no #默認yes,開啓保護模式,限制爲本地訪問
daemonize no #默認no,改爲yes意爲以守護進程方式啓動,可後臺運行,除非kill進程,改爲yes會使配置文件方式啓動redis失敗
dir  ./ #輸入本地redis數據庫存放文件夾(可選)
appendonly yes #redis持久化(可選)
requirepass  密碼 #配置redis訪問密碼
#-----------------
#運行 加上--privileged=true會啓動失敗
docker run -d --name redis -p 6379:6379 -v /root/docker/redis/redis.conf:/etc/redis/redis.conf -v /root/docker/redis/data:/data redis redis-server /etc/redis/redis.conf 
#進入
docker exec -it redis redis-cli
auth password

在服務器控制檯上添加安全組,放開6379端口

Nginx

主要工作是將配置文件掛載到docker的nginx中

如果需要配置反向代理,注意格式別寫錯了

#第一種
location /proxy/ {
proxy_pass http://127.0.0.1/;
}
#代理到URL:http://127.0.0.1/test.html

#第二種(相對於第一種,最後少一個 / )
location /proxy/ {
proxy_pass http://127.0.0.1;
}
#代理到URL:http://127.0.0.1/proxy/test.html

#第三種:
location /proxy/ {
proxy_pass http://127.0.0.1/aaa/;
}
代理到URL:http://127.0.0.1/aaa/test.html

#第四種(相對於第三種,最後少一個 / )
location /proxy/ {
proxy_pass http://127.0.0.1/aaa;
}
#代理到URL:http://127.0.0.1/aaatest.html

步驟

docker pull nginx
mkdir nginx
cd nginx
#創建目錄 用於外掛
mkdir html logs conf conf.d
docker cp ID:/etc/nginx/nginx.conf conf/nginx.conf
docker cp ID:/etc/nginx/conf.d/default.conf conf.d/default.conf

#基礎配置
vim conf/nginx.conf
#配置代理
vim nginx/conf.d/default.conf
#default.conf
server {
    listen       80;
    server_name  ip;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    #location /oauth-server/ {
        #proxy_pass http://ip:port/;
        #proxy_set_header X-real-ip $remote_addr;
        #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        #proxy_set_header Host $http_host;
    #}
    location / {
        root   /usr/share/nginx/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   /usr/share/nginx/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;
    #}
}


#運行
docker run -d --name nginx -p 80:80 --net host \
-v /root/nginx/html:/usr/share/nginx/html \
-v /root/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /root/nginx/logs:/var/log/nginx \
-v /root/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf \
nginx

Vue打包

#更改vue.config.js 
publicPath: './'
#運行
npm run build:prod
#如果不成功 看看package.json裏的scripts的build帶不帶參數

成功之後多出一個dis文件夾
image-20200516121240244
將dist路徑下的所有文件複製到剛纔創建的html下即可

後臺服務打包

docker環境默認爲ip:2375,所以需要在環境變量中添加DOCKER_HOST=tcp://ip:2375 ,然後重啓

Dockerfile

以eureka-server-single模塊爲例,在模塊根路徑下創建Dockerfile文件
在這裏插入圖片描述

內容如下

FROM java:8
VOLUME /tmp
ADD target/eureka-server-single-0.0.1-SNAPSHOT.jar app.jar
RUN bash -c 'touch /app.jar'
EXPOSE 8761
ENTRYPOINT ["java","-jar","/app.jar"]

配置是網上找的,大致說明一下意思:

FROM是基礎鏡像

ADD的前半部分是項目打成jar包之後的路徑

RUN是要執行的命令

EXPOSE是要暴露的端口

pom.xml

打包使用了dockerfile-maven-plugin,因此在build下配置

			<plugin>
                <groupId>com.spotify</groupId>
                <artifactId>dockerfile-maven-plugin</artifactId>
                <version>1.4.13</version>
                <executions>
                    <execution>
                        <id>default</id>
                        <goals>
                            <goal>build</goal>
                            <goal>push</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <!--     鏡像名稱-->
                    <repository>${project.artifactId}</repository>
				   <!--    鏡像的tag 默認就是latest-->
                    <tag>latest</tag>
                    <dockerfile>Dockerfile</dockerfile>
                    <!--     Dockerfile的路徑-->
                    <contextDirectory>${project.basedir}</contextDirectory>
                    <buildArgs>
                        <!--     項目jar包路徑-->
                        <!--     finalName = artifactId + version-->
                        <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
                    </buildArgs>
                </configuration>
            </plugin>

打包

配置完上述之後,直接用maven的Lifecycle打包即可

多模塊打包

打包失敗

要注意打包的項目是不是依賴其他的包。比如本項目需要oauth2模塊先啓動

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章