Laravel 环境安装和配置

PHP环境的安装配置

下载PHP源码

1
2
3
reasonpun@vultr:~$ wget "http://cn2.php.net/get/php-7.1.4.tar.gz/from/this/mirror" php714.tar.gz
reasonpun@vultr:~$ tar xvzf php70.tar.gz
reasonpun@vultr:~$ cd php-7.0.7/

更新环境

1
reasonpun@vultr:~/php-7.0.7$ sudo apt-get update

安装组件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# debain
reasonpun@vultr:~/php-7.0.7$ sudo apt-get install libxml2-dev
reasonpun@vultr:~/php-7.0.7$ sudo apt-get install build-essential
reasonpun@vultr:~/php-7.0.7$ sudo apt-get install openssl
reasonpun@vultr:~/php-7.0.7$ sudo apt-get install libssl-dev
reasonpun@vultr:~/php-7.0.7$ sudo apt-get install make curl
reasonpun@vultr:~/php-7.0.7$ sudo apt-get install libcurl4-gnutls-dev
reasonpun@vultr:~/php-7.0.7$ sudo apt-get install libjpeg-dev
reasonpun@vultr:~/php-7.0.7$ sudo apt-get install libpng-dev
reasonpun@vultr:~/php-7.0.7$ sudo apt-get install libmcrypt-dev
reasonpun@vultr:~/php-7.0.7$ sudo apt-get install libreadline6
reasonpun@vultr:~/php-7.0.7$ sudo apt-get install libreadline6-dev
# centos 7
[root@i-d5n1vxda data]# yum groupinstall 'Development Tools'
[root@i-d5n1vxda data]# yum install gd.x86_64
[root@i-d5n1vxda data]# yum install readline.x86_64 readline-devel.x86_64
[root@i-d5n1vxda data]# yum install php-mysql.x86_64
[root@i-d5n1vxda php-7.1.4]# yum install libwebp.x86_64 libwebp-devel.x86_64
# centos7下需要源码安装libmcrypt
[root@i-d5n1vxda data]# wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz
[root@i-d5n1vxda libmcrypt-2.5.7]# make & make install

编译&安装PHP

1
2
3
reasonpun@vultr:~/php-7.0.7$ ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mysqli --with-pdo-mysql --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --disable-fileinfo --enable-maintainer-zts

reasonpun@vultr:~/php-7.0.7$ make && sudo make install

设置php-fpm运行用户

1
2
reasonpun@vultr:~/php-7.0.7$ cd /usr/local/php/etc
reasonpun@vultr:/usr/local/php/etc$ sudo cp php-fpm.conf.default php-fpm.conf
1
2
3
4
5
# 切换到配置文件目录
reasonpun@vultr:/usr/local/php/etc$ cd /usr/local/php/etc/php-fpm.d/
reasonpun@vultr:/usr/local/php/etc/php-fpm.d$ sudo vim www.conf.default
reasonpun@vultr:/usr/local/php/etc/php-fpm.d$ sudo mv www.conf.default www.conf

1
2
3
# 修改运行用户
user = www-data
group = www-data
1
2
# 启动php-fpm
reasonpun@vultr:/usr/local/php/etc/php-fpm.d$ sudo /usr/local/php/sbin/php-fpm
1
2
3
4
# 加入系统变量
sudo echo "PATH=$PATH:/usr/local/php/bin">> /etc/profile
sudo echo "export PATH">> /etc/profile
source /etc/profile
1
2
3
4
5
6
# 现在看看版本信息吧
reasonpun@vultr:/usr/local/php/etc/php-fpm.d$ php -v
PHP 7.0.7 (cli) (built: Jun 13 2016 16:12:37) ( ZTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
reasonpun@vultr:/usr/local/php/etc/php-fpm.d$

Composer 安装

下载安装文件

1
2
reasonpun@vultr:/data/web/17tu$ ls
reasonpun@vultr:/data/web/17tu$ php -r "copy('https://getcomposer.org/installer','composer-setup.php');"

验证文件合法性

1
2
reasonpun@vultr:/data/web/17tu$ php -r "if (hash_file('SHA384', 'composer-setup.php') === 'bf16ac69bd8b807bc6e4499b28968ee87456e29a3894767b60c2d4dafa3d10d045ffef2aeb2e78827fa5f024fbe93ca2') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
Installer verified

执行安装

1
2
3
4
5
6
7
8
reasonpun@vultr:/data/web/17tu$ php composer-setup.php
All settings correct for using Composer
Downloading 1.1.2...

Composer successfully installed to: /data/web/17tu/composer.phar
Use it: php composer.phar
reasonpun@vultr:/data/web/17tu$ ls
composer.phar  composer-setup.php

删除安装文件

1
2
3
reasonpun@vultr:/data/web/17tu$ php -r "unlink('composer-setup.php');"
reasonpun@vultr:/data/web/17tu$ ls
composer.phar

将composer执行脚本移动到bin目录下

1
2
3
4
reasonpun@vultr:/data/web/17tu$ mv composer.phar /usr/local/bin/composer
mv: cannot move ‘composer.phar’ to ‘/usr/local/bin/composer’: Permission denied
reasonpun@vultr:/data/web/17tu$ sudo mv composer.phar /usr/local/bin/composer

查看命令的可用性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
reasonpun@vultr:/data/web/17tu$ composer
Composer version 1.1.2 2016-05-31 19:48:11

Usage:
  command [options] [arguments]

Options:
  -h, --help                     Display this help message
  -q, --quiet                    Do not output any message
  -V, --version                  Display this application version
      --ansi                     Force ANSI output
      --no-ansi                  Disable ANSI output
  -n, --no-interaction           Do not ask any interactive question
      --profile                  Display timing and memory usage information
      --no-plugins               Whether to disable plugins.
  -d, --working-dir=WORKING-DIR  If specified, use the given directory as working directory.
  -v|vv|vvv, --verbose           Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:
  about           Short information about Composer
  archive         Create an archive of this composer package
  browse          Opens the package's repository URL or homepage in your browser.
  clear-cache     Clears composer's internal package cache.
  clearcache      Clears composer's internal package cache.
  config          Set config options
  create-project  Create new project from a package into given directory.
  depends         Shows which packages cause the given package to be installed
  diagnose        Diagnoses the system to identify common errors.
  dump-autoload   Dumps the autoloader
  dumpautoload    Dumps the autoloader
  exec            Execute a vendored binary/script
  global          Allows running commands in the global composer dir ($COMPOSER_HOME).
  help            Displays help for a command
  home            Opens the package's repository URL or homepage in your browser.
  info            Show information about packages
  init            Creates a basic composer.json file in current directory.
  install         Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json.
  licenses        Show information about licenses of dependencies
  list            Lists commands
  outdated        Shows a list of installed packages that have updates available, including their latest version.
  prohibits       Shows which packages prevent the given package from being installed
  remove          Removes a package from the require or require-dev
  require         Adds required packages to your composer.json and installs them
  run-script      Run the scripts defined in composer.json.
  search          Search for packages
  self-update     Updates composer.phar to the latest version.
  selfupdate      Updates composer.phar to the latest version.
  show            Show information about packages
  status          Show a list of locally modified packages
  suggests        Show package suggestions
  update          Updates your dependencies to the latest version according to composer.json, and updates the composer.lock file.
  validate        Validates a composer.json and composer.lock
  why             Shows which packages cause the given package to be installed
  why-not         Shows which packages prevent the given package from being installed
reasonpun@vultr:/data/web/17tu$

Lavarel 安装配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
reasonpun@vultr:/data/web/17tu$ composer global require "laravel/installer"
Changed current directory to /home/reasonpun/.composer
Using version ^1.3 for laravel/installer
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing symfony/process (v3.1.1)
    Downloading: 100%

  - Installing symfony/polyfill-mbstring (v1.2.0)
    Downloading: 100%

  - Installing symfony/console (v3.1.1)
    Downloading: 100%

  - Installing guzzlehttp/promises (1.2.0)
    Downloading: 100%

  - Installing psr/http-message (1.0)
    Downloading: 100%

  - Installing guzzlehttp/psr7 (1.3.0)
    Downloading: Connecting...

Nginx配置

编译Nginx源码

1
2
3

./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --with-pcre=../pcre-8.40 --with-zlib=../zlib-1.2.11 --lock-path=/var/run/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_gunzip_module  --with-http_gzip_static_module --with-http_auth_request_module --with-http_v2_module

nginx配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /your/path/to/lavarel_code;
    index index.php index.html index.htm;

    server_name 45.32.16.180;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    # PHP 支持
    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

更改目录权限

1
2
reasonpun@vultr:/data/web/17tu$ sudo chmod -R 777 bootstrap/cache
reasonpun@vultr:/data/web/17tu$ sudo chmod -R 777 storage/

参考文献

  • http://www.oschina.net/question/2010961_242272?fromerr=PD31w5Kw
  • https://getcomposer.org/doc/00-intro.md#installation-linux-unix-osx