Blog by Daniel Hauck. Sysadmin, Developer, Blogger.

PHP / Symfony Development Environment with Vagrant and Ansible

What do we want / need?

Before we can dive deep into symfony in general, we need environment to run our code in. Sure we could install PHP directly on our Mac, Linux or Windows. But if you work on a team and everybody would use a different operating system and web server that would suck hard.

We need to build a environment, that’s easy to use, generic enough to extend and production alike, because we don’t want to be a “It works on my machine”-dickhead.

To accomplish this, we will use an virtual machine, that is able to act as a production server, but also easy to install and maintain. And of course it should run on all major operating systems like Mac, Linux and Windows

Vagrant + Ansible = Love

So to build our cool dev virtual machine, we will create a VirtualBox managed by Vagrant. Of course we do not want to install PHP and Nginx everytime we have a new project or decide to recreate our environment. Therefore we will use ansible, to automate this setup.

First we need to install all the stuff to our local machine, so here you can find the installation instructions for every product:

  1. VirtualBox
  2. Vagrant
  3. Ansible
  4. Install vagrant-hostsupdater plugin:vagrant plugin install vagrant-hostsupdater

Creating our Machine

After installing all the tools and verifying the functionality of them, let’s build the machine.

Therefore create a directory for the vm:

mkdir php-symfony-vm
cd php-symfony-vm

You could now use vagrant init to create a Vagrantfile and so on, but since we will delete most of the Vagrantfiles content, we can also create it by hand. Now fire up your favorite text editor, and let’s create the file:

vim Vagrantfile

Now I’ll walk you through the file line by line, or at least the most important parts.
Just look at the comments:

Vagrant.configure(2) do |config|

    config.vm.define "symfony" do |symfony|
        # Base template for virtualbox, we use ubuntu 14.04 here
        symfony.vm.box = "ubuntu/trusty64"
        # Domain on which our application will respond later on
        symfony.vm.hostname  = "symfony.dev"
        # IP address will be used by the VM
        symfony.vm.network :private_network, ip: "192.168.33.151"

        # Tell vagrant to run ansible as a provisioner
        symfony.vm.provision :ansible do |ansible|
            # where the playbook is located
            ansible.playbook = "provisioning/playbook.yml"
        end
    end

    # Access the shared vagrant directory via NFS, otherwise slow on mac and windows
    config.vm.synced_folder ".", "/vagrant", type: "nfs"

    config.vm.provider "virtualbox" do |v|
        # tell virtualbox to give our machine 1 GB RAM and 2 Cores
        v.memory = 1024
        v.cpus = 2
    end
end

If something isn’t clear, please use the comments function, or head over to the vagrant documentation.

I am still using the trusty release, although xenial is the latest LTS, but the vagrant box has a lot of bugs, that make working with it a PITA.

We are almost done, with our basic setup.

Ansible Setup

Now it’s time to create the necessary file structure for ansible to work. We need the provisioning directory, with a playbook.yml inside it and some additional files.

mkdir -p provisioning/{group_vars,roles}

group_vars: Here lives the dynamic part of our configuration. We will set database parameters and additional packages here.
roles: to stick to ansible best practices, we create one role for each component, e.g. one for php, nginx, postgresql and so on

Let’s create our first role in which we can set the locale for our server. Create another dir inside roles/ called general. We will use the general role, for configuring stuff like the language and the charset. Also for some packages, which are not crucial for the machine to act as a development machine, for example htop and vim.

mkdir -p provisioning/roles/general/tasks/ 

Inside the tasks directory, you now need to create a main.yml file. Ansible always looks first for a main.yml inside the tasks/ dir. So if we later decide to split up our configuration into different .yml files, just remember to include them into main.yml as a entry point for ansible.

vim provisioning/roles/general/tasks/main.yml

In this file we start defining our first tasks, which is to generate and set the server locale:

- name: generate the server locale
  locale_gen: name={{ locale }} state=present

Here we see the first dynamic part of our ansible role, the {{ locale }} variable. You can set it dynamically, independent from your role inside the group_vars directory. Therefore let’s create a file called all.

vim provisioning/group_vars/all

This file will be read used for all vagrant machines provisioned with our Vagrantfile. In our case, just this single machine. Don’t care about multimachine setup now.

Now put the locale variable into the all file:

locale: en_US.UTF-8

Ok, so we now generating it, we just need to set it. Therefore we will use another cool feature of ansible, which is lineinfile. Append this to your main.yml inside tasks:

- name: set locale 
  lineinfile: dest=/etc/default/locale regexp='^LANG=' line='LANG={{ locale }}'

- name: reload local
  raw: source /etc/default/locale

And again we can use our locale variable.

Now we got our variables, created our first role. The only thing that’s missing now, is the playbook itself. Just create a file named playbook.yml:

vim provisioning/playbook.yml

with the following content, explaned in the comments:

--- 
# valid for all hosts provisioned inside this vagrant instance
- hosts: all
  # become replaces sudo since ansible v.1.9
  become: true
  # roles to be executed
  roles: 
    - general

Save the file, and now we should be ready, to do our first boot of the machine and check if our provisioning works:

 vagrant up

You will be prompted for the sudo password, since vagrant tries to alter the /etc/hosts and /etc/exports file. After the normal boot of the virtual machine, you can see the start of the provisioning. If not just type:

vagrant provision 

And you should get a very similar output to this:

PLAY [all] *********************************************************************

TASK [setup] *******************************************************************
ok: [symfony]

TASK [general : generate the server locale] ************************************
ok: [symfony]

TASK [general : set locale] ****************************************************
ok: [symfony]

TASK [general : reload local] **************************************************
ok: [symfony]

PLAY RECAP *********************************************************************
symfony                    : ok=4    changed=0    unreachable=0    failed=0

Version our config

That’s nearly it for the first part of this little tutorial. The last thing, I want to do is, version our configuration into a repo, so that we can track changes. Which is quite easy.

Initialize your Git Repo:

git init 

Add a gitignore:

vim .gitignore

Add .vagrant/ to .gitignore:

.vagrant/

Add our files to the index and commit:

git add . 
git commit -m '01 - setup vagrant and ansible, created first role'

That’s it

In the next part, I will show you, how to install PHP, nginx and postgresql in your machine.

If you want to stay up to date, be sure to follow me on twitter, instagram or subscribe to the newsletter!

Non technical posts

1. I am sorry

If you read my blog regularly, I am pretty sure, you have discovered, in the last weeks, months, whatever, there isn’t much content about Linux, Development and so on. I am really sorry for this, am I? Yes, I am.

I have so many topics, that are scripted, but not fully worked out. And I don’t like to put out half-ass stuff.

I am putting a lot of hours and passion into my regular job at the moment, which takes away a lot of time from the ‘blogging’ time. If you’ve ever written tutorials or shoot videos, you know how much time it can take to produce something.

Second point, if you’re not living in the basement and have a neckbeard, you probably have recognized it’s summer. This means I have around 35 – 40 degree in my rooftop home office, which decreases my motivation to sit inside even more.

It’s just simpler and not that time consuming to write about non-technical stuff. I do not have to prepare all the stuff for the rasbperry pi, I do not have to setup VMs. I don’t have to sit in my office, talking into my microphone. Most of this blog I write somewhere, and that’s it. Some have been written on a plane, some laying in the bed. This one I actually write on the balcony drinking a beer.

Don’t get me wrong here. I love writing tutorials, in my opinion, it’s the best way to teach yourself, because you have to be sure about what you’re talking. But at the moment I just enjoy writing more stuff like these.

And your feedback tells me that this is some good alternation.

2. Be prepared for winter

As the big heatwaves are over now (at least I hope so), I’ll probably spend more time in the office again, producing, writing and shooting. So here are just a few things, that you can expect to come in the next view months.

  1. Android Smartwatch Tutorials
    Watchface Design, Setup, Using sensors
  2. Ansible + Vagrant Setup for devs and ops
    Dev Environment Setup, Automation
  3. Using Docker
  4. A few reviews
    The most awesome bag
  5. Something blinking 😉
  6. The Tmux Book (finally)

Also I am working on some little pages, that I will update from time to time on which I list the books I have read, or currently reading. Or even some products I would recommend.

See you on the other side (of this damn hot summer ;))

 

Why I am not using Vim anymore …

… for writing.

HAHA! Click bait.

Now I got your attention or at least you are still on my site. Continue reading, I don’t want to steal much of your time.

Haven’t you

had a few articles about vim for writing? Yes, I have. And I’ll leave them here for ‘historical’ purpose. I’ve written a lot in Vim. I’ve written almost 50 – 60 % of my book about tmux in Vim. And I still have the opinion, Vim is worlds best and most advanced text editor.

And here comes the evil

Editor, the word EDITOR. You can edit texts in vim so efficiently, that it can be toxic. I am using it for 6 – 7 years now. About 5 of them I’d consider myself as a power user. I spent a lot of time, training myself to use Vim. Remember shortcuts, commands, crazy stuff, tinkering with my .vimrc and so on. I do not regret one second of this, because Vim will probably be one of my favourite tools for the rest of my life.

But when it comes to writing, this kind of efficiency, like I mentioned above, can be toxic. Writing is a creative process. And at the time of writing a chapter or a paragraph, you shouldn’t care about typos, missing articles and so on. You can fix that later. Your writing program or auto correction will yell at you early enough (BTW: turn them off or ignore them while typing). Because I am so familiar with Vim, I always jumped back when I saw an error or something and totally lost the flow and blocked creativity.

To cut a long story

If you are writing an article, not a tutorial about programming or so, break out of your comfort zone (what a stupid word) and use another tool. Efficiency can be distracting in some ways. And I learned, when it comes to writing, less is more. Less features, less stuff to think about.

 

 

Stop blocking yourself

When I started my career in the tech sector I was this kind of guy who told you: “Naaaaaaaaaaaaaawh, I can do it (better|faster) on the command line.” – “No, on Linux this works better.” – “I don’t need an IDE.”

This brought me some great experience for using the command line, for scripting, programming, workflow and so on. I learned to master vim, tmux and so on. I converterted all kind of media files on the command line.

I had tons of scripts for all purposes.

Today, I still have share some of this opinions, e.g. writing scripts for repetetive stuff, like converting and so on. But I also gained some experience on the other side. I use IDEs, desktop or web applications for task management and so on.

I am so grown up, haha.

Productivity counts

These were the positive sites of this.

So why am I telling you this? Because I recognized, that I had sabotaged and blocked myself.

In some cases I spent a lot of time, trying to figure out how to do it with X instead of accepting that Y is already the perfect solution for this. Didn’t get it? Ok.

For example:

In my former job and at private project I spend a lot of time with databases. I always interacted on it directly via command line clients. That ain’t bad at all. So I could remember complex queries better. But when it comes to complex table layout, visualization it a nice thing. Dammit, I could write a script to generate an image file for me… And the stress begins. Hours, Days, Weeks, and than you lose the focus on the point you wanted to accomplish. Instead of just using a tool that already exists I tried to re-invent the wheel, only because I wanted to it via command line.

You won’t believe how often I made this mistake, instead focusing on the problem. When I now take a look into my backups and open up my workspace I will find dozens of little applications I’ve written, that I really never used. Because at the time of finishing, I recognized they are useless. And I would have done better in checking out a existing tool or library instead of solving a problem that has been solved multiple times before.

So don’t do it like I did. If there’s a tool that let’s you do your work faster and more efficient dont’ block yourself with technology racism. Use it. You are no command line activist 😉

Framework First Programmers or … I don’t know

Back in my days…

Ok, stop that. I am 23. I cannot say back in my days.

I started my career as a sysadmin. But I could never really decide. I also learned to program in PHP, Python and a few other languages. It has great advantages. I could face a problem from both sides and from multiple perspectives of multiple programming languages and so on. So yeah, I could never really decide, what I want to be. Developer, Sysadmin, Architecture Dude, DBA. So I live in multiple worlds 😉 Not all the time but sometimes. Ok, back to the topic now.

When I learned all programming languages or sysadmin stuff, I always learned the plain form first. I avoided frameworks, IDEs, wrappers and anything else. I wanted to learn things at their core to get a deep understanding of what really happens. I don’t wanted some framework or wrapper to catch the error and hide it from me with a non-saying message, which won’t help me on my path, at least for the beginner time. So I learned python and wrote any code in vim, without code completion or any fancy things. Same thing to PHP, Java, C and so on. So I got a solid understanding of these topics and developed techniques for debugging and finding errors. Of course, now I also use some IDEs or tools that help me or speed up my work. But whenever I want to learn something new, I begin with the absolute minimum tool stack.

What about the title?

And now we face the other side. In the past years I met a lot of people that are also into programming or other tech topics. I met people like me. That prefer the ‘hard way’ of learning. But also other guys, that for example have a minimum knowledge of Python, and then they start developing Django applications. Of course, after a while, you get good at it, at least at Django. But if you don’t advance your general programming skills in the language of your choice you tend to think inside the framework. And that’s toxic.

Devs like this often miss the basics of the programming language. When you just spend a day or two on learning python and then jump directly into Django development, the chances are big; you missed some core principles of Python.

Imagine

You got an error. The error is not Django related. There’s not post about it on StackOverflow.

What do you do?

In situations like this, it pays, to take the ‘hard way’ of learning a programming language.

If you can’t figure out what’s happening, here’s how I deal with situations like this:

Take the piece of code out of your view, model, whatever. Don’t use an IDE. Take vim, emacs or even nano. Get whatever you need, like credentials or keys and put them in this single file. Throw out as many dependencies as possible until you have the minimal amount of code needed to get this working. Now you can work through the code line by line and figure out what it does, without any framework overhead and gain some understanding what’s happening.

Plain vs. Framework

Shouldn’t I learn using frameworks like sf2, Django or so? You should. In most cases companies search for “{LANGUAGE}/{FRAMEWORK} Developer” and not for “{LANGUAGE} Developer”. So it’s important to know frameworks and their principles like ORMs or Routing and so on.

But it’s also important to sometimes go back to the core of the programming language and figure out how stuff works without a framework. It is important to know the core concepts behind the language that you use on a daily base. From time to time you will also discover new stuff you haven’t heard before. And even more important, it will give you skills, that you can use in your framework. Why use an extra app if you can solve it in a single line?

print(“Hello %s\n” % argv[1])

Take action and do some basic stuff. Close your IDE, open up a text editor and do some basic stuff, hack around, write a little command line application with the language of your choice.

And don’t forget to tell me your opinion about this topic in the comments below. Thanks for reading 🙂

 

 

If you got nothing to say … or the tone is in your fingers

I often get asked, which theme or plugins I use on my blog. What do you use to write? How much to spend on a theme. How to this, how to that. But is this crucial?

In short

If you got nothing to say or write about, there is no magic WordPress or other blog setup that will help you produce content rapidly without sitting on your ass and start typing. As guitarists say ‘the tone is in your fingers’.

Throw money at your blog

You can spend hundreds of dollar each year for your blog. Subscriptions for plugins, themes, little helpers and so on. If you are a professional writer, which I am far away from, that can make sense. If you make a living from writing and publish multiple articles a day or a week, if it saves you let’s say 20 % of the time for writing, this totally makes sense. But if you just start out, or just doing this for fun or to give something back, you should focus elsewhere.

Don’t waste your time and creativity

When I started blogging I spend (days|weeks|months) searching for ‘the perfect theme’.  Instead of putting my ass on the chair and write articles I searched around and installed hundreds of themes. Now I know this is wasted time.

So when you just start your blog, don’t waste your time searching for a theme that is perfect. Take a simple one. That hasn’t too many options. Focus on producing content. Make a list of articles you want to write. When you got enough articles, you will know, what’s missing in your blogging system.

Maybe you’re having a lot of photos in your posts, then get a photo gallery plugin.

Maybe you have a lot of code samples in your posts, get a code highlighter.

Maybe you produce a video for every post, get a featured video plugin.

And so on.

Endurance is necessary

Many starters (I did it myself) happily start their blog and write their first article and don’t write anything for months, or even a year. That’s what you need to think about. If someone is visiting your site and recognizes there has been no post for a month or a year and you shout at him to follow you on twitter, how big is the chance he will do this?

So, set up a schedule and stick to it. For example, as a beginner commit to at least one article each two weeks. Make an appointment in your calendar, every Sunday or so to write an article.

You will get better and faster at writing so that you could increase your schedule to one blog post each week. Do you not know what to write about? Earlier in the post, I told you to make a list of articles you could write about. Write down each idea that comes to your mind. You can always delete them later. Use a piece a paper, a note app on you phone, trello or whatever. But don’t miss an idea.

By the way, that’s what I will do from now on. I had a schedule for two articles each month. But now, as I have canceled other projects, I have the time and more important the will to put more and effort to this blog.

Publish early and often

You may already know this term if you are working in the tech field.

Don’t write a blog post and leave it on draft for days or even weeks. Whenever it is ready, hit the publish button! Chances are good, that if it is in the draft for more than two days, it will never get published. Believe me.

If you just started, you may be scared to post your article and share it on social networks. Maybe someone will be offended by your writing. Maybe someone will criticize you. Don’t think about this. Especially when you are blogging in the technology field, there’s always some knowing it better and has a significant need to tell you what dumbass you are and so on. I got several comments on G+ and Hackernews, and so on that told me I would get fired with this kind of mindset, and I am an idiot and so on. But I am still employed, and I still publish blog posts, and I am still confident. So don’t be afraid to post and don’t let them talk you down.

Let’s end here

This post should just be about, that you shouldn’t overstrain yourself with tools, plugins, and all this stuff. But at the time of writing so many things came to my mind, that I put at least a part of it in here. If you are just starting out, I hope I could help you at least a bit. If you like this topic and want me to share more about it, give me some feedback in the comments, and I will write some more about this.

 

Docker – Installation on Ubuntu/Debian

Getting the latest shit

As a ubuntu user you are probably used to just add a ppa to get the latest version of whatever on you machine. At the moment of writing this post, sadly there is no ppa. So we have to go the old school way of importing keyrings and stuff. The good thing about this is, that this also works on debian for example.

Prepare your setup

First ensure you have apt-transport-https and ca-certificates installed:

sudo apt-get install apt-transport-https ca-certificates

Now you can add the key for docker.io:

sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D

Now we need to add the docker repo to our apt-repositories:

sudo sh -c 'echo "deb https://apt.dockerproject.org/repo ubuntu-$(lsb_release -sc) main" > /etc/apt/sources.list.d/docker.list'

After adding the repo you need to update your apt-cache:

sudo apt-get update

We’re almost there 😉 After everything worked we can now install docker itself 🙂 Therefore just use apt again and after that start your docker service:

sudo apt-get install docker-engine
sudo service docker start

And you’re done. You can now verify your installation with:

sudo docker run hello-world

 

Docker – Not only for Hipsters

Not only a hype

Although you are sure to get new followers on social networks when using the docker hashtag (yes, we tried) it’s not only a hype. At least containers, not especially docker only.

But docker is the most used and most famous container solution at the moment. Big companies like spotify use docker in production at the moment and the number of adopters keeps growing.

I first used docker when it was in beta and liked the idea from beginning. At that time I hadn’t the chance to use it in production. I try to implement it as a vagrant replacement. Because of performance, no hypervisor overhead and so on. There had been a lot of bugs at that time. So after while I stopped using it, cannot exactly name a reason. Too much work and less time I guess 😉 It was in the winter I was moving I think.

Another attempt

Now docker seams pretty stable and has built a huge environment around it. swarm, compose, machine, just to name a few.  As I am in charge of IT at a tech startup I thought about taking a look at docker, first for development, maybe later for production. Until now we have built a vagrant machine with ansible for every project we had. It works the most time, but as everywhere there are some issues at which docker could help. Also we are going the way to service based architecture, where docker also could work great. I’ll give it a try. And here on my blog and on my youtube-channel, I’ll keep you up to date.

 

Do I need a smartwatch? – Moto 360 2nd Generation

 The Reason

Before going on vacation I decided to dive into android wear programming and ordered a smartwatch, a moto 360. I decided to take it and use it on the trip and see if it can assist me or make life easier in some ways. If you are just asking yourself if you definitely need one, I can tell you. No.

First impressions

As I mentioned I want to dive into android wearable development. So the first task would be to get a feeling, how it works and what I can do with this. So the setup and configuration are straightforward:

  • Install Android wear app on Android (My Nexus already had detected the watch, because I head Bluetooth turned on)
  • Pair the watch to the phone
  • Update..
  • Update….
  • Update………….

Of course, after the thousands of updates, the battery was very low. This will not be the usual battery lifetime. To support the battery lifetime I first switched off the so-called ‘Always on’ display, so I could get the most out of the battery. I don’t bother if someone else than me can take a look at the time walking beside me, so why should I waste the battery? 😉 And if you raise your arm in the natural position of looking at a normal watch the display turns on. That really works well.

Also, the leather wristband feels comfortable and the watch is not too heavy. And more important, not too thick.

Daily Usage

The first thing you will recognize are the general notifications like they appear on your smartphone. Some apps already have built-in smartwatch functionality. Some apps can even replace existing functionality like Trello can replace the ‘Take Note’ functionality. So what else is possible?

I always carry a little pedometer with me, which is very accurate, and try to reach at least 10.000 steps each day. As smartwatch are praised for their fitness … I don’t know compatibility? … I thought I’d check this out next. Walking on the street and running at the gym. To my surprise, it worked great. Just to let you know, I tested Google Fit and Moto Body. Both were quite accurate.

At the office, it becomes quite handy for appointments. I know your smartphone also reminds you, but again, you do not have to pull it out.

Also for the tech part, I discovered great features. I use two-factor authentication where I can. But sometimes it’s quit annoying to pull out the phone all the time to check the code. With a smartwatch on your wrist, you can check the latest authentication code just on your wrist, which is quite handy when you have to type it in. You don’t have to unlock your phone all the time.

But ain’t that a security hole? Yes and no. If you don’t lock the watch when you take it off, it is. But if you enable the automatic lock function when you take it off you have to draw a pattern before you can use it.

Next thing I recognized is, my phone battery lasts longer when I wear the watch. Wait, how can that be? It uses the Bluetooth connection all the time. Simple. When I wear the watch and get a notification I just take a look at the watch, and do not pull out the phone, unlock it, drag down the notification bar, read the notification, lock it and pull it back in.

Another cool thing is controlling music. Or in my case, audio books. When I am at the gym, driving, or doing other mindless tasks I like to listen to audiobooks. Same thing as above, I do not have to pull out the phone all the time when I want to control audible. The same works for every music program such as prime music or similar.

I bet you know this situation. You are at home, work, hotel or elsewhere and don’t know where you left your phone… In the fridge (yes, it happened), in some drawers, bag, jacket. And there’s another handy feature of the watch, the ‘find your phone’ menu point. You click it and in about 5 seconds your phone starts ringing at full volume, whether it is in ‘Do not disturb’ or on vibration.

Holiday Usage

As I mentioned we traveled to Stockholm for a few days. A nice city, but I have never been there before, so I don’t know anything there.

So the first thing that came in quite handy was the connection to google maps or the navigation. I could either tell the watch “Take me to Hotel XYZ” or type it one my phone. And the cool thing is, that you can just look at the watch where you have to go. It vibrates if you need to change direction. This was a very nice feature.

Also, if you want to what the weather will be like, you can, for example, use weather.com’s app and you got the weather on your wrist.

Another cool feature is the flashlight. Don’t expect too much, but it’s powerful enough to light up a sign on a darker bus station in the night.

 

 

Still in progress

As this is a post based on an ongoing experience, I will update this post occasionally. Stay tuned and subscribe to the newsletter!