1. laravel 学习 环境搭建
1. 项目环境
vagrant + laradock (因为 自己手动搭建环境太麻烦了 自己弄了一下 感觉还是有些漏洞 所以采用 laradock)
2. Vagrantfile
备注 : box ubuntu1604 (因为 网速太慢 自己下载之后再 add 的 网速不够可以 参考 ttps://c4ys.com/archives/1230 ) 可以改写为 ubuntu/xenial64 ( https://app.vagrantup.com/ubuntu/boxes/xenial64 )
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.require_version ">= 1.6.0"
boxes = [
{
:name=>"docker-host",
:eth1=>"192.168.205.11",
:mem=>"3072",
:cpu=>"1",
:port=>"8888"
}
]
Vagrant.configure(2) do |config|
config.vm.box = "ubuntu1604"
boxes.each do |opts|
config.vm.define opts[:name] do |config|
config.vm.hostname = opts[:name]
config.vm.network "forwarded_port", guest: 80, host: opts[:port]
config.vm.provider "vmware_fusion" do |v|
v.vmx["memsize"] = opts[:mem]
v.vmx["numvcpus"] = opts[:cpu]
end
config.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--memory", opts[:mem]]
v.customize ["modifyvm", :id, "--cpus", opts[:cpu]]
end
config.vm.network :private_network, ip: opts[:eth1]
end
end
config.vm.synced_folder "./labs", "/home/ubuntu/labs"
config.vm.provision "shell", privileged: true, path: "./setup.sh"
end
3. 创建 labs
mkdir labs
4. 编写 setup.sh
#setup.sh
# change apt-get source
sudo su
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
sudo tee >>/etc/apt/sources.list<<EOF
# deb cdrom:[Ubuntu 16.04 LTS _Xenial Xerus_ - Release amd64 (20160420.1)]/ xenial main restricted
deb-src http://archive.ubuntu.com/ubuntu xenial main restricted #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
deb http://mirrors.aliyun.com/ubuntu/ xenial multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse #Added by software-properties
deb http://archive.canonical.com/ubuntu xenial partner
deb-src http://archive.canonical.com/ubuntu xenial partner
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-security multiverse
EOF
sudo apt-get update
# install docker
sudo apt-get remove docker docker-engine docker.io
sudo apt-get update
sudo apt-get install -y \
linux-image-extra-$(uname -r) \
linux-image-extra-virtual
sudo apt-get install -y \
apt-transport-https \
ca-certificates \
curl \
software-properties-common
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo apt-key fingerprint 0EBFCD88
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
sudo apt-get update
sudo apt-get install -y docker-ce
sudo docker version
# create docker user and auth
sudo groupadd docker
sudo usermod -aG docker ubuntu
sudo systemctl restart docker
# change docker source
mkdir /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://v2ltjwbg.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
sudo docker info
# install docker-compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
5. 启动 vagrant 并 进入 容器
vagrant up
vagrant ssh docker-host
6. 因 mac/windows 的 labs 挂载 到了 虚拟机的 /home/ubuntu/labs 下 , 所以 在 labs 目录下使用 mac/windows 的 git 复制 laradock 到本地
在 labs 下 (git 请自行下载)
git clone https://github.com/laradock/laradock.git
7. 在 laradock 下 部署 nginx + php-fpm + rabbitmq + elasticseach + redis 的 环境
cp env-example .env
docker-compose up -d elasticsearch mysql rabbitmq nginx php-fpm redis
注意 如果 elasticseach 报错
max virtual memory areas vm.maxmapcount [65530] is too low
请在 虚拟机 上运行
sudo sysctl -w vm.max_map_count=262144
然后重新 build elasticseach 即可
docker-compose build elasticsearch
docker-compose restart elasticsearch
8. 查看各个项目 启动情况
docker-compose ps
9. 部署项目
想一个域名 (eg : laravel-demo.com) 配置到 host 文件 保存退出
sudo vim /etc/host (mac host , window 的话自己查一下咯)
192.168.205.11 laravel-demo.com
在 labs 下 创建 一个 laravel 5.5 的项目
# 全局 安装 composer (php 的话 自己解决一下)
# curl --silent -o /tmp/composer-setup.php https://getcomposer.org/installer
# php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer
composer create-project --prefer-dist laravel/laravel=5.5.* laravel
在 laradock 下 nginx 下 sites 复制 default.conf 一个 相同的 conf 做 稍许修改 (eg: laravel-demo.conf)
server {
listen 80;
listen [::]:80;
# For https
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server ipv6only=on;
# ssl_certificate /etc/nginx/ssl/default.crt;
# ssl_certificate_key /etc/nginx/ssl/default.key;
server_name laravel-demo.com;
root /var/www/laravel-demo/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass php-upstream;
fastcgi_index index.php;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fixes timeouts
fastcgi_read_timeout 600;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
location /.well-known/acme-challenge/ {
root /var/www/letsencrypt/;
log_not_found off;
}
}
重新 build nginx
docker-compose build nginx
docker-compose restart nginx
10 访问部署网站
laravel-demo.com