Laravel是一个免费、开源、灵活、轻量级的 PHP 框架,采用模型-视图控制器( MVC ) 设计结构。它具有精致、简单且可读的语法,可用于从头开始开发现代、健壮且功能强大的应用程序。此外,Laravel还附带了多种工具,您可以使用它们来编写干净、现代且可维护的 PHP 代码。
另请阅读:如何在 CentOS 中安装 Laravel PHP Web 框架
在本文中,我将解释如何在Ubuntu 18.04、16.04和14.04 LTS (长期支持)上安装和运行最新版本的 Laravel 5.6 PHP 框架,并支持Apache2和PHP 7.2。
系统要求
您的系统必须满足以下要求才能运行最新版本的Laravel:
- PHP >= 7.1.3,带有 OpenSSL、PDO、Mbstring、Tokenizer、XML、Ctype 和 JSON PHP 扩展。
- Composer – PHP 的应用程序级包管理器。
安装先决条件
首先,确保使用以下命令更新您的系统源和现有软件包。
$ sudo apt-get 更新 $ sudo apt-get 升级
在 Ubuntu 上安装 LAMP 堆栈
接下来,设置运行的LAMP(Linux、Apache、MySQL和PHP)环境,如果已经有,可以跳过此步骤,或者在 Ubuntu 系统上使用以下命令安装 lamp stack。
$ sudo apt-get install python-software-properties $ sudo add-apt-repository ppa:ondrej/php $ sudo apt-get 更新 $ sudo apt-get install apache2 libapache2-mod-php7.2 mysql-服务器 php7.2 php7.2-xml php7.2-gd php7.2-opcache php7.2-mbstring php7.2-mysql
尽管默认的 Ubuntu 存储库具有PHP,但拥有第三方存储库以进行更频繁的更新始终是一个好主意。如果需要,您可以跳过此步骤并坚持使用 Ubuntu 存储库中的默认 PHP 版本。
在 Ubuntu 上安装 Composer
现在,我们需要使用以下命令安装Composer(PHP 的依赖管理器)来安装所需的Laravel依赖项。
#curl -sS https://getcomposer.org/installer | php # mvcomposer.phar /usr/local/bin/composer # chmod +x /usr/local/bin/composer
在 Ubuntu 上安装 Laravel
安装Composer后,现在您可以从 Apache /var/www目录下的官方 git 存储库下载并安装最新版本的Laravel。
$ cd /var/www $ git 克隆 https://github.com/laravel/laravel.git $ cd /var/www/laravel $ sudo composer install
Once Laravel installation completes, set the appropriate permissions on all files using following commands.
$ chown -R www-data.www-data /var/www/laravel $ chmod -R 755 /var/www/laravel $ chmod -R 777 /var/www/laravel/storage
Setting Up Encryption Key
Now create a environment file for your application, using the sample file provided.
$ cp .env.example .env
Laravel uses an application key to secure user sessions and other encrypted data. So you need to generate and set your application key to a random string using following command.
$ php artisan key:generate
Once the key has been generated, now open the .env
configuration file and update the required values. Also, make sure APP_KEY is correctly set in the configuration file as generated in above command.
APP_NAME=Laravel APP_ENV=local APP_KEY=base64:AFcS6c5rhDl+FeLu5kf2LJKuxGbb6RQ/5gfGTYpoAk= APP_DEBUG=true APP_URL=http://localhost
Create Database for Laravel
You might also needed to create a MySQL database for your Laravel application project using following commands.
$ mysql -u root -p mysql> CREATE DATABASE laravel; mysql> GRANT ALL ON laravel.* to 'laravel'@'localhost' IDENTIFIED BY 'secret_password'; mysql> FLUSH PRIVILEGES; mysql> quit
Now open the .env
configuration file and update the database settings as shown.
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel DB_USERNAME=laravel DB_PASSWORD=secret_password
Configuring Apache for Laravel
Now go to the Apache default virtual host configuration file /etc/apache2/sites-enabled/000-default.conf and update the DocumentRoot to Laravel public directory as shown.
$ nano /etc/apache2/sites-enabled/000-default.conf
Now modify the default virtual host configuration with the following content and also make sure to replace yourdomain.tld with the domain name of your website as shown.
<VirtualHost *:80> ServerName yourdomain.tld ServerAdmin webmaster@localhost DocumentRoot /var/www/laravel/public <Directory /> Options FollowSymLinks AllowOverride None </Directory> <Directory /var/www/laravel> AllowOverride All </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
After making above changes, make sure to reload the Apache configuration changes by restarting service using following command.
$ sudo service apache2 restart
Accessing Laravel Application
Finally access your Laravel application from a browser, using the following URL.
http://yourdomain.tld 或者 http://您的 IP 地址
![](https://www.alaica.com/wp-content/uploads/linux-348.png)
从此时起,您就可以开始使用 Laravel PHP 框架构建强大的应用程序了。对于缓存、数据库和会话等其他配置,您可以访问Laravel 主页。