How to use MS SQL Server from PHP 5.x on Linux

Short answer: don’t. It’s a world of pain.

If you really must, keep reading.

Install the required packages

I’ll assume you already have a working webserver and PHP installation, along with the EPEL and Remi repositories.

yum --enablerepo=epel --enablerepo=remi install php-mssql freetds

Make sure the mssql library is loaded

cat /etc/php.d/mssql.ini
; Enable mssql extension module
extension=mssql.so

Configure mssql

In /etc/php.ini make sure these two lines are set like this:

mssql.textlimit = 20971520
mssql.textsize = 20971520

Configure freetds

Edit /etc/freetds.conf to change the text size value and to create a new entry:

[global]
  [...]
  text size = 20971520
[...]
[mssqltest]
  host = 192.168.0.100
  instance = instance_name
  port = 1433
  client charset = UTF-8
  tds version = 8.0
  text size = 20971520

You can omit the instance parameter if you are going to use the default.

Restart Apache and PHP

service httpd configtest && service httpd restart

Test the configuration

Create a simple php script, mssql_test.php:

<?php

$host_db = "mssqltest";  // reference to the name you used in /etc/freetds.conf
$user_db = "your_user";
$pass_db = "your_pass";
$name_db = "your_db_name";

$conn_db = mssql_connect($host_db, $user_db, $pass_db)
  or die("Connection failed to $host_db: " . mssql_get_last_message() . "\n\n");

$dbh     = mssql_select_db($name_db, $conn_db)
  or die("Cannot open DB $name_db: " . mssql_get_last_message() . "\n\n");

echo 'Connected.';

$query_results = mssql_query('SELECT * from YourTable')
  or die ("Cannot run your query: " . mssql_get_last_message() . "\n\n");

$row = mssql_fetch_array($query_results)
  or die ("Cannot fetch row: " . mssql_get_last_message() . "\n\n");

echo "$row\n\n";

mssql_close($link);
?>

References

Advertisements

Ansible: how to skip a specific host or group in a playbook

If you search the Ansible documentation, you’ll probably end up using a when in your playbooks to skip a host.

I found out that it makes more sense to me to skip hosts from the hosts declaration, by prepending them with a !:

---
- name: check on which hosts this would run
  remote_user: root
  hosts: all !bastion
  tasks:
  - name: say hello
    debug:
      msg: "hello {{ ansible_hostname }}"

My hosts file looks like:

[common]
bh[01:02]
ws[01:20]
db[01:03]

[bastion]
bh[01:02]

[ws]
ws[01:20]

[db]
db[01:03]

Running the playbook with the usual commandline:

ansible-playbook -i hosts playbook.yaml

produces the expected result: the debug module is only run on webservers (ws) and databases (db), not on the bastion hosts (bastion).

I hope this will help some other sysadmin out there :)

Firefox Beta PPA on Linux Mint 18.2

I wanted to test out a more recent version of Firefox on Linux Mint 18.2, so I went over to grab the Ubuntu PPA.

I ran the installation commands (as root):

add-apt-repository ppa:mozillateam/firefox-next
apt-get update
apt-get install firefox

But apt wouldn’t update the package, because the default package priorities (check /etc/apt/preferences.d/*.pref) won’t allow the use of Ubuntu packages over Mint packages.

Fair enough, I created a new file /etc/apt/preferences.d/firefox.pref containing:

Package: firefox
Pin: origin ppa.launchpad.net
Pin-Priority: 900

Package: firefox
Pin: release o=Ubuntu
Pin-Priority: 900

And updated to the beta (currently 55.0b12).

Enjoy the 10 fixed bugs and the exciting 127 new bugs to discover!

PHP Sessions on MS Azure Redis service

I sure hope you’ll never end up facing this, but in case you do indeed have a PHP application on MS Azure, and you want to use MS Azure Redis service for the session backend, you’ll have to set your session.save_path to something like:

session.save_path='tcp://UNIQUENAME.redis.cache.windows.net:6379?auth=MSHATESYOU4FUNandPROFIT=&timeout=1&prefix=OHNOMS'

Easy enough, unless your auth key happens to contain a + symbol. In that case, your PHP session creation will fail with this error:

# php count.php
PHP Fatal error:  Uncaught exception 'RedisException' with message 'Failed to AUTH connection' in count.php:3
Stack trace:
#0 count.php(3): session_start()
#1 {main}
  thrown in count.php on line 3
PHP Fatal error:  Uncaught exception 'RedisException' with message 'Failed to AUTH connection' in [no active file]:0
Stack trace:
#0 {main}
  thrown in [no active file] on line 0

From redis-cli the authentication was working fine, so it took us a while to debug. It ended up being a problem with the + symbol, and the the quickest solution in our case was to just regen the auth key so it didn’t have a + in it, but I suspect (but I didn’t test this solution) that URLencoding the + as %2B might work as well.

How to install and use SPICE for VMs in Debian, Ubuntu or Mint

SPICE is a suite of tools for interfacing with desktop-oriented Virtual Machines. I’ve been using it for a couple of years, on Fedora and CentOS systems, mostly for Windows VMs that I required for work.

Until recently, it was fairly complicated to get SPICE to work on Debian-based systems, but I’ve just installed and got it working on Mint. Thankfully, nowadays you don’t need to recompile anything. All the patches and support are included by default, and you need to install these packages:

# apt-get update
# apt-get install virt-manager libvirt-daemon python-spice-client-gtk qemu-kvm-spice virt-viewer spice-vdagent qemu-utils  gir1.2-spice-client-gtk-3.0 gir1.2-spice-client-gtk-2.0 gir1.2-spice-client-glib-2.0

After this, I just created a new VM with virt-manager and it had SPICE enabled by default.

For more information, I recommend checking:

Pacemaker cluster maintenance mode

This is intended as a quick reminder for all the sysadmins out there (me included) that keep forgetting the syntax while working on different environments.

For older clusters you would use:

crm configure property maintenance-mode=true

While for newer cluster you might want to use:

pcs property set maintenance-mode=true

Set the property to “false” to exit maintenance mode.

For reference, check the official Red Hat documentation.

How to start rails or puma (installed with rvm) with systemd

Just a few notes I took about how to start rails server or puma (installed with rvm) with systemd.

I started reading this question on StackOverflow, but it has no answers. Then I found out this gist containing an interesting example: it shows how to create a wrapper with rvm, and then how to use this wrapper to start the server.

I read more in systemd.service documentation and the rails command line documentation and wrote this (gist):

# based on https://gist.github.com/twtw/5494223
# create systemd service file for rails/puma startup
# 0. [if required: rvm use ruby@default]
# 1. rvm wrapper default systemd rails
# 2. put this file in /etc/systemd/system/rails-puma.service
# 3. systemctl enable rails-puma
# 4. systemctl start rails-puma
[Unit]
Description=Rails-Puma Webserver
 
[Service]
Type=simple
User=app
WorkingDirectory=/home/app/your-app
ExecStart=/home/app/.rvm/bin/systemd_rails server -e production
TimeoutSec=15
Restart=always
 
[Install]
WantedBy=multi-user.target

I didn’t get to test it yet, if it works for you let me know :)