如何在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.tw/date.timezone
date.timezone = Asia/Kolkata

安裝Symfony

要安裝Symfony,請使用以下命令,它將下載Symfony安裝程式並將其放置在你的`/usr/local/bin`路徑下:

$ sudo curl -LsS http://symfony.com/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 http://symfony.com/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

啟動你的職業生涯

完成課程獲得認證

開始學習
廣告