Enable sudo without password in Ubuntu/Debian

You probably know that in Ubuntu/Debian, you should not run as the root user, but should use the sudo command instead to run commands that require root privileges. However, it can also be inconvenient to have to enter your password every time that you use sudo. Here’s a quick fix that removes the requirement to enter you password for sudo.

  1. Open the /etc/sudoers file (as root, of course!) by running:
    sudo visudo
    

    You should never edit /etc/sudoers with a regular text editor, such as Vim or nano, because they do not validate the syntax like the visudo editor.

  2. At the end of the /etc/sudoers file add this line:
    username     ALL=(ALL) NOPASSWD:ALL
    

    Replace username with your account username, of course. Save the file and exit with <ESC>wq. If you have any sort of syntax problem, visudo will warn you and you can abort the change or open the file for editing again.

    It is important to add this line at the end of the file, so that the other permissions do not override this directive, since they are processed in order.

  3. Finally, open a new terminal window and run a command that requires root privileges, such as sudo apt-get update. You should not be prompted for your password!

That’s it! Enjoy your new freedom! 🙂

Creating and configuring a new MySQL database via the command line

When developing almost any application, you will need to have database of some type to store the data created and used by the application and its users. The most popular open-source database is MySQL. Of course, there are other well-known open-source database platforms, such as PostgreSQL and SQLite, as well as so-called NoSQL databases like MongoDB, since MySQL is ubiquitous, we’ll work with it.

Just as there are many different database platforms, there are many ways to interact with MySQL. There are web-based tools, like phpMyAdmin and Adminer, and regular client applications, such as DBeaver and SQuirrel SQL, all of which are free and open-source. I use and can recommend all of the tools mentioned. However, since you may not always have access to these tools, it’s always good to know how to use the default command-line/shell tools for MySQL.

To log into MySQL at the command line, enter this command:

mysql -uusername -ppassword

Note that there is no space between the -u and -p and the username and the password, respectively. (Technically, you can put a space between -u and username, but I find it difficult to remember which one you can and which one you can’t, so I just do them both the same way!) Assuming that you enter a valid username and password, you will be logged in and at the MySQL command prompt: mysql>.

All MySQL commands end with ; (semi-colon). To try things out, let’s look at the databases that you already have:

mysql> SHOW DATABASES;

Running this command should return something like this:

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.40 sec)

Every MySQL database server will have an information_schema and a mysql database. These databases hold metadata about the configuration of the MySQL instance itself. While you won’t often use these databases, you might want to know a little about them.

Let’s create a new database and grant privileges (permissions) to that database to a specific (new) user. I prefer to create a user with the same name as the database itself, but you can use any username that you like. For this example, we’ll the username will be different from the database name to avoid confusion. Of course, to maintain security, you should usually not use the MySQL root user for access to application databases.

mysql> CREATE DATABASE `laravel` CHARACTER SET utf8 COLLATE utf8_general_ci;
mysql> CREATE USER 'laravel_user'@'%' IDENTIFIED BY 's3cr3t';
mysql> GRANT ALL ON `laravel.*` TO 'laravel_user'@'%';

After each command above, you should get a response message of Query OK followed by the number of rows affected (usually 0 or 1 for DDL commands like these). Also, if you get the message ERROR 1046 (3D000): No database selected when running the GRANT command, this means that MySQL doesn’t know what database you are referring to. You can execute USE `laravel`; to select the laravel database as the default.

Let’s look at the details of each of these commands in turn. The CREATE DATABASE command does just that: it creates a new database with the given name. Note that we included the name (laravel) in a pair of back ticks (`). While this isn’t absolutely necessary in most cases, it’s good practice since it allows you to use non-ASCII characters in the name. Likewise, we specified the optional CHARACTER SET and COLLATE parameters. By using UTF8 character set, this ensures that our database can store non-ASCII characters properly. Essentially, we are making our database more flexible.

Next, we turn to the CREATE USER command. Obviously, this creates a new user in our database platform. However, initially, this user (laravel_user) can’t actually do anything (other than log into MySQL). The username should be contained in single quotes (') [not back ticks!). Also, you are probably wondering about the @'%' following the username. This defines the scope of accessiblity for this user and % essentially means that this user can connect to the database from any host (machine). If you want to restrict connections to only the host where MySQL is running, such as in a development environment, you can specify @'localhost' (or @'127.0.0.1') instead of @'%'. Finally, the IDENTIFIED BY clause specifies the password (called authentication_string in MySQL terminology) for the user. In this case, we simply specify the actual password (s3cr3t) enclosed in single quotes. MySQL hashes passwords so that they are not stored in plain text in the database. You can see the hashed value for this user (laravel) by running:

mysql> select authentication_string from mysql.user where user = 'laravel_user';

You should get a result similar to this:

+-------------------------------------------+
| authentication_string                     |
+-------------------------------------------+
| *58C036CDA51D8E8BBBBF2F9EA5ABF111ADA444F0 |
+-------------------------------------------+
1 row in set (0.08 sec)

Obviously, the actual value will (or should!) be different on your system.

Finally, we come to the GRANT command, which explicitly gives priveliges (permissions) to a user for certain databases or objects in a database. In this case the ALL specification means that all privileges (permissions) available are being given (“granted”) to the specified user. If you want to grant only certain permissions, such as to allow “read-only” access, you can specify any list of the other privileges, such as SELECT, CREATE, UPDATE, and DELETE. (See MySQL documentation on GRANT for full list.) The specification after the ON indicates the scope of the privileges that are granted. In this case, by specifying `laravel.*`, we are granting these permissions to all objects (not only tables) in the laravel database. If you want to limit permissions to only a specific table, you could specify that, such as `laravel.user`. And, last but not least, the TO clause indicates which user (or users) these permissions should be given to. Again, the format of the user follows that of the format for CREATE USER. You can specify multiple usernames by separating each with a comma (,).

To log out of the MySQL command prompt, run:

mysql> exit;

Now, we have our database created and ready to use. In Laravel, you need to update the .env file in the root directory of your project with the appropriate parameter values to match your new database. Using the database above (and assuming that MySQL server is running on the same machine as our Laravel project), our settings would be:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laravel_user
DB_PASSWORD=s3cr3t

Hope you enjoyed this article. Please provide feedback/questions in the comments.

Install PHP 5.6 (or 5.5) in Ubuntu 16.04 LTS (Xenial Xerus)

Ubuntu has released the latest LTS (Long-Term Support) version 16.04 (Xenial Xerus) of their distribution. The official Ubuntu repositories include only PHP 7.0, since it is the official version (at the time of the 16.04 release). However, many developers still use PHP 5.5 or 5.6 in their production environments, so developing on these versions is frequently necessary. As usual, the PPAs come to our rescue for installing PHP 5.5 or 5.6 on Ubuntu 16.04. Here’s how.

First, we add the PHP 5 PPA maintained by Ondrej Sury and update the repository data on your Ubuntu installation:

sudo apt-add-repository -y ppa:ondrej/php
sudo apt-get -y update

If you are familiar with these repositories, you’ll notice that the PHP version is not included in the PPA repository name. For Ubuntu 16.04, both PHP 5.5 and 5.6 are included in the PPA. (If you need PHP 5.4 or earlier, you’ll need to stay with Ubuntu 14.04 LTS Trusty Tahr.)

Now, we can install both PHP 5.6 (or 5.5) and 7.0, along with support for Apache 2 and MySQL, including various PHP modules, such as those required for Laravel framework:

sudo apt-get -y install php7.0 php5.6-mysql php5.6-cli php5.6-curl php5.6-json php5.6-sqlite3 php5.6-mcrypt php5.6-curl php-xdebug php5.6-mbstring libapache2-mod-php5.6 libapache2-mod-php7.0 mysql-server-5.7 apache2

As with any LAMP installation on Ubuntu, you’ll be prompted for the administrator (root) password for MySQL. Also, note that the package for XDebug (php-xdebug) does not include a version, since it supports both PHP 5.6 (or 5.5) and 7.0. Most other packages are PHP version-specific. If you aren’t sure, just enter part of the name on the apt-get command line and press <Tab> to see the various options.

Once we have PHP 5.6 (or 5.5) and 7.0 installed, we can easily switch between them from the shell using these commands. To enable PHP 5.6 (and disable PHP 7.0) use this command:

sudo a2dismod php7.0 ; sudo a2enmod php5.6 ; sudo service apache2 restart ; echo 1 | sudo update-alternatives --config php

Similarly, to switch from PHP 5.6 to PHP 7.0, use this command:

sudo a2dismod php5.6 ; sudo a2enmod php7.0 ; sudo service apache2 restart ; echo 2 | sudo update-alternatives --config php

Or, better yet, why not set up a couple of simple Bash aliases in your .bashrc to take care of this for you:

alias phpv5='sudo a2dismod php7.0 ; sudo a2enmod php5.6 ; sudo service apache2 restart ; echo 1 | sudo update-alternatives --config php'
alias phpv7='sudo a2dismod php5.6 ; sudo a2enmod php7.0 ; sudo service apache2 restart ; echo 2 | sudo update-alternatives --config php'

Now, you can just run phpv5 or phpv7 to switch to PHP 5.6 or PHP 7.0, respectively.

In the commands above to switch/re-start PHP versions, the update-alternatives command is used switch the version of PHP that is used when running PHP from the shell/command line. This is especially important when using PHP command-line tools, such as Composer and the Laravel Artisan commands.

Update

If you run into problems starting Apache 2 web server (sudo service apache2 restart) with the error “Apache is running a threaded MPM, but your PHP Module is not compiled to be threadsafe. You need to recompile PHP.”, then run the following commands in Ubuntu:

sudo a2dismod mpm_event
sudo a2enmod mpm_prefork
sudo service apache2 restart

This should work for both PHP 5.6 and PHP 7.0; therefore, you should not need to run this again when switching between PHP versions.

Let me know if you have any questions in the comments below.

Make your .bash_profile aliases available anywhere

Bash aliases are one of the truly great timesavers when working in Unix/Linux. I’ve picked up quite a few aliases from others on my travels around the Internet and many have become second nature. Moreover, in quite a few cases, I’ve (almost) forgotten the original command!

Lately, I’ve done a lot of development work using Vagrant and, honestly, I’d never want to go back to trying to set up development environments on individual machines, since Vagrant makes standardization so simple. At the same time, even though my Vagrant box is pretty well standardized, everyone probably has their on bash aliases that they like to use. So, here’s how to have the best of both worlds!

What we want to do is provide a simple way to grab our bash aliases from Github and apply them from most any machine.

  1. Put your bash aliases (or any other bash settings!) into a file a file named .bash_alias and add this file to your Github account. The conventional practice is to put these in a repository named dotfiles.
  2. Determine the “raw git” URL to your new .bash_alias on Github. Essentially, this URL starts with https://raw.githubusercontent.com/ and then follows the standard pattern for Github URLs of your username, repository name, branch, and file name. For example, my .bash_aliases “raw” URL is https://raw.githubusercontent.com/TimothyDJones/dotfiles/master/.bash_aliases.
  3. Now, here’s where all of the magic happens! When you want to apply your aliases on a machine that you are logged into, such as a Vagrant box, just run this command at the shell:
    curl https://raw.githubusercontent.com/TimothyDJones/dotfiles/master/.bash_aliases -o /tmp/.bash_aliases; source /tmp/.bash_aliases
    

    Viola! All of your aliases are now available on this machine. And, of course, you can add this command to your .bash_profile on that machine, so that it will run automatically everytime you log in.

Note: When dealing with your Unix/Linux configuration files (“dotfiles”), it is very important to pay attention to the line endings of your files. Windows and Unix/Linux use different conventions. The best way to account for this is to set the autocrlf parameter in the dotfile repository on your workstation by running:

git config core.autocrlf true