Ce blog — désormais archivé — est en lecture seule. Pour continuer à lire mes tribulations, rendez-vous sur le blog d'Akei, ma société.

Prendre un Café

L'espace d'expression de Nicolas Perriault

Aller au contenu | Aller au menu | Aller à la recherche

Keyword - howto

Fil des billets

vendredi 17 juillet 2009

Installing php 5.3, apache2 and MySQL 5 on OS X using Macports

I wanted to test latest php 5.3 on my OSX box. So here’s a quick reminder on how to proceed to a full setup of apache2, mysql5 and php 5.3 using Macports.

First, get a fresh copy of the Macports installer for OSX. Install macports, then in a shell prompt, type:

$ sudo port install mysql5 +server
$ sudo port install php5 +apache2 +debug +pear +sqlite +mysql5 

You have now the time to have lunch, dinner, sex or to watch an episode of Derrick[1].

To enable the php module for apache:

$ cd /opt/local/apache2/modules
$ sudo /opt/local/apache2/bin/apxs -a -e -n "php5" libphp5.so

Then, add this line in the /opt/local/apache2/conf/httpd.conf file:

Include conf/extras-conf/*.conf

Copy one of the standard php.ini files proposed by the default installation:

$ sudo cp /opt/local/etc/php5/php.ini-development /opt/local/etc/php5/php.ini

To start apache automatically at system startup:

$ sudo launchctl load -w /Library/LaunchDaemons/org.macports.apache2.plist

Or manually:

$ sudo /opt/local/apache2/bin/apachectl start

To initialize, configure and start MySQL automatically:

$ sudo -u mysql mysql_install_db5
$ sudo /opt/local/bin/mysql_secure_installation5
$ sudo /opt/local/etc/LaunchDaemons/org.macports.mysql5/mysql5.wrapper start
$ sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist

Now launch your browser at http://localhost/: you’re done. Have some rest.

Notes

[1] Don’t try to do all that stuff at the same time, result cannot be guaranteed

vendredi 8 mai 2009

Installer Redmine sous Ubuntu

Redmine est un gestionnaire de projet technique exploitant Ruby on Rails et très inspiré de Trac, mais qui le dépasse fonctionnellement sur plusieurs points, notamment avec une gestion multiprojets qui fait cruellement défaut à son inspirateur. Voyons comment installer et déployer la dernière version de la branche stable (0.8) sur une Ubuntu Hardy Heron LTS[1].

(Redmine Screenshot)

Toutes les opérations décrites ci-dessous se font en root, mais libre à vous d’utiliser sudo à chaque fois, ou une fois pour toutes en lançant la commande sudo -s.

En préambule, on installe les quelques paquets qui nous seront nécessaires[2] :

# apt-get install build-essential ruby ruby1.8-dev rake libopenssl-ruby \
  libmysql-ruby apache2-threaded-dev subversion git

Maintenant, on récupère les sources de Redmine[3], qu’on va installer dans /opt :

# svn co http://redmine.rubyforge.org/svn/branches/0.8-stable /opt/redmine-0.8
# ln -s /opt/redmine-0.8 /opt/redmine

On va ajouter les sources de la version 2.1.2 de rails[4] dans le répertoire vendor de l’application, afin de cloisonner autant que possible l’environnement qu’elle exploite :

# cd /opt/redmine
# wget -O rails.tar.gz http://github.com/rails/rails/tarball/v2.1.2
# tar xvzf rails.tar.gz && rm rails.tar.gz
# mv railsXXX vendor/rails

Créez maintenant une base de données (MySQL ou autre) dédiée à Redmine. Une bonne pratique est de créer également un utilisateur MySQL aux droits restreints à sa seule exploitation.

Ensuite, éditez le fichier config/database.yml afin de modifier les paramètres de connexion à la base de données précédemment créée.

On installe maintenant une version récente de rubygems, ce qui nous permettra d’installer passenger, une sorte de mod_rails permettant de déployer facilement des applications rails sur un serveur Apache :

# wget http://rubyforge.org/frs/download.php/56228/rubygems-1.3.3.tar.gz
# tar xvzf rubygems-1.3.3.tar.gz && rm rubygems-1.3.3.tar.gz
# ruby setup.rb
# rm -r rubygems-1.3.3

Vous pouvez maintenant lancer la tâche de création des tables :

# rake db:migrate RAILS_ENV="production"

Et remplir la base avec quelques données par défaut :

# rake redmine:load_default_data RAILS_ENV="production"

On installe donc passenger, qui au passage vous posera quelques questions très simples :

# gem install passenger

On installe le module apache pour prendre en charge passenger :

# passenger-install-apache2-module

Pour mapper un domaine - par exemple mon.domaine.tld - vers votre instance Redmine, il faut d’abord créer un VirtualHost apache dédié, par exemple dans le fichier /etc/apache2/sites-available/redmine :

<VirtualHost *>
  SetEnv RAILS_ENV production
  ServerName mon.domaine.tld
  DocumentRoot /opt/redmine/public
</VirtualHost>

Vous noterez que la racine publique du vhost pointe vers le dossier public de l’instance Redmine précédemment installée. N’oubliez pas non plus de déclarer le domaine dans votre fichier /etc/hosts :

127.0.0.1 mon.domaine.tld

On active maintenant le vhost et on relance Apache :

# a2ensite redmine
# /etc/init.d/apache2 restart

Une dernière petite chose, vous devez autoriser Apache à lire et écrire dans certains répertoires de l’instance Redmine, comme suit :

# cd /opt/redmine
# chown -R www-data:www-data files log tmp
# chmod -R ug+rw files log tmp
# chmod -R o-rw files log tmp

Voila, vous devriez avoir une instance totalement fonctionnelle de Redmine, accessible par le domaine que vous avez configuré.

En cadeau Bonux, le support de GMail et TLS pour l’envoi d’emails

Si vous désirez configurer l’envoi des emails en utilisant un (ou votre) compte GMail[5], voici la procédure :

Installez tout d’abord le support de TLS pour ActiveMailer :

# cd /opt/redmine
# ruby script/plugin install git://github.com/collectiveidea/action_mailer_optional_tls.git

Puis éditez le fichier config/email.yml :

production:
  delivery_method: :smtp
  smtp_settings:
    tls: true
    address: "smtp.gmail.com"
    port: '587'
    domain: "smtp.gmail.com"
    authentication: :plain
    user_name: "votreadresse@gmail.com"
    password: "votremotdepasse"

Relancez Apache pour que la modification soit effective.

Edit : Prise en compte de certaines remarques faites en commentaires.

Notes

[1] C’est la version qui propulse mon serveur.

[2] Ce sont les paquets qui m’ont été nécessaires, sur ma machine ; n’hésitez pas à remonter d’éventuels manques en commentaires.

[3] On installe ici la dernière branche stable disponible à l’heure où sont écrites ces lignes, la 0.8.

[4] Oui, il y a plus récent, mais Redmine 0.8 est conçu pour tourner avec cette version : autant limiter les risques d’incompatibilités.

[5] L’intérêt principal à mes yeux reste l’archivage de tous les éléments envoyés.

vendredi 31 octobre 2008

Let's Play with Symfony 1.2 and Doctrine

It’s been quite a long time I didn’t give a go to Doctrine, so as it’s gonna be bundled by default in with the upcoming 1.2 release of symfony, I thought it was a good occasion to play with it.

So let’s checkout the 1.2 SVN branch of symfony and create a test project with a main application[1]:

$ mkdir sf12test && cd sf12test
$ mkdir -p lib/vendor
$ svn co http://svn.symfony-project.com/branches/1.2 lib/vendor/symfony
$ php lib/vendor/symfony/data/bin/symfony generate:project sf12test
$ ln -s ../lib/vendor/symfony/data/web/sf web/sf
$ ./symfony generate:app main

Create a webserver vhost pointing to the web folder of the project directory. I’ve already explained plenty of times how to achieve this step.

Now, let’s enable the sfDoctrinePlugin and disable the Propel one by editing the setup() method of the config/ProjectConfiguration.class.php file:

 php
  public function setup()
  {
    $this->disablePlugins('sfPropelPlugin');
    $this->enablePlugins('sfDoctrinePlugin');
  }

You can list the available tasks running this simple command:

$ ./symfony list doctrine

Managing the Database Schema

First, configure your config/databases.yml file to set the database connection parameters. If you want to quick test Doctrine, use a local SQLite db, like this:

 yaml
all:
  doctrine:
    class:    sfDoctrineDatabase
    param:
      dsn:    sqlite://<?php echo dirname(__FILE__).'/../data/data.db' ?>

We’re going to make a very simple weblog application, so let’s configure our database schema. We can do it in YAML[2], so fire up your favorite editor/IDE and edit a brand new config/doctrine/schema.yml:

 yaml
BlogPost:
  actAs:
    Sluggable:
      fields:       [title]
    Timestampable:
  columns:
    title:          string(255)
    body:           clob
    author:         string(255)

BlogComment:
  actAs:            [Timestampable]
  columns:
    blog_post_id:   integer
    author:         string(255)
    email:          string(255)
    content:        clob
  relations:
    BlogPost:
      class:        BlogPost
      local:        blog_post_id
      foreign:      id
      foreignType:  many
      type:         one

Note that Doctrine offers several pretty cool features including native behaviors (timestampable and slugable are used here).

Now, create a data/fixtures folder and put a data.yml file in, containing some test data in YAML format:

 yaml
BlogPost:
  p1:
    title: My first post
    body: |
      This is cool.
    author: NiKo
    created_at: "<?php echo date('Y-m-d H:i:s', time() - 86400) ?>"
  p2:
    title: My second post
    body: |
      This is still cool.
    author: NiKo
    created_at: "<?php echo date('Y-m-d H:i:s', time() - 7200) ?>"
  p3:
    title: Third post
    body: |
      Is this one cool?
    author: Roger Hanin
    created_at: "<?php echo date('Y-m-d H:i:s') ?>"

BlogComment:
  c1:
    BlogPost: p3
    author: John
    email: john@doe.com
    content: Hey, you're right there.
    created_at: "<?php echo date('Y-m-d H:i:s', time() - 86400) ?>"
  c2:
    BlogPost: p3
    author: Paul
    email: paul@doe.com
    content: Nope, he's not.
    created_at: "<?php echo date('Y-m-d H:i:s') ?>"

Okay, now run the command below to generate the needed files, create the database and fill it with the data fixtures:

$ ./symfony doctrine:build-all-load

We can run several DQL queries in command line to check if everything is fine. DQL is very powerful, and compatible with a lot of RDBMS. You’ll find more information on DQL on the doctrine website.

For example, to find all blog posts:

$ ./symfony doctrine:dql "From BlogPost p"
found 3 results
-
  id: '21'
  title: 'My first post'
  body: "This is cool.\n"
  author: NiKo
  slug: my-first-post
  created_at: '2008-10-29 15:14:25'
  updated_at: '2008-10-30 15:14:25'
-
  id: '22'
  title: 'My second post'
  body: "This is still cool.\n"
  author: NiKo
  slug: my-second-post
  created_at: '2008-10-30 13:14:25'
  updated_at: '2008-10-30 15:14:25'
-
  id: '23'
  title: 'Third post'
  body: "Is this one cool?\n"
  author: 'Roger Hanin'
  slug: third-post
  created_at: '2008-10-30 15:14:25'
  updated_at: '2008-10-30 15:14:25'

Another example, to find informations about the blog post with slug third-post and its associated comments:

$ ./symfony doctrine:dql "Select p.title, p.author, c.author, c.content From BlogPost p, p.BlogComment c Where p.slug = 'third-post' Group by c.id"
found 3 results
-
  id: '23'
  title: 'Third post'
  author: 'Roger Hanin'
  BlogComment: [{ id: '15', author: John, content: 'Hey, you''re right there.' }, { id: '16', author: Paul, content: 'Nope, he''s not.' }]

Put the Query Logic in the Model

The Model part of any MVC architecture must contains the business data and associated logic. In other words, these data and logic should never be handled anywhere else, to decouple your components at max. So we’ll add some query methods in the lib/model/doctrine/BlogPostTable.class.php file, which represents our blog_post table and available operations on it:

 php
<?php
class BlogPostTable extends Doctrine_Table
{
  public function getAll()
  {
    return Doctrine_Query::create()->
      select('p.title, p.slug, p.body, p.author, p.created_at, count(c.id) numcomments')->
      from('BlogPost p, p.BlogComment c')->
      orderBy('p.created_at DESC')->
      groupBy('p.id')->
      execute();
  }

  public function getOneBySlug($slug)
  {
    $posts = Doctrine_Query::create()->
      from('BlogPost p')->
      leftJoin('p.BlogComment c')->
      where('p.slug = ?')->
      orderBy('c.created_at ASC')->
      limit(1)->
      execute(array($slug));

    return isset($posts[0]) ? $posts[0] : null;
  }
}

A Weblog is About Web Interface, uh?

Okay, let’s add pretty controllers and templates to give some life to our blog. First, generate a post module in the main app:

$ ./symfony generate:module main post

Then, edit the apps/main/modules/post/actions/actions.class.php file:

 php
<?php
class postActions extends sfActions
{
  public function executeIndex($request)
  {
    $this->posts = Doctrine::getTable('BlogPost')->getAll();
  }
  
  public function executeShow($request)
  {
    $this->post = Doctrine::getTable('BlogPost')->getOneBySlug($slug = $request->getParameter('slug'));
    $this->forward404Unless($this->post, 'No post with slug=' . $slug);
    $this->comments = $this->post->getBlogComment();
  }
}

We should have display templates too. The first one will show the posts list, in apps/main/modules/post/templates/indexSuccess.php:

 php
<?php foreach ($posts as $post): ?>
  <?php include_partial('post/post', array('post' => $post, 'numComments' => $post->getNumcomments())) ?>
  <hr/>
<?php endforeach; ?>

Note that we must create the _post partial template, in apps/main/modules/post/templates/_post.php:

 php
<h2><?php echo link_to($post->getTitle(), 'post/show?slug='.$post->getSlug()) ?></h2>
<p>
  <small>Posted by <?php echo $post->getAuthor() ?> on <?php echo $post->getCreatedAt() ?>
  <?php if (isset($numComments)): ?>
    - <?php echo $numComments ?> comments
  <?php endif; ?>
  </small>
</p>
<?php echo $post->getBody(ESC_RAW) ?>

The other main template will display one post and its comments, in apps/main/modules/post/templates/showSuccess.php:

 php
<?php include_partial('post/post', array('post' => $post)) ?>

<h2>Comments</h2>
<?php if (!count($comments)): ?>
  <p>No comment yet.</p>
<?php else: ?>
<?php foreach ($comments as $comment): ?>
  <p><small>By <?php echo $comment->getAuthor() ?> on <?php echo $comment->getCreatedAt() ?></small></p>
  <blockquote><?php echo $comment->getContent() ?></blockquote>
<?php endforeach; ?>
<?php endif; ?>

That’s it. A rough but functional weblog if you lauch your browser to yourhost/main_dev.php/post/index:

step2.png

And if you click a post title:

step1.png

Good News, the Forms Framework Works with Doctrine Too

Symfony 1.1 introduced the new forms framework, and good news, Doctrine can take part of it. So maybe you’ve already noticed it, we have form classes generated already, in the lib/form/doctrine folder of the project.

So let’s add a neat commenting system to our blog, by first editing the lib/form/doctrine/BlogCommentForm.class.php file:

 php
<?php
class BlogCommentForm extends BaseBlogCommentForm
{
  public function configure()
  {
    unset($this['id'], $this['created_at'], $this['updated_at']);
    
    $this->widgetSchema['blog_post_id'] = new sfWidgetFormInputHidden();
    
    $this->validatorSchema['author']  = new sfValidatorString(array('min_length' => 3));
    $this->validatorSchema['email']   = new sfValidatorEmail();
    $this->validatorSchema['content'] = new sfValidatorString(array('min_length' => 5));
  }
}

Now, use the form in the executeShow() method of our controller:

 php
<?php
// ...
  public function executeShow($request)
  {
    $this->post = Doctrine::getTable('BlogPost')->getOneBySlug($slug = $request->getParameter('slug'));
    $this->forward404Unless($this->post, 'No post with slug=' . $slug);
    $this->comments = $this->post->getBlogComment();
    
    $comment = new BlogComment();
    $comment->setBlogPost($this->post);
    $this->form = new BlogCommentForm($comment);
    
    if ($request->isMethod('post') && $this->form->bindAndSave($request->getParameter('blog_comment')))
    {
      $this->redirect('post/show?slug='.$this->post->getSlug());
    }
  }

And in the showSuccess.php template, we’ll append the form display:

 php
<h3>Add a comment</h3>

<?php echo $form->renderFormTag(url_for('post/show?slug='.$post->getSlug())) ?>
  <table>
    <?php echo $form ?>
    <tr>
      <td></td><td><input type="submit"/></td>
    </tr>
  </table>
</form>

We’ve now a pretty commeting system added to our blog, thanks to all the goodness provided by symfony and Doctrine:

step3.png

Conclusion

The time when everyone choosed Propel because it was more stable than Doctrine seems to be over. Doctrine is robust, and performs quite well on my box. Furthermore, it handles complex relationships and dynamic object hydratation natively and better than Propel. Doctrine is also very well integrated into symfony, certainly because Jonathan Wage - the Doctrine lead developer - now works for Sensio, creator and main sponsor of symfony.

Notes

[1] Note that Windows users should replace calls to ./symfony by php symfony.

[2] If you hate YAML, you can still write Doctrine table definition classes in raw PHP by hand