如何在Ubuntu上安装和开始使用Symfony 2


Symfony是一个全栈、开源的PHP框架。它以其独立的组件而闻名,这些组件可以轻松集成到任何其他PHP项目中。Symfony框架适用于构建任何长度的个人主页应用程序,包括仅在命令行运行的控制台程序。本文介绍了如何在Ubuntu上安装和开始使用Symfony 2。

在开始之前,我们需要为命令行环境安装PHP。要安装`php7.0-cli`包,请使用以下命令:

$ sudo apt-get install php7.0-cli

现在你应该已经在你的服务器上安装了PHP。要验证,请使用以下命令:

$ php -v

示例输出应如下所示:

PHP 7.0.4-7ubuntu2 (cli) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies

Symfony要求在你的php.ini文件中设置`date.timezone`选项。要打开php.ini文件,请使用以下命令:

$ sudo nano /etc/php/7.0/cli/php.ini

示例输出应如下所示:

[PHP]

;;;;;;;;;;;;;;;;;;;
; About php.ini ;
;;;;;;;;;;;;;;;;;;;
; PHP's initialization file, generally called php.ini, is responsible for
; configuring many of the aspects of PHP's behavior.

; PHP attempts to find and load this configuration from a number of locations.
; The following is a summary of its search order:
; 1. SAPI module specific location.
; 2. The PHPRC environment variable. (As of PHP 5.2.0)
; 3. A number of predefined registry keys on Windows (As of PHP 5.2.0)
; 4. Current working directory (except CLI)
; 5. The web server's directory (for SAPI modules), or directory of PHP
; (otherwise in Windows)
; 6. The directory from the --with-config-file-path compile time option, or the
; Windows directory (C:\windows or C:\winnt)
; See the PHP docs for more specific information.
.......................................................................................

搜索包含**date.timezone**的行。通过删除行首的**;**号取消注释该指令,并添加适合你的应用程序的时区,如下所示:

[Date]
; Defines the default timezone used by the date functions
; https://php.net/date.timezone
date.timezone = Asia/Kolkata

安装Symfony

要安装Symfony,请使用以下命令,它将下载Symfony安装程序并将其放置在你的`/usr/local/bin`路径下:

$ sudo curl -LsS https://symfony.com.cn/installer -o /usr/local/bin/symfony

现在,你需要使用以下命令使脚本可执行:

$ sudo chmod a+x /usr/local/bin/symfony

要测试Symfony安装程序,请使用以下命令:

$ symfony

示例输出应如下所示:

Symfony Installer (1.5.1)
=========================

This is the official installer to start new projects based on the
Symfony full-stack framework.

To create a new project called blog in the current directory using
the latest stable version of Symfony, execute the following command:

   symfony new blog

Create a project based on the Symfony Long Term Support version (LTS):

   symfony new blog lts

Create a project based on a specific Symfony branch:

   symfony new blog 2.3

Create a project based on a specific Symfony version:

   symfony new blog 2.5.6

Create a demo application to learn how a Symfony application works:

   symfony demo

Updating the Symfony Installer
------------------------------

New versions of the Symfony Installer are released regularly. To update your
installer version, execute the following command:

   symfony self-update

创建新的Symfony项目

要创建一个Symfony项目,请使用以下命令:

$ symfony new tutorialspoint

上面的命令中`tutorialspoint`是项目名称。示例输出应如下所示:

Preparing for project...

✔ Symfony 3.0.6 was successfully installed. Now you can:

   * Change your current directory to /home/linux/tutorialspoint

   * Configure your application in app/config/parameters.yml file.

   * Run your application:
      1. Execute the php bin/console server:run command.
      2. Browse to the https://:8000 URL.

   * Read the documentation at https://symfony.com.cn/doc

要运行应用程序,请使用以下命令:

$sudo php bin/console server:run

示例输出应如下所示:

[OK] Server running on http://127.0.0.1:8000
// Quit the server with CONTROL-C.

现在打开你的Web浏览器并添加以下URL:

http://your_server_ip:8000

示例输出应如下所示:

要退出服务器,请在命令行中使用Ctrl+C。就是这样。通过本文,你将能够理解如何在Ubuntu上安装和开始使用Symfony 2,我们将推出更多基于Linux的技巧和提示。继续关注!

更新于:2020年1月20日

浏览量:155

启动你的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.