ubuntu下docker环境、php环境以及laravel的安装



因为在学习 laravel ,需要搭建一个php7的开发环境,经常要反复卸载重新安装各种软件,多搞几遍环境可能就被污染。所有想到了可以使用 docker 来安装容器,还方便扩展。安装步骤:

  • ubuntu安装git php composer docker
  • ubutnu安装laravel
  • docker下载镜像,启动容器
  • 修改docker nginx容器的配置文件

1. ubuntu安装git php composer docker

  • 安装git
sudo apt-get install git

  • 安装php7.0
sudo apt-get install php7.0 php7.0-dev

  • 安装docker
sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates
sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
sudo vim /etc/apt/sources.list.d/docker.list
增加内容
deb https://apt.dockerproject.org/repo ubuntu-precise main
deb https://apt.dockerproject.org/repo ubuntu-trusty main
deb https://apt.dockerproject.org/repo ubuntu-wily main
deb https://apt.dockerproject.org/repo ubuntu-xenial main
sudo apt-get update
sudo apt-get install docker-engine

  • 安装composer
curl -sS https://getcomposer.org/installer | php

注意: 如果上述方法由于某些原因失败了,你还可以通过 php >下载安装器:

php -r "readfile('https://getcomposer.org/installer');" | php

全局调用composer设置

sudo mv composer.phar /usr/local/bin/composer

2. ubuntu安装laravel

下载laravel安装器

composer global require "laravel/installer"

laravel命令加入环境变量,实现全局调用

sudo vim /etc/profile

在文件底部加入

export PATH=~/.config/composer/vendor/bin:$PATH

使环境变量生效

source /etc/profile

建立文件夹,修改权限,进入目录

sudo mkdir /var/www
sudo chmod -R 777 /var/www
cd /var/www

用命令建立laravel项目

laravel new bolg
or
composer create-project --prefer-dist laravel/laravel blog

修改项目文件里面的storage 和 bootstrap/cache 权限 777

cd /var/www/bolg
chmod -R 777 storage
chmod -R 777 bootstrap/cache

3. docker下载镜像,启动容器

sudo docker pull mysql
sudo docker pull php:7.0-fpm
sudo docker pull nginx

sudo docker run -d -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=root mysql
sudo docker run -d --name php-fpm --link mysql:mysql -v /var/www:/var/www/html php
sudo docker run -d -p 80:80 --name nginx --link php-fpm:php --volumes-from php-fpm nginx

4. 修改docker nginx容器的配置文件

进入nginx容器

sudo docker exec -it nginx /bin/bash
apt-get update
apt-get isntall vim
vim /etc/nginx/conf.d/default.conf

用下面文件替换

请注意 fastcgi_pass ,它的值是你php容器的域名或者ip。例如:172.17.0.3:9000;

server {
listen 80;
server_name localhost;

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


root /var/www/html;
index index.php 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 $document_root;
fastcgi_pass 172.17.0.3:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$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;
#}
}