从一台全新的Ubuntu服务器使用Docker运行Laravel项目
从一台全新的 Ubuntu 服务器使用 Docker 运行 Laravel 项目
服务器环境
服务器是全新的腾讯云的轻量应用香港服务器(37 元),这种轻量级的服务器,很适合用于尝试新东西(玩坏了,方便重置…)
- 一核 2G
- Ubuntu Server 20.04 LTS 64bit
- 选择香港地区,可以有效避免科学上网带来的一系列小问题
安装 Docker
避免重复写相同的代码,将安装步骤写成一个 shell
脚本
$ touch install_docker.sh && sudo chmod +x install_docker.sh
$ vim install_docker.sh
输入以下内容到 install_docker.sh
# 安装 docker
sudo apt update -y
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"
sudo apt update -y
apt-cache policy docker-ce
sudo apt install docker-ce -y
# 开始安装 docker-compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
docker-compose --version
保存后,执行该脚本 bash install_docker.sh
。
然后,输入 docker info
安装 Portainer 方便管理和查看 Docker 容器、镜像、日志
官网:https://www.portainer.io/
避免重复写相同的代码,将安装步骤写成一个 shell
脚本
$ touch install_portainer.sh && sudo chmod +x install_portainer.sh
$ vim install_portainer.sh
输入以下内容到 install_portainer.sh
#!/bin/bash
docker volume create portainer_data
docker run -d -p 8000:8000 -p 9000:9000 --name=portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce
docker run -d -p 9001:9001 --name portainer_agent --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/docker/volumes:/var/lib/docker/volumes portainer/agent
保存后,执行该脚本 bash install_portainer.sh
,等待安装完成。
安装完成后,访问本机 IP 或域名的9000
端口。
设置一下账户,密码。
进入之后,选择Docker
。
然后你就能看到:
还有容器日志:
完成。
安装 Laravel 项目所需环境
官网:https://laradock.io/
$ git clone https://github.com/Laradock/laradock.git
$ cp .env.example .env
$ docker-compose up -d nginx mysql
使用 docker-compose
启动 nginx
和mysql
当 启动
nginx 时,它会自动启动 php-fpm 和 workspace
。
这个逻辑,查看 docker-compose.yml
,发现:
上图是nginx server
,当收到了指令,当要启动 nginx
时,它会在 php-fom
他会先启动php-fpm
上图为php-fpm
,当它要启动时,指定它需要先启动 workspace
。
这也就是为什么启动了 nginx
一个服务时,php-fpm
和 workspace
都启动了。
depends_on
决定了容器之间的启动顺序,也决定了它们之间的依赖关系。
我们存放项目的容器是 workspace
所以,我们现在要进入 workspace
,拉取项目,初始化项目。
$ cd /data/www/laradock
$ docker-compose exec workspace bash
$ cd /vat/www/
$ git clone your_project && cd your_project
# 做项目初始化的命令
$ composer install ......
配置 Nginx
刚刚已经将项目部署并初始化完成了。
Nginx 配置文件
server {
server_name xxx.xxx.xxx;
root /var/www/xxx.com/public; #docker 容器内目录
# php conf
location ~ [^/]\.php(/|$) {
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
# laravel conf
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
}
接下要将该配置文件上传到服务器,并上传到 nginx conf 目录中。
先看看这个文件:
这一步,docker-compose
将服务器的${NGINX_SITES_PATH}
目录和 nginx 容器的 /etc/nginx/sites/available
目录做了映射。
valumes
定制了容器和宿主机的映射关系。
这里,是将 nginx
容器内的 /etc/nginx/sites/available
目录映射到了宿主机的${NGINX_SITES_PATH}
目录。
所以,我们只要将nginx
配置文件,上传到服务器的${NGINX_SITES_PATH}
目录,nginx
容器即可使用该配置文件。
然后进入nginx
容器内执行 nginx -s reload
使用 certbot 生成 ssl 证书
$ cd /data/www/laradock
$ vim .env
修改 APP_DOMAIN
参数为目标域名,然后:
$ docker-compose up -d certbot
看一下 certbot volumes
:
我们看到,certbot
将 var/certs
目录映射到了./data/certbot/certs
目录
然后再看 Nginx volumes
:
它将./data/certbot/certs
目录映射到了nginx
容器的 /var/certs
目录。
所以效果是:
certbot
生成了 ssl
证书,生成成功后,会在certbot
容器的 /var/certs
目录下,而 /var/certs
目录映射到了宿主机的 ./data/certbot/certs
目录下,然后Nginx
将./data/certbot/certs
目录映射到了 Nginx
容器的 /var/certs
目录下了。
经过以上流程,最终效果是:certbot
生成了 ssl证书
,在Nginx
容器中可以看到生成出来的证书,所以Nginx
也可以将看到的ssl
证书来做https
服务。
Nginx 配置模板:
server {
server_name your_domain;
root your_dir;
include enable-certbot-verify.conf;
include enable-php.conf;
include enable-laravel.conf;
listen 443 ssl;
ssl_certificate /var/certs/your_ssl;
ssl_certificate_key /var/certs/your_ssl_key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
}
server {
if ($host = your_domain) {
return 301 https://$host$request_uri;
}
server_name your_domain;
listen 80;
return 404;
}
enable-certbot-verify.conf
的文件内容是:
location /.well-known/acme-challenge/ {
root /var/www/letsencrypt/;
log_not_found off;
}
这是给 certbot
生成SSL
证书时,来做验证使用的。
完成。