How to use the new Vagrant on Windows Subsystem for Linux on Windows 10

I'm writing this so I won't forget it later.

1 - First, install VirtualBox and enable the Windows Subsystem for Linux

2 - Add the following line to your PATH variable on Windows (Control Panel -> System -> Environment Variables) so that the command VBoxManage.exe becomes accessible to the command-line:

C:\Program Files\Oracle\VirtualBox

3 - Now go into WSL and install ssh and rsync if you don't already have it.

4 - Download Vagrant for Linux on the Vagrant website.

5 - Now do the dpkg -i vagrant.deb thing to install it.

6 - Add the following thing to your ~/.bashrc

export VAGRANT_WSL_ENABLE_WINDOWS_ACCESS="1"

7 - Logout, login again and vagrant should now work from your WSL Bash!

XRandR extend desktop

This command was a lifesaver

xrandr --auto --output DVI-0 --mode 1920x1080 --left-of VGA-0

Of course change monitors and resolutions accordingly.

How to mount an ISO file on Linux?

Your wish is my command!

mount -t iso9660 /dev/sr0 /media/cdrom

Building a simple Debian package

First, create a directory for your program which will imitate the filesystem structure, for example here I'm making a package for a shell script called myscript, so my directories will have the following structure:

 

MyProgram/usr

MyProgram/usr/bin

MyProgram/usr/bin/myscript

 

If you need to put a file in /etc or to put a file in /var/cache you create these directories in MyProgram, you get the idea.

 

For creating the package, we will need a directory named DEBIAN (all caps) in the root of MyProgram. This directory will contain the Debian package related files, the most important is the control file, create it:

 

Package: MyProgram

Version: 1.0

Architecture: all

Maintainer: Your name <your@email.com>

Depends: ruby2.1, python3, bash

Installed-Size: in_kb

Homepage: http://www.gtbono.com

Description: Short description of the package

 Here goes the full description of the file, which is idented by one space.

 

So the directory structure becomes like this:

 

MyProgram/usr

MyProgram/usr/bin

MyProgram/usr/bin/myscript

MyProgram/DEBIAN

MyProgram/DEBIAN/control

 

To build the package, we use dpkg -b MyProgram to build it, and dpkg -i mypackage.deb to install it.

Linux bash: Making a for loop in one line

I'm writing this so I won't forget later.

Here's how to write a bash loop inline so you can use in the command line:

for i in {1..20}; do 'touch file$i' ;done

Where touch file is my command and $i is the counter variable.

Rails, Passenger and Apache in production - Secret key

I've run into an issue where my Rails application was put into production with an Apache + Phusion Passenger, which is an awesome setup, and I've had more success with it than Unicorn + Nginx but I've run into a strange issue.

I didn't know about Rails secret_key, So I didn't knew how to put my app into production mode. It was running in development mode when it should be production (feels weird? it definitely is, it exposes data that shouldn't be there in production).

Here's what I did.

I had an environment called production in my Rails configuration (I believe it comes by default, so it should be the same for you).

When I put my app into production, I needed that Apache knew beforehand what my secret key was, so I had to generate them by going into my applications directory and typing in the following command:

RAILS_ENV=production rake secret

This will give you a key that should be put into Apache config (because you're running Passenger through Apache, duh) so edit the file /etc/apache2/envvars and add the following line:

export SECRET_KEY_BASE=yourkeyhere

Restart Apache and you're good to go, no more 500 Internal server errors and stuff, just don't forget to add the following directive to your app VirtualHost config:

RailsEnv production

This worked for my app, if you have any questions feel free to ask me on Twitter.

@ sign before function in PHP

When a function starts with an at sign (@) in PHP, it means that all errors and notices that this function could raise will be silenced, they won't be put on screen or server logs. The at sign is also called the Error Control Operator.

Example:
    @include("example.php");

See also:
    http://php.net/manual/en/language.operators.errorcontrol.php

Overriding a PHP configuration file from code with php_ini function

string init_set(string $varname, string $newvalue)

This function sets a default value for a configuration option in PHP.

This function enables you to override php.ini configuration options from your script. The configuration option will keep the value you set until the script stops executing, and will restore it's value when the script stops executing.

When you are on a shared server, or don't have root access for modifying php.ini configuration options, this can be really useful.

Example:
    <?php init_set('register_globals', 0); ?>

See also:
    http://php.net/manual/en/function.ini-set.php

Managing systemd targets in Linux

I'm writing it down so I won't forget about it myself.

Systemd is now the default init system in many Linux distributions.

In systemd, the concept of runlevels that sysV had is now replaced by targets.

The ones you need to know are:

For command-line interface:
runlevel3 in sysV became multi-user.target in systemd

For graphical interface:
runlevel5 in sysV became graphical.target in systemd

Here's the most commonly used commands:

To see what target is loaded by default when you boot the system:

systemctl get-default

To list all currently loaded targets, type the command:

systemctl list-units --type target

You can use the following command to show even the target units who are not loaded:

systemctl list-units --type target --all

To change the target which the system loads at boot time:

systemctl set-default name.target

To change the target without reboot, use the commands:

systemctl isolate multi-user.target
systemctl isolate graphical.target

How to reset Bash on Ubuntu on Windows

I've been a Mac user for five years now, but I have to admit that Bash on Ubuntu on Windows have gotten my attention.

If you broke your Bash on Ubuntu on Windows (formely called Windows Linux Subsystem) you can reset it by reinstalling the environment. Use the following command on Windows Command Prompt:

C:\WINDOWS\System32\LxRun.exe /uninstall

It will prompt you to type "y" so it can continue uninstalling. When it finishes, use the following command to install it:

C:\WINDOWS\System32\LxRun.exe /install

It will again prompt you to type "y" so it can install, when it finishes, it will prompt you to type a UNIX password, which will be the password you use for sudo in Ubuntu.

After you type the password, look for the "Bash on Ubuntu on Windows" on your start menu and start having fun again! :)

How to completely delete a user account in Linux

You can use the following command for deleting the user with name "gtbono":

userdel gtbono

But note that this will stil let the user's home folder intact, you can delete it by using the command:

rm -rf /home/gtbono

Or you can do both commands for deleting the user and the home folder at once using the command:

userdel -r gtbono

What exactly is LAMP?

If you are just starting out web programming, you may have already heard the term LAMP before, in this post I'll explain it to you.

 

LAMP is an acronym for Linux, Apache, MySQL and PHP.

 

The P may refer to Python or Perl too, but 99% of the time it is referring to PHP.

Some guys refer the M to MariaDB, which is essentially a MySQL fork.

 

LAMP is a technology stack generally used for web development.

 

Linux is the operating system, which will be installed on the server, something like Ubuntu or Debian which will server as basis for installing the other things.

 

Apache is the web server, it is one of the most famous and widely-used web servers which will serve our web pages.

 

MySQL is the database server, which will store the data that will be used on the application.

 

PHP is the programming language that will glue all pieces together. PHP is a powerful language and really shines in this environment. He is the star of the show that most people will spend their time in with a LAMP stack.

 

Of course, all of these components are made to be used with web pages, so HTML/CSS and JavaScript come together in this stack.

 

Hope it helps anyone, thanks!

Giovanni Tempobono

Variables in PHP

A variable is one of the most common concepts in programming. A variable is a symbolic name which refers to a location in memory. Sounds complicated? it really isn't! here's an example of why you might use variables:

 

My sister is called Joana.

Joana is a really nice girl.

Yesterday I bought a guitar for Joana.

Joana likes to play it all day long.

 

In this simple text, we have the word Joana called out four times. Imagine if you suddenly realize that your sister's name is Jenna, you would have to change it in four places:

 

My sister is called Jenna.

Jenna is a really nice girl.

Yesterday I bought a guitar for Jenna.

Jenna likes to play it all day long.

 

Phew! I was lucky that this text was small! imagine if it was bible-sized? My fingers would hate me, luckily, we can put variables here to make our lifes better.

 

Imagine that you have a little empty box called "name", you would call name like this:

 

$name

 

But $name is empty! So, we need to give it a value:

 

$name = "Joana"

 

So now, $name is a placeholder for the word "Joana"! In PHP, it needs to be inside quotes because it is a string, which is really just a bunch of characters together.

 

With the variable set, we can code our previous text like this:

 

My sister is called $name.

$name is a really nice girl.

Yesterday I bought a guitar for $name.

$name likes to play it all day long.

 

When we need to change the girl's name, we just assign another value to the variable, like this:

 

$name = "Jenna"

 

And with this, we just replace the name in one place and PHP will recognize that reference and point it to the value we just assigned. Variables are really useful and are used all the time in programming, hope this article helped you with something.

 

See you guys later!

Giovanni Tempobono

The state of tags in PHP

In PHP7, one of the changes that were made were to do with the opening and closing tags.

You can use these ones without modifying PHP and they are available on every system:

 

<?php ?>

<?= ?>

 

The last one is a short tag, it abbreviates <?php echo ?> so both of these things will give you the same result

 

<?= $variable ?>

<?php echo $variable ?>

 

There's another option, in php.ini (PHP's configuration file), you have an option to enable short tags. If you put this into your php.ini file:

 

short_open_tag=On

 

You will be able to open and close PHP with the following tag, it will be equivalent to <?php ?>:

 

<? ?>

 

Although there's an advantage to using short tags, I wouldn't recommend it, I would stick to the two ones that I can use without having to modify any PHP configuration.

 

There's a common practice too, If you have a file that's pure PHP with no HTML or whatever. You can skip the PHP closing tag, which is better for security reasons.

 

That's it for today!

Giovanni Tempobono

Which Linux distribution would you recommend for a beginner?

When it comes to Linux, there's something that scares the hell out of anyone wanting to get a taste of it.

You can't exactly "download linux", Linux is only an operating system kernel, the central, but still only a part of an operating system.

So, for anyone wanting to use Linux, you will have to download a distribution (sometimes called just "distro"). which is an awesome all-in-one package, containing the Linux kernel, together with a desktop environment and an set of programs, ready to be used.

All right! I think I'm ready to use some Linux! but hey... I've seen that I have something like hundreds of Linux distributions to choose from, what do I do?

I'll explain it to you, quick and easy.

There are hundreds of Linux distributions out there, but really, the list of distros which matter is something MUCH smaller, in fact, there are only two principal types of distros that you will need to choose from:

The ones that use DEB packages.
And the ones that use RPM packages.

For a desktop user, this brings down your choice to either choose:

Ubuntu, a distribution that uses DEB packages.
Fedora, a distributions that uses RPM packages.

You can't go wrong with these, Ubuntu and Fedora are awesome and will rock out your desktop machine (servers are another story...).

I recommend you to start with Ubuntu (personal preference) and try out Fedora later, I had both experiences with both of them.

If my advice was useful, send me a high-five on Twitter!

Giovanni Tempobono

Hello, new blog! (again)

Hello and welcome to the new iteration of gtbono.com!

I've wanted to join the cool people's party so I decided to build my own blogging engine from scratch, yay!

This one is built with Ruby on Rails, served by Phusion Passenger through Apache in a Debian box. I just couldn't help but nerd out about my new server setup, please bear with me as I am insanely happy. 

The blog engine I've built is called Bloguinho, and it is going to be open-sourced really really soon, I'll keep you guys updated.

See you guys later!