Symfony安裝及使用


 

安裝Symfony,使用

brew install homebrew/php/symfony-installer

開始一直下載不了包,我手動瀏覽器下載了幾個,發現好像都是Permission問題,運行了下面的命令:

sudo chown -R $(whoami) /Users/baidu/Library/Caches/Homebrew/

 

Symfony安裝在:

$ which symfony

/usr/local/bin/symfony

 

在以下目錄:

/Users/baidu/Documents/Data/Work/Installed/symfony

運行

symfony new mywebsite 2.8

一直超時。

所以就自己到Symfony下了個demo,解壓到html根目錄,然后運行

http://127.0.0.1:7080/symfony-demo/web/config.php

發現兩個major問題:

  1. Vendor libraries must be installed

    Vendor libraries are missing. Install composer following instructions from http://getcomposer.org/. Then run "php composer.phar install" to install them.

  2. date.timezone setting must be set

    Set the "date.timezone" setting in php.ini* (like Europe/Paris).

發現還是有很多問題。

連上公司網絡,然后重新進行命令:

symfony new mywebsite 2.8 -vvv

初步看來可以運行。

安裝成功會顯示:

✔  Symfony 2.8.12 was successfully installed. Now you can:

    * Change your current directory to /Users/baidu/Documents/Data/Work/Installed/symfony/mywebsite

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

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

    * Read the documentation at http://symfony.com/doc

 

來到mywebsite目錄下面,跑命令 

 php app/console server:run  開始會出現以下錯誤:
[Symfony\Component\Debug\Exception\ContextErrorException]                                                
  Warning: date_default_timezone_get():

需要 sudo vi /private/etc/php.ini (沒有的話,從php.ini.template拷貝過來)

date.timezone = Asia/Shanghai

然后運行成功:

[OK] Server running on http://127.0.0.1:8000    

 頁面展現:

 
Welcome to
Symfony 2.8.12

Your application is now ready. You can start working on it at: /Users/baidu/Documents/Data/Work/Installed/symfony/mywebsite/

What's next?

Read the documentation to learn
How to create your first page in Symfony

 

下一步是要把Nginx和Symfony結合起來。 

nginx的配置文件 /usr/local/etc/nginx/nginx.conf

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    include servers/*;
}

 

Symfony的配置文件/usr/local/etc/nginx/servers/mywebsite.conf 

log_format logformat '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

server {
    listen       7090;
    server_name  localhost;
    index app.php;
    root /Users/baidu/Documents/Data/Work/Installed/symfony/mywebsite/web;

    location / {
        if (!-e $request_filename){
            rewrite ^/(.+)$ /app.php/$1 last;
        }
        client_max_body_size 20M;
    }
    location ~ ^/(app|app_dev)\.php(/|$) {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_param  HTTPS              off;
        client_max_body_size 20M;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires 1h;
        client_max_body_size 20M;
    }

    access_log /Users/baidu/Documents/Data/Work/Installed/symfony/mywebsite/log/mywebsite.log logformat;
    error_log /Users/baidu/Documents/Data/Work/Installed/symfony/mywebsite/log/mywebsite.error_log;
}

 

以上全部完成后,運行命令:

$ brew services restart nginx

得到輸出:

Stopping `nginx`... (might take a while)
==> Successfully stopped `nginx` (label: homebrew.mxcl.nginx)
==> Successfully started `nginx` (label: homebrew.mxcl.nginx)

 

但是瀏覽器頁面出現如下錯誤:

Warning: date_default_timezone_get(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /Users/baidu/Documents/Data/Work/Installed/symfony/mywebsite/app/cache/prod/classes.php on line 6526

開始試了,是不是要改php7的配置 

vi /usr/local/etc/php/7.0/php.ini 

date.timezone = "Asia/Shanghai"

發現還是報錯,看了一下,需要改兩個地方:

一是php的配置文件 

vi /etc/php.ini

date.timezone = "Asia/Shanghai"

二是報錯的代碼中加上:

vi /Users/baidu/Documents/Data/Work/Installed/symfony/mywebsite/app/cache/prod/classes.php

6526行 加上 date_default_timezone_set('Asia/Shanghai');:

if (!static::$timezone) {
date_default_timezone_set('Asia/Shanghai');
static::$timezone = new \DateTimeZone(date_default_timezone_get() ?:'UTC');
}

然后就不報錯了。

瀏覽器頁面正常顯示(Nginx):

http://127.0.0.1:8080/

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

(Symfony): (注意,Symfony自己運行的時候用的是8000端口,而跟Nginx結合用的是7090端口)

http://localhost:7090/

Welcome to
Symfony 2.8.12

Your application is now ready. You can start working on it at: /Users/baidu/Documents/Data/Work/Installed/symfony/mywebsite/

What's next?

Read the documentation to learn
How to create your first page in Symfony

 

Symfony還有一個debug模式,訪問的url是:

http://localhost:7080/app_dev.php/ (區別在於:1. 錯誤提示到頁面,2無緩存)

開始訪問的時候,還是報timezone的錯,

然后根據提示,在 

vi /Users/baidu/Documents/Data/Work/Installed/symfony/mywebsite/app/cache/dev/classes.php 6008行加上:

date_default_timezone_set('Asia/Shanghai');

就不報錯了,頁面顯示:

Welcome to
Symfony 2.8.12

Your application is now ready. You can start working on it at: /Users/baidu/Documents/Data/Work/Installed/symfony/mywebsite/

What's next?

Read the documentation to learn
How to create your first page in Symfony

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM