ubuntu下docker環境、php環境以及laravel的安裝

ubuntu下docker環境、php環境以及laravel的安裝


由於在學習laravel,須要搭建一個php7的開發環境,常常要反覆卸載從新安裝各類軟件,多搞幾遍環境可能就被污染。全部想到了能夠使用docker來安裝容器,還方便擴展。安裝步驟:php

  • 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 >下載安裝器:html

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

全局調用composer設置mysql

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

2. ubuntu安裝laravel

下載laravel安裝器nginx

composer global require "laravel/installer"

laravel命令加入環境變量,實現全局調用laravel

sudo vim /etc/profile

在文件底部加入git

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

使環境變量生效sql

source /etc/profile

創建文件夾,修改權限,進入目錄docker

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

用命令創建laravel項目shell

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

修改項目文件裏面的storage 和 bootstrap/cache 權限 777bootstrap

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;
    #}
}