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

<span class="warning >

Status: Deprecated

This article covers a version of Ubuntu that is no longer supported. If you are currently operate a server running Ubuntu 12.04, we highly recommend upgrading or migrating to a supported version of Ubuntu:

Reason: Ubuntu 12.04 reached end of life (EOL) on April 28, 2017 and no longer receives security patches or updates. This guide is no longer maintained.

See Instead:
This guide might still be useful as a reference, but may not work on other Ubuntu releases. If available, we strongly recommend using a guide written for the version of Ubuntu you are using. You can use the search functionality at the top of the page to find a more recent version.

Introduction

Symfony is an open source PHP web development framework - a set of tools and methodology to help you build great applications. Some of the traits of this framework are its speed, flexibility, scalability, and stability. You can use it for a full blown web application but also for smaller functionalities needed for your project.

Aside from being a full stack framework, Symfony is a set of components that you can install and use individually. These components are the basis for the framework, but they can also be used decoupled from each other. You can use Composer to install components.

Additionally, Symfony2 comes in the form of distributions. These are packages of bundles, components and configuration that can help you get started very quickly. The Symfony people recommend the Standard Edition as the easiest one to get started for a new project. It contains the most common bundles and has an easy configuration system.

In this tutorial, we will see how to install this distribution and get started using it. For this, it is assumed that you are running your own Ubuntu VPS with sudo privileges and terminal access. Additionally, you need to have a webserver in place, preferably the LAMP stack (Linux, Apache, MySQL and PHP), but you can also configure it with Nginx. You will require PHP 5.3.3 or higher.

Installation

There are 2 main ways you can install this distribution. Either you download and unpack the archive in your project folder or you use Composer. In this tutorial, we will go with the first choice and download Symfony right into our web server’s root folder: /var/www. The reason is that all the files in the archive reside in a folder called Symfony already so no need to create another one. Navigate to /var/www and download the archive:

cd /var/www
wget http://symfony.com/download?v=Symfony_Standard_Vendors_2.3.3.tgz

You get the link to the version you want on the Symfony download page. After the download is complete, you have to untar the archive (it may have downloaded under a weird name, but no matter):

tar -zxvf download?v=Symfony_Standard_Vendors_2.3.3.tgz

Now you should get a new folder called Symfony which holds all the files. You can also delete the tar file from your VPS if you want for cleanliness.

Permissions

There are a couple of files and folders that you will need to adjust permissions for. You’ll need to make these writable by your webserver. If you are using Apache, this user is the www-data from the www-data group. So run the following commands from the terminal (while in the main application folder) to change the ownership of these files and folders:

chown -R root:www-data app/cache
chown -R root:www-data app/logs
chown -R root:www-data app/config/parameters.yml

This makes them (and all inside) owned by the root user and the www-data group. Now let’s make sure that also the group users can write in these folders:

chmod -R 775 app/cache
chmod -R 775 app/logs
chmod -R 775 app/config/parameters.yml

Server Configuration

Next, you should check the server configuration to make sure everything is alright. Symfony2 has a tester available at the following url:

http://www.example.com/Symfony/web/config.php

If you run into the following error: "This script is only accessible from localhost", it means you are trying to access that file remotely. If you are using a DigitalOcean VPS, this will be the case. So let's configure it to let you access it remotely.

Open the config.php file found in the /web folder of your newly downloaded application. Find the following block:

if (!in_array(@$_SERVER['REMOTE_ADDR'], array(
    '127.0.0.1',
    '::1',
))) {

Here you need to add a new array value with the IP you would like Symfony to allow access for. You can use Google to check the IP your are operating from and add that one into the array like so:

if (!in_array(@$_SERVER['REMOTE_ADDR'], array(
    '127.0.0.1',
    '::1',
    'YOUR IP',
))) {

And now you should be able to see the config tester in the browser remotely. Try to follow the instructions provided there to configure your VPS to best be fitted for Symfony. Once you fixed the major problems the tester indicates, you can click on the "Bypass configuration and go to the Welcome page" button to visit your new Symfony2 application home page.

If you get the following error: "You are not allowed to access this file. Check app_dev.php for more information", you will need to open the app_dev.php file in the /web folder of your application and comment out the following block of code:

if (isset($_SERVER['HTTP_CLIENT_IP'])
    || isset($_SERVER['HTTP_X_FORWARDED_FOR'])
    || !in_array(@$_SERVER['REMOTE_ADDR'], array('127.0.0.1', 'fe80::1', '::1'))
) {
    header('HTTP/1.0 403 Forbidden');
    exit('You are not allowed to access this file. Check '.basename(__FILE__).' for more information.');
}

Save the file and exit. Now refresh the page and you should see the welcome page of your Symfony2 application.

Some Global Configuration

On the welcome page of the application we just created, you can click the Configure button to go through a config wizard in the browser. On the first page, you can set up your database connection. There you will notice various drivers you can use: MySQL, SQLite, PosgreSQL, Oracle etc. After you specify the connection information, you can click Next and generate a global secret string for protection against CSRF.

If you click Next after this, the information you provided will be written to the parameters.yml file located in the app/config folder. If you followed the steps earlier in the tutorial and set the correct permissions for this file, you should be fine to continue. If not, you can also copy and paste the code you are given there into the file itself.

Vendors / Dependencies

A Symfony project depends on a few external libraries. You can find these in a folder called /vendor of your project and are downloaded and managed by the PHP dependency manager called Composer. You can consult this tutorial on how to use Composer to find out more information about it.

Note: When you run the php composer.phar install or php composer.phar update commands, Composer will clear the caches and install assets. Additionally, it will change back the permissions to the app/cache folder so make sure you redo the steps from before when you set the proper permissions and made the folder writable by the VPS.

Conclusion

In this tutorial, we’ve seen how to install a distribution of the Symfony2 PHP framework and how to perform some initial configuration on it. In the next tutorial, we will introduce some concepts about how Symfony applications work.

Article Submitted by: Danny
Published At
Categories with 技术
comments powered by Disqus