如何在 CentOS 7 / Ubuntu 15.04 上安装 PHP 框架 Laravel

大家好,这篇文章将要讲述如何在 CentOS 7 / Ubuntu 15.04 上安装 Laravel。如果你是一个 PHP Web 的开发者,你并不需要考虑如何在琳琅满目的现代 PHP 框架中选择,Laravel 是最轻松启动和运行的,它省时省力,能让你享受到 web 开发的乐趣。Laravel 信奉着一个普世的开发哲学,通过简单的指导创建出可维护代码是最优先的,你将保持着高速的开发效率,能够随时毫不畏惧更改你的代码来改进现有功能。

如何在 CentOS 7 / Ubuntu 15.04 上安装 PHP 框架 Laravel

Laravel 安装并不繁琐,你只要跟着本文章一步步操作就能在 CentOS 7 或者 Ubuntu 15 服务器上安装。

1) 服务器要求

在安装 Laravel 前需要安装一些它的依赖前提条件,主要是一些基本的参数调整,比如升级系统到最新版本,sudo 权限和安装依赖包。

当你连接到你的服务器时,请确保你能通以下命令能成功的使用 EPEL 仓库并且升级你的服务器。

CentOS-7

  1. <span class="com">#</span><span class="kwd">yum</span><span class="pln"> install epel</span><span class="pun">-</span><span class="pln">release</span>
  2. <span class="com">#</span><span class="pln"> rpm </span><span class="pun">-</span><span class="typ">Uvh</span><span class="pln"> https</span><span class="pun">:</span><span class="com">//dl.Fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm</span>
  3. <span class="com">#</span><span class="pln"> rpm </span><span class="pun">-</span><span class="typ">Uvh</span><span class="pln"> https</span><span class="pun">:</span><span class="com">//mirror.webtatic.com/yum/el7/webtatic-release.rpm</span>
  4. <span class="com">#</span><span class="kwd">yum</span><span class="pln"> update</span>

Ubuntu

  1. <span class="com">#</span><span class="kwd">apt-get</span><span class="pln"> install Python</span><span class="pun">-</span><span class="pln">software</span><span class="pun">-</span><span class="pln">properties</span>
  2. <span class="com">#</span><span class="pln"> add</span><span class="pun">-</span><span class="pln">apt</span><span class="pun">-</span><span class="pln">repository ppa</span><span class="pun">:</span><span class="pln">ondrej</span><span class="pun">/</span><span class="pln">php5</span>
  3. <span class="com">#</span><span class="kwd">apt-get</span><span class="pln"> update</span>
  4. <span class="com">#</span><span class="kwd">apt-get</span><span class="pln"> install </span><span class="pun">-</span><span class="pln">y php5 mcrypt php5</span><span class="pun">-</span><span class="pln">mcrypt php5</span><span class="pun">-</span><span class="pln">gd</span>

2) 防火墙安装

系统防火墙和 SELinux 设置对于用于产品应用安全来说非常重要,当你使用测试服务器的时候可以关闭防火墙,用以下命令行设置 SELinux 成宽容模式(permissive)来保证安装程序不受它们的影响。

  1. <span class="com">#</span><span class="kwd">setenforce</span><span class="lit">0</span>

3) Apache, MariaDB, PHP 安装

Laravel 安装程序需要完成安装 LAMP 整个环境,需要额外安装 OpenSSL、PDO,Mbstring 和 Tokenizer 等 PHP 扩展。如果 LAMP 已经运行在你的服务器上你可以跳过这一步,直接确认一些必要的 PHP 插件是否安装好。

要安装完整 AMP 你需要在自己的服务器上运行以下命令。

CentOS

  1. <span class="com">#</span><span class="kwd">yum</span><span class="pln"> install httpd mariadb</span><span class="pun">-</span><span class="pln">server php56w php56w</span><span class="pun">-</span><span class="pln">mysql php56w</span><span class="pun">-</span><span class="pln">mcrypt php56w</span><span class="pun">-</span><span class="pln">dom php56w</span><span class="pun">-</span><span class="pln">mbstring</span>

要在 CentOS 7 上实现 MySQL / Mariadb 服务开机自动启动,你需要运行以下命令。

  1. <span class="com">#</span><span class="kwd">systemctl</span><span class="pln"> start httpd</span>
  2. <span class="com">#</span><span class="kwd">systemctl</span><span class="pln"> enable httpd</span>
  3. <span class="com">#</span><span class="kwd">systemctl</span><span class="pln"> start mysqld</span>
  4. <span class="com">#</span><span class="kwd">systemctl</span><span class="pln"> enable mysqld</span>

在启动 MariaDB 服务之后,你需要运行以下命令配置一个足够安全的密码。

  1. <span class="com">#</span><span class="pln">mysql_secure_installation</span>

Ubuntu

  1. <span class="com">#</span><span class="kwd">apt-get</span><span class="pln"> install mysql</span><span class="pun">-</span><span class="pln">server apache2 libapache2</span><span class="pun">-</span><span class="pln">mod</span><span class="pun">-</span><span class="pln">php5 php5</span><span class="pun">-</span><span class="pln">mysql</span>

4) 安装 Composer

在我们安装 Laravel 前,先让我们开始安装 composer。安装 composer 是安装 Laravel 的最重要步骤之一,因为 composer 能帮我们安装 Laravel 的各种依赖。

CentOS/Ubuntu

在 CentOS / Ubuntu 下运行以下命令来配置 composer 。

  1. <span class="com">#</span><span class="pln"> curl </span><span class="pun">-</span><span class="pln">sS https</span><span class="pun">:</span><span class="com">//getcomposer.org/installer | php</span>
  2. <span class="com">#</span><span class="kwd">mv</span><span class="pln"> composer</span><span class="pun">.</span><span class="pln">phar </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">composer</span>
  3. <span class="com">#</span><span class="kwd">chmod</span><span class="pun">+</span><span class="pln">x </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">composer</span>

如何在 CentOS 7 / Ubuntu 15.04 上安装 PHP 框架 Laravel

composer installation

5) 安装 Laravel

我们可以运行以下命令从 github 上下载 Laravel 的安装包。

  1. <span class="com">#</span><span class="kwd">wget</span><span class="pln"> https</span><span class="pun">:</span><span class="com">//github.com/laravel/laravel/archive/develop.zip</span>

运行以下命令解压安装包并且移动 document 的根目录。

  1. <span class="com">#</span><span class="pln"> unzip develop</span><span class="pun">.</span><span class="pln">zip</span>
  2. <span class="com">#</span><span class="kwd">mv</span><span class="pln"> laravel</span><span class="pun">-</span><span class="pln">develop </span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">www</span><span class="pun">/</span>

现在使用 compose 命令来安装目录下所有 Laravel 所需要的依赖。

  1. <span class="com">#</span><span class="kwd">cd</span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">www</span><span class="pun">/</span><span class="pln">laravel</span><span class="pun">-</span><span class="pln">develop</span><span class="pun">/</span>
  2. <span class="com">#</span><span class="pln"> composer install</span>

如何在 CentOS 7 / Ubuntu 15.04 上安装 PHP 框架 Laravel

compose laravel

6) 密钥

为了加密服务器,我们使用以下命令来生成一个加密后的 32 位的密钥。

  1. <span class="com">#</span><span class="pln"> php artisan key</span><span class="pun">:</span><span class="pln">generate</span>
  2. <span class="typ">Application</span><span class="pln"> key </span><span class="pun">[</span><span class="typ">Lf54qK56s3qDh0ywgf9JdRxO2N0oV9qI</span><span class="pun">]</span><span class="kwd">set</span><span class="pln"> successfully</span>

现在把这个密钥放到 'app.php' 文件,如以下所示。

  1. <span class="com">#</span><span class="kwd">vim</span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">www</span><span class="pun">/</span><span class="pln">laravel</span><span class="pun">-</span><span class="pln">develop</span><span class="pun">/</span><span class="pln">config</span><span class="pun">/</span><span class="pln">app</span><span class="pun">.</span><span class="pln">php</span>

如何在 CentOS 7 / Ubuntu 15.04 上安装 PHP 框架 Laravel

Key encryption

7) 虚拟主机和所属用户

在 composer 安装好后,分配 document 根目录的权限和所属用户,如下所示。

  1. <span class="com">#</span><span class="kwd">chmod</span><span class="lit">775</span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">www</span><span class="pun">/</span><span class="pln">laravel</span><span class="pun">-</span><span class="pln">develop</span><span class="pun">/</span><span class="pln">app</span><span class="pun">/</span><span class="pln">storage</span>
  2. <span class="com">#</span><span class="kwd">chown</span><span class="pun">-</span><span class="pln">R apache</span><span class="pun">:</span><span class="pln">apache </span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">www</span><span class="pun">/</span><span class="pln">laravel</span><span class="pun">-</span><span class="pln">develop</span>

用任意一款编辑器打开 apache 服务器的默认配置文件,在文件最后加上虚拟主机配置。

  1. <span class="com">#</span><span class="kwd">vim</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">httpd</span><span class="pun">/</span><span class="pln">conf</span><span class="pun">/</span><span class="pln">httpd</span><span class="pun">.</span><span class="pln">conf</span>

  1. <span class="typ">ServerName</span><span class="pln"> laravel</span><span class="pun">-</span><span class="pln">develop</span>
  2. <span class="typ">DocumentRoot</span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">www</span><span class="pun">/</span><span class="pln">laravel</span><span class="pun">/</span><span class="kwd">public</span>
  3. <span class="pln">start </span><span class="typ">Directory</span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">www</span><span class="pun">/</span><span class="pln">laravel</span>
  4. <span class="typ">AllowOverride</span><span class="typ">All</span>
  5. <span class="typ">Directory</span><span class="pln"> close</span>

现在我们用以下命令重启 apache 服务器,打开浏览器查看 localhost 页面。

CentOS

  1. <span class="com">#</span><span class="kwd">systemctl</span><span class="pln"> restart httpd</span>

Ubuntu

  1. <span class="com">#</span><span class="pln"> service apache2 restart</span>

8) Laravel 5 网络访问

打开浏览器然后输入你配置的 IP 地址或者完整域名(Fully qualified domain name)你将会看到 Laravel 5 的默认页面。

如何在 CentOS 7 / Ubuntu 15.04 上安装 PHP 框架 Laravel

Laravel Default

总结

Laravel 框架对于开发网页应用来说是一个绝好的的工具。所以,看了这篇文章你将学会在 Ubuntu 15 和 CentOS 7 上安装 Laravel, 之后你就可以使用这个超棒的 PHP 框架提供的各种功能和舒适便捷性来进行你的开发工作。

如果您有什么意见或者建议请在以下评论区中回复,我们将根据您宝贵的反馈来使我们的文章更加浅显易懂。


via: http://linoxide.com/linux-how-to/install-laravel-php-centos-7-ubuntu-15-04/

作者:Kashif 译者:NearTan 校对:Caroline

本文由 LCTT 原创编译,Linux中国 荣誉推出

相关推荐