欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Linux laravel安装

第一步:安装php套件

目前为止laravel是5.1版本,需要对php有要求,要php5.59以上

The Laravel framework has a few system requirements. Of course, all of these requirements are satisfied by the Laravel Homestead virtual machine:

    * PHP >= 5.5.9


    * OpenSSL PHP Extension


    * PDO PHP Extension


    * Mbstring PHP Extension


    * Tokenizer PHP Extension

ubuntu 14.2 64位(开发使用,主要因为php的包版本够高,满足laravel的要求)

apt-get install php5 libapache2-mod-php5 php5-mcrypt php5-curl php5-gd php5-json php5-mysqlnd openssl
apt-get install mysql-server libapache2-mod-auth-mysql php5-mysql curl

基本上这里安装完成,larvel的所需东西都齐全了。

第二步:安装composer工具

由于laravel是试用composer来做包依赖管理的,包括laravel的安装,所以还需要安装composer https://getcomposer.org/

对于composer其实不需要了解太多,只需要知道如何使用,如何安装即可
在没有被墙的情况下执行这2个命令

curl -sS https://getcomposer.org/installer | php #下载composer安装文件并使用php执行,以便生成一个composer.phar包

mv composer.phar /usr/local/bin/composer # 将生成的composer包放到一个存放命令包的目录,以便系统的$PATH能够搜索到这个命令,这样就可以在命令行直接使用composer命令了。

需要注意composer的执行权限

chmod +x /usr/local/bin/composer

第三步:安装laravel

确保有composer这个工具之后,我们就可以安装laravel了

composer global require "laravel/installer=~1.1"

执行这个命令成功后
会在本地目录生成一个 ~/.composer/vendor/bin 这样的目录,laravel命令就是放在了这个目录里面,为了方便我们使用laravel命令,所以我们需要将他放到系统的$PATH里面,因为我是在root执行上述那个命令的,所以我的包生成在了root的目录

export PATH=$PATH:/root/.composer/vendor/bin/

可以将这个命令写进去用户的环境变量配置文件里面
Ubuntu Linux系统环境变量配置文件:

不建议:/etc/profile : 在登录时,操作系统定制用户环境时使用的第一个文件 ,此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行。 

不建议: /etc /environment : 在登录时操作系统使用的第二个文件, 系统在读取你自己的profile前,设置环境文件的环境变量。 

建议:~/.profile :  在登录时用到的第三个文件 是.profile文件,每个用户都可使用该文件输入专用于自己使用的shell信息,当用户登录时,该文件仅仅执行一次!默认情况下,他设置一些环境变量,执行用户的.bashrc文件。 

不建议: /etc/bashrc : 为每一个运行bash shell的用户执行此文件.当bash shell被打开时,该文件被读取. 

建议~/.bashrc : 该文件包含专用于你的bash shell的bash信息,当登录时以及每次打开新的shell时,该该文件被读取。 

例如:

cat ~/.profile 
# ~/.profile: executed by Bourne-compatible login shells.

if [ "$BASH" ]; then
  if [ -f ~/.bashrc ]; then
    . ~/.bashrc
  fi
fi

mesg n
export PATH=$PATH:/root/.composer/vendor/bin/

第四步:创建laravel应用项目

安装好laravel后

laravel new blog

执行new命令,可以创建一个app,app其实就是一个网站应用的目录

这个项目的目录架构如下:

tree -d -L 1 /root/blog/
/root/blog/
├── app
├── bootstrap # 需要有apache的写入权限 
├── config
├── database
├── public    #apache的document目录需要指向它
├── resources
├── storage # 需要有apache的写入权限
├── tests
└── vendor

一个全新创建的 Laravel 项目的根目录中包含以下子目录:

app 目录:如你所料,包含应用程序的核心代码。我们之后将会很快深入地探讨这个目录的细节。

bootstrap 目录:包含的几个文件用于启动框架和配置自动加载功能,还有一个 cache 目录,用于存放框架自动生成的文件,能够加速框架启动。

config 目录:顾名思义,包含所有应用程序的配置文件。

database 目录:包含了数据库迁移与数据填充文件。如果你不介意的话,也可以将 SQLite 数据库文件存放在这个目录中。
The public directory contains the front controller and your assets (images, JavaScript, CSS, etc.). public 目录包含前面的控制器和你的资源文件 (图片、JavaScript、CSS,等等)。

resources 目录:包含你的视图、原始的资源文件 (LESS、SASS、CoffeeScript) 和本地化语言文件。

storage 目录:包含编译后的 Blade 模板、基于文件的 session、文件缓存和其他由框架生成的文件。
此目录下面包含三个子目录:app、framework 和 logs。app 目录用户存放应用程序所用到的任何任何文件;framework 目录用于存放由框架生成的文件和缓存文件;最后,logs 目录用于存放应用程序的日志文件。

tests 目录用于存放你的自动化测试文件。Laravel 默认自带了一个 PHPUnit 的实例。
vendor 目录用于存放 Composer 的依赖包。

第五步:配置apache

laravel会使用mod_rewrite和.htaccess

The framework ships with a public/.htaccess file that is used to allow URLs without index.php. If you use Apache to serve your Laravel application, be sure to enable the mod_rewrite module.

所以需要需要配置一下,以下包含所有apache用到的配置

1.配置一个虚拟主机

ubuntu的apache安装目录是

/etc/apache2/

配置文件/etc/apache2/ports.conf,主要是为了配置监听端口,默认是80,如果你不配置也可以,但是80因为太多应用使用了,所以我还是配置一下,不配置也可以的。apache2的端口配置改为了在ports.conf里,我配置的8011

Listen 8011

配置虚拟主机

ls /etc/apache2/sites-enabled/
000-default.conf 

默认有一个demo模板配置文件000-default.conf ,将其复制一份,当作我们自己使用的虚拟机主机配置文件

cp 000-default.conf testsite.conf

主要配置端口是8011(刚才配置的监听端口)和DocumentRoot目录位置,跟前面所说的一致,是public目录

cat /etc/apache2/sites-enabled/testsite.conf 
<VirtualHost *:8011>
     # The ServerName directive sets the request scheme, hostname and port that
     # the server uses to identify itself. This is used when creating
     # redirection URLs. In the context of virtual hosts, the ServerName
     # specifies what hostname must appear in the request's Host: header to
     # match this virtual host. For the default virtual host (this file) this
     # value is not decisive as it is used as a last resort host regardless.
     # However, you must set it for any further virtual host explicitly.
     #ServerName www.example.com

     DocumentRoot /var/www/html/laravel-v5.1.4/public  #这里laravel new blog的话会在本地创建目录,需要将整个blog目录移到apache的documentroot目录里,这样apache才能识别。

     # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
     # error, crit, alert, emerg.
     # It is also possible to configure the loglevel for particular
     # modules, e.g.
     #LogLevel info ssl:warn

     ErrorLog ${APACHE_LOG_DIR}/error.log
     CustomLog ${APACHE_LOG_DIR}/access.log combined

     # For most configuration files from conf-available/, which are
     # enabled or disabled at a global level, it is possible to
     # include a line for only one particular virtual host. For example the
     # following line enables the CGI configuration for this host only
     # after it has been globally disabled with "a2disconf".
     #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

2.配置开启mod_rewrite

在/etc/apache2/apache2.conf里找到

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
</Directory>

将AllowOverride None改为AllowOverride All

3.检查权限

因为ubuntu的apache是使用www-data用户来运行的。

简单的方法就是将整个larave目录设置为www-data用户所属

chown -R www-data  /var/www/html/laravel-v5.1.4/

这样就不存在权限不正常的情况了,不过按照严格标准的话,只需要上面提到的3个目录有写入权限即可。

4.开启apache

service apache2 start

访问http://XXXX:8011即可看到有larave的字样出现,代表安装配置成功。

检查排错和备注

1.检查apache2的日志

/var/logs/apache2/ 的access.log和error.log

2.关闭selinux和iptables

3.这里我只是列出了安装的必须项,其他的都是非必须项目,可以自行参考官网文档,剩余的那些就基本上可以看得懂了,即使我这样的人。
 

官网
http://laravel.com/
http://laravel.com/docs/5.1

中文网
http://www.golaravel.com/

composer官网
https://getcomposer.org/doc/00-intro.md

原文引用:
http://www.godblessyuan.com/2015/07/20/laravel5-1_install_tutorial/


来自 http://www.cnblogs.com/grimm/p/5049659.html

 

普通分类: