Home / Blog / Vagrant: A box with Drupal 7 and simpleSAMLphp
Angel's picture
Angel Alvarado R.
CEO and Data engineer.
23 Jan 2017
Vagrant:
A box with Drupal 7 and simpleSAMLphp

Configure SimplesaSAML using Vagrant in a trice.

Body: 

Vagrant box and Drupal 7

The porpuse of this article is to help you to get started with SSO using Drupal 7 and simpleSAMLphp. While developing a custom solution for SSO I found myself reinventing the wheel every now and then; reinstalling all the libraries needed to set up my testing SSO environment.

That's why I decided to create a box with the tools needed to start testing out SSO with SimpleSAMLphp and Drupal. This box assumes you are using Drupal 7, SimpleSAMLphp 1.3 and simplesamlphp_auth or multiple_idp_simplesamlphp.

Taking advantages of tools like Vagrant, Ansible, Phansible, etc. I decided to take some of my favorites articles to automate Drupal development, here. This article assumes you have followed @dev_meshev's instructions to setup a Vagrant machine using Phansible at least through part 2 of the No More Excuses Series. This is her release and here is my release which you can use to follow the following steps.

Vagrant box SimpleSAMLphp

The next step is to set up simpleSAMLphp so Nginx recognizes it. We are using the same Vagrant configuration from part 2 of the No More Excuses Series. So far we have Drupal 7 under the directory www. Go ahead and download simplesamlphp and place it into a folder named simplesamlphp next to the www directory.

You'll have something like this:

tree vagrant

Open build/install.sh and add: sudo ln -sf $base/simplesamlphp /var/simplesaml under the symlink to the settings.php

  1. #!/bin/bash
  2.  
  3. set -e
  4. path=$(dirname "$0") #usually .
  5. base=$(cd $path/.. && pwd) #keep track of root directory
  6. drush="drush $drush_flags -y -r $base/www" #prepare drush command to receive argument and always accept(Y)
  7.  
  8. chmod -R +w $base/www/sites/default #Make sure Drupal does not messes up with our permissions
  9. chmod -R +w $base/cnf # keep our cnf writable
  10.  
  11. echo "Symlink settings.php into our Drupal. "
  12. ln -sf $base/cnf/settings.php $base/www/sites/default/ #from host to guest (vagrant/* folders are already sync by VirtualBox)
  13. sudo ln -sf $base/simplesamlphp /var/simplesaml #from host to guest (vagrant/* folders are already sync by VirtualBox)
  14. echo "Installing Drupal like a boss."
  15. $drush si --site-name=no-excuses --account-pass=admin

The later lets us keep our library "outside" the Vagrant machine (in the shared directory). We will be editing several files in this directory and seems like the easiest solution. I'm sure there are better ways to handle the autoprovisioning of Drupal and SAML(i.e. using Composer), but let's keep it simple.

SimpleSAMLphp Nginx

We have to tell Nginx that 192.168.33.90/simplesaml points to /var/simplesaml/www. To do this, let's edit the ansible role ngix. We will add an alias. Add this to the template default.tpl located in ansible/roles/ngix/templates.

  1. server {
  2. listen 80;
  3.  
  4. root {{ nginx.docroot }};
  5. index index.html index.php;
  6.  
  7. server_name {{ nginx.servername }};
  8.  
  9. location / {
  10. try_files $uri $uri/ /index.php?$query_string;
  11. }
  12.  
  13. location /simplesaml {
  14. alias /var/simplesaml/www;
  15. try_files $uri $uri/ /index.php?$query_string;
  16. location ~ \.php(/|$) {
  17. fastcgi_split_path_info ^(.+?\.php)(/.+)$;
  18. fastcgi_param PATH_INFO $fastcgi_path_info;
  19. fastcgi_pass unix:/var/run/php5-fpm.sock;
  20. fastcgi_index index.php;
  21. include fastcgi_params;
  22. }
  23. }
  24.  
  25. error_page 404 /404.html;
  26.  
  27. error_page 500 502 503 504 /50x.html;
  28. location = /50x.html {
  29. root /usr/share/nginx/www;
  30. }
  31.  
  32. location ~ \.php$ {
  33. fastcgi_split_path_info ^(.+\.php)(/.+)$;
  34. fastcgi_pass unix:/var/run/php5-fpm.sock;
  35. fastcgi_index index.php;
  36. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  37. include fastcgi_params;
  38. }
  39. }

Once your Vagrant machine is running take a look at /etc/nginx/sites-enabled and you'll see the template there.

Memcache

To configure Drupal 7 with simpleSAMLphp you need to store sessions in Memcache or Slq. We'll use Memcache. Add memcached to our Phansible config file all.yml : packages: [git, vim, sendmail, drush, unzip, zip, g++, libssl-dev, apache2-utils, openssl-blacklist, memcached]. And add the memcache and memcached to the PHP packages: [php5-gd, php5-cli, php5-curl, php5-mcrypt, php5-mysql, php5-xdebug, php5-memcached, php5-memcache, php5-dev, php5-ldap, php5-gmp, php5-common, php-pear]

all.yml will look like this:

  1. ---
  2. server:
  3. install: '1'
  4. packages: [git, vim, sendmail, drush, unzip, zip, g++, libssl-dev, apache2-utils, openssl-blacklist, memcached]
  5. timezone: America/Chicago
  6. locale: en_US.UTF-8
  7. vagrant_local:
  8. install: '1'
  9. vm: { base_box: trusty64, hostname: drupal7, ip: 192.168.33.99, memory: '2048', sharedfolder: ./, useVagrantCloud: '1', syncType: nfs }
  10. nginx:
  11. install: '1'
  12. docroot: /vagrant/www
  13. servername: myApp.vb
  14. mariadb:
  15. install: '1'
  16. root_password: drupal7
  17. database: drupal7
  18. user: drupal7
  19. password: drupal7
  20. dump: ''
  21. php:
  22. install: '1'
  23. ppa: php5-5.6
  24. packages: [php5-gd, php5-cli, php5-curl, php5-mcrypt, php5-mysql, php5-xdebug, php5-memcached, php5-memcache, php5-dev, php5-ldap, php5-gmp, php5-common, php-pear]

Default SAML Configuration

In order to use simpleSAMLphp we need to configure the file simplesamlphp/config/config.php. I'll add my config.php file to cnf/. The minimum settings that have to be modified are:

  • 'auth.adminpassword' => '1234',
  • 'secretsalt' => 'defaultsecretsalt2',
  • 'technicalcontact_email' => 'na2@example.org',
  • 'store.type' => 'memcache',
  • 'memcache_store.prefix' => 'd7',

I'll replace simplesaml/config/config.php with cnf/config.php in the provisioning script build/install.sh

  1. #!/bin/bash
  2.  
  3. set -e
  4. path=$(dirname "$0") # .
  5. base=$(cd $path/.. && pwd) #keep track of root directory
  6. drush="drush $drush_flags -y -r $base/www" #prepare drush command to receive argument and always accept(Y)
  7.  
  8. chmod -R +w $base/www/sites/default #Make sure Drupal does not messes up with our permissions
  9. chmod -R +w $base/cnf # keep our cnf writable
  10.  
  11. echo "Symlink settings.php into our Drupal. "
  12. ln -sf $base/cnf/settings.php $base/www/sites/default/ #from host to guest (vagrant/* folders are already sync by VirtualBox)
  13. echo "Installing Drupal like a boss."
  14. $drush si --site-name=no-excuses --account-pass=admin
  15. echo "Configuring simplesamlphp."
  16. cp $base/cnf/config.php $base/simplesamlphp/config/ #Use our settings
  17. sudo ln -sf $base/simplesamlphp /var/simplesaml #from host to guest (vagrant/* folders are already sync by VirtualBox)
  18. echo "Done."

Now you can run the Vagrant machine ($ vagrant up). Wait a few minutes and enjoy a fresh installation of Drupal 7 and SimpleSAMLphp.

You should see something similar to this:

Visit http://192.168.33.99 and http://192.168.33.99/simplesamlphp. You should be all set to start implementing SSO.

Our last step is to install & configure the Drupal simplesamlphp_auth module. From this point you can proceed and install your modules in /vagrant/www/sites/all/modules. However, it is not a good CI approach for our box. To accomplish a good CI you could download Drupal using the provisioning script (or following the no-excuses tutorial).

Simplesamlphp_auth

Based on the Drupal module instructions, at this point we have done the following steps:

Installation Overview

  1. Install SimpleSAMLphp
  2. Configure SimpleSAMLphp as a Service Provider
  3. Install Drupal (if you haven't already)
  4. Install simplesamlphp_auth module
  5. Configure simplesamlphp_auth module
  6. Activate the simplesamlphp_auth module

Don't know how to configure a Service Provider using simpleSAMLphp? Check out this article.

Last but not least, enjoy the release of the Vagrant box in github. If you didn't follow the instructions of this article just download-clone the repo/release and the provising script will download and configure Drupal and simplesamlphp under www and simplesamlphp directories (next to the ansible, cnf and build folders).

TODOs

  • Autoprovision SimpleSAMLphp and Drupal
  • Configure SP
  • Configure IdP
  • Configure https://www.drupal.org/node/2573451

References

https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-virtual-hosts-server-blocks-on-ubuntu-12-04-lts--3

http://stackoverflow.com/questions/21399789/nginx-how-to-create-an-alias-url-route

http://casadelkrogh.dk/code/2014/09/30/embedding-simplesamlphp-using-nginx/