如何在Ubuntu12.04VPS上安装和开始使用Phalcon
状态: 已弃用
本文介绍了不再受支持的 Ubuntu 版本。 如果您当前正在运行运行 Ubuntu 12.04 的服务器,我们强烈建议您升级或迁移到受支持的 Ubuntu 版本:
原因: Ubuntu 12.04 已于 2017 年 4 月 28 日终止生命周期 (EOL) and no longer receives security patches or updates. This guide is no longer maintained.
请参阅:
本指南可能仍可用作参考,但可能不适用于其他 Ubuntu 版本。 如果可用,我们强烈建议使用为您正在使用的 Ubuntu 版本编写的指南。 您可以使用页面顶部的搜索功能来查找更新的版本。
关于法尔康
Phalcon 是一个 PHP 框架,它促进了模型-视图-控制器架构,并具有许多类似框架的功能,您可以在这样的软件中使用这些功能 - ORM、模板引擎、路由、缓存等。
关于它的一件很酷的事情是在性能方面,它可以说比其他框架快得多。 原因是它不是您的普通 PHP 框架,您只需将其文件复制到您的服务器上就可以了。 它实际上是用 C 编写的 PHP 扩展。
在本文中,我们将了解如何在运行 Ubuntu 12.04 的 VPS 上开始使用 Phalcon。 如果您继续跟进,我假设您已经使用 LAMP 堆栈(Apache、MySQL 和 PHP)设置了您的服务器。 DigitalOcean 上有一个很好的 教程可以帮助您在需要时进行设置。
安装
您需要做的第一件事是安装 Phalcon 的要求。 运行以下三个命令来做到这一点:
sudo apt-get update sudo apt-get install git-core gcc autoconf make sudo apt-get install php5-dev php5-mysql
You can remove packages from the commands if you already have them installed (for instance 混帐核心 if you already have Git installed). Next up, you need to clone the framework repo onto your system:
git clone git://github.com/phalcon/cphalcon.git
After that is done, navigate in the following folder with this command:
cd cphalcon/build
And run the install file to install the extension:
sudo ./install
What you need to do next is edit your php.ini file:
nano /etc/php5/apache2/php.ini
And add the following line to the end of it:
extension=phalcon.so
Then restart your server for the changes to take effect:
sudo service apache2 restart
And this should do it. To check if Phalcon has been successfully installed, you'll need to check the output of phpinfo() statement. If you don't know how to proceed, create a file called 信息.php somewhere where you can access it from the browser and paste in the following line:
<?php phpinfo(); ?>
Save the file and point your browser to it. In the PHP information displayed on that page, you should see that the Phalcon framework is enabled and also verify its version.
你的第一个 Phalcon 项目结构
如果您使用过其他 PHP 框架,您会期望在项目文件夹结构中的某处有一些与框架相关的文件。 使用 Phalcon,所有这些文件都可以在内存中轻松使用,因此您需要做的就是在 Apache 的文档根目录(默认为 /var/www)的某个位置创建一个空文件夹结构。 推荐的方法如下:
project_name/ app/ controllers/ models/ views/ public/ css/ img/ js/
So what you have here is a project folder which has 2 main folders: 应用程序 and 上市. The first will house the logic of your application (mostly PHP) whereas the second one is where your browser will point and be redirected to the resources in the app folder on the one hand, and have access to all the frontend assets, on the other.
自举
您需要创建的第一个也是最重要的文件是应用程序将用于引导的 index.php 文件。 在应用程序的 public/ 文件夹中创建此文件:
nano /var/www/project_name/public/index.php
And paste in the following code:
<?php try { //Register an autoloader $loader = new \Phalcon\Loader(); $loader->registerDirs(array( '../app/controllers/', '../app/models/' ))->register(); //Create a DI $di = new Phalcon\DI\FactoryDefault(); //Setup the view component $di->set('view', function(){ $view = new \Phalcon\Mvc\View(); $view->setViewsDir('../app/views/'); return $view; }); //Setup a base URI so that all generated URIs include the "tutorial" folder $di->set('url', function(){ $url = new \Phalcon\Mvc\Url(); $url->setBaseUri('/project_name/'); return $url; }); //Handle the request $application = new \Phalcon\Mvc\Application($di); echo $application->handle()->getContent(); } catch(\Phalcon\Exception $e) { echo "PhalconException: ", $e->getMessage(); }
For more information about what this file contains, you can check the official Phalcon website. But please note that you need to replace this line:
$url->setBaseUri('/project_name/');
With one that is appropriate for your case, i.e. containing the name of your project folder.
网址重写
Phalcon 将需要使用 .htaccess 文件来进行一些重要的重新路由并保护应用程序的文件夹结构不被窥探。 为此,需要启用 Apache 的 mod_rewrite 模块,并且需要允许 .htaccess 文件对 Apache 指令进行修改。
因此,如果您不是这种情况,请编辑 Phalcon 应用程序所在的 Apache 虚拟主机文件(默认为 /var/www 如果您此应用程序没有特定的虚拟主机),并确保 /var/www 目录下的 Allow Overrides 设置为 All(同样,如果您的应用程序位于默认的 Apache 文档根目录中)。 您可以使用以下命令编辑默认虚拟主机文件:
nano /etc/apache2/sites-available/default
And where you see this block, make the changes to correspond to the following.
<Directory /var/www/> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory>
Finally, make sure that mod_rewrite is enabled in your Apache. To check if it is already enabled, use the following command:
apache2ctl -M
If you see "rewrite_module" in the list, you are fine. If not, use the following command to enable the module:
a2enmod rewrite
After all these steps, or after any individual one you had to perform, give Apache a restart so they take effect:
sudo service apache2 restart
Now that this is taken care of, create an .htaccess file in the main project folder:
nano /var/www/project_name/.htaccess
And paste in the following directives:
<IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^$ public/ [L] RewriteRule (.*) public/$1 [L] </IfModule>
This will reroute all requests from the project main folder to the public/ folder. Now create another .htaccess file but this time in the public/ folder:
nano /var/www/project_name/public/.htaccess
And paste in the following directives:
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L] </IfModule>
This will reroute all the requests coming to this folder to the index.php file (in case an actual file by the name requested does not exist in that folder or below - you know, for the frontend assets to still be accessible).
你的第一个控制器
如果您现在将浏览器指向项目文件夹,您将收到无法加载索引控制器类的错误。 这是因为默认情况下,如果请求中没有传入控制器,则应用程序需要其中之一。 所以让我们创建一个在页面上显示 Hello World。
在控制器文件夹中创建以下 IndexController.php 文件:
nano /var/www/project_name/app/controllers/IndexController.php
里面,粘贴以下内容:
<?php class IndexController extends \Phalcon\Mvc\Controller { public function indexAction() { echo "<h1>Hello World!</h1>"; } }
You'll notice that we extend the default Phalcon Controller class and name it IndexController (all controller names need to end with the word "Controller". Inside this class, we define a method (all methods, also called Actions, need to end with the word "Action") called indexAction. Since it is called index, it is also the first Action that gets called by this controller if no particular action is specified in the request.
If you now access the project folder from the browser, you should see the string being echoed.
结论
在本教程中,我们已经了解了如何安装 Phalcon 并开始您的项目。 在下一篇中,我们将更进一步,看看我们如何利用 Phalcon MVC 框架以及如何访问存储在数据库中的信息。
文章提交者:Danny