从 0 开始在 VPS 上部署 Laravel (CentOS 7.2)

准备工作

之前写了一个 Ubuntu 系统的搭建教程,这次以 CentOS 7.2 为例,来说说怎么部署一个最简单的 Laravel 应用。我这里都是以 root 权限运行,如果不是 root 用户,请在命令前加上sudo
先运行yum update -y

安装 lnmp 一键安装包

前期使用一键安装包可以有效防止莫名其妙出现的错误,简化安装步骤,这个安装过程预计需要1个小时左右的安装时间。
注意事项:
需要3GB以上硬盘剩余空间
需要128MB以上内存(如果为128MB的小内存VPS,Xen的需要有SWAP,OpenVZ的至少要有128MB以上的vSWAP或突发内存),注意小内存请勿使用64位系统!
安装MySQL 5.6或5.7及MariaDB 10必须1G以上内存!

首先安装 screen:yum install -y screen
然后运行screen -S lnmp
如果网路出现中断,可以执行命令 screen -r lnmp 重新连接安装窗口
然后是安装 lnmp:wget -c http://soft.vpser.net/lnmp/lnmp1.3-full.tar.gz && tar zxf lnmp1.3-full.tar.gz && cd lnmp1.3-full && ./install.sh lnmp
安装过程中需要输入 MySQL 的 root 密码,后面设置可以自己设置,或者参考我下面的设置。

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
+------------------------------------------------------------------------+
| LNMP V1.3 for CentOS Linux Server, Written by Licess |
+------------------------------------------------------------------------+
| A tool to auto-compile & install LNMP/LNMPA/LAMP on Linux |
+------------------------------------------------------------------------+
| For more information please visit http://www.lnmp.org |
+------------------------------------------------------------------------+
Please setup root password of MySQL.(Default password: root)
Please enter: root
MySQL root password: root
===========================
Do you want to enable or disable the InnoDB Storage Engine?
Default enable,Enter your choice [Y/n]: Y
You will disable the InnoDB Storage Engine!
===========================
You have 5 options for your DataBase install.
1: Install MySQL 5.1.73
2: Install MySQL 5.5.48 (Default)
3: Install MySQL 5.6.29
4: Install MariaDB 5.5.48
5: Install MariaDB 10.0.23
6: Install MySQL 5.7.11
Enter your choice (1, 2, 3, 4, 5 or 6): 2
You will install MySQL 5.5.48
===========================
You have 6 options for your PHP install.
1: Install PHP 5.2.17
2: Install PHP 5.3.29
3: Install PHP 5.4.45 (Default)
4: Install PHP 5.5.36
5: Install PHP 5.6.22
6: Install PHP 7.0.7
Enter your choice (1, 2, 3, 4, 5 or 6): 6
You will install PHP 7.0.7
===========================
You have 3 options for your Memory Allocator install.
1: Don't install Memory Allocator. (Default)
2: Install Jemalloc
3: Install TCMalloc
Enter your choice (1, 2 or 3): 1
You will install not install Memory Allocator.
Press any key to install...or Press Ctrl+c to cancel

配置 nginx

vi /usr/local/nginx/conf/nginx.conf
参考如下修改 nginx 的配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/laravel/public;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

部署 Laravel

安装 composer

1
2
3
4
5
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === 'e115a8dc7871f15d853148a7fbac7da27d6c0030b848d9b3dc09e2a0388afed865e6a3d6b3c0fad45c48e2b5fc1196ae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
mv composer.phar /usr/local/bin/composer

配置 Laravel

把文件larave上传至/home/wwwroot/目录下
修改storage文件夹权限chmod -R 775 /home/wwwroot/laravel/storage/
chown www:www -R /home/wwwroot/laravel/
编辑envDB_PASSWORD=root中的root改为之前设置的密码
之后运行命令mv env .env
运行composer install,在国内的话,安装时间会比较长

安装完成

现在打开浏览器访问 VPS 的 IP 地址来看看效果吧。
larave

参考文档

PHP 7 on CentOS/RHEL 6.8 and 7.2 via Yum
阿里云CentOS 7.1使用yum安装MySql5.6.24
从零开始部署 Laravel 项目
Download Composer
从 0 开始在 VPS 上部署 Laravel (Ubuntu 14.04)


从 0 开始在 VPS 上部署 Laravel (CentOS 7.2)
https://slw.im/2016/08/deploy-lavaral-on-centos/
作者
Ryo Shen
发布于
2016年8月13日
许可协议