Get to know the hosts file

Find your hosts file

In windows your hosts file is located:

C:\WINDOWS\system32\drivers\etc\hosts

For windows you will need to open an editor as administrator to edit the file. Also you may need to disable your anti virus as it may prevent you from editing your hosts file. Just remember to enable the anti virus again as soon as you are done.

In linux your hosts file is located:

/etc/hosts

For linux you will need to use sudo to edit the hosts file.

Experiment with the hosts file

Add at the bottom of your hosts file:

127.0.0.1 moo
127.0.0.1 baa
127.0.0.1 moobaa

In your command line you can now ping these:

ping moo
ping baa
ping moobaa

Each one will show something similar to:

Reply from 127.0.0.1: bytes=32 time<1ms TTL=128

Then remove the lines we just added to the hosts file, and ping them again.

You will receive an error message similar to:

Ping request could not find host baa. Please check the name and try again.

Practice some more

Lets practice using an existing IP like google. First lets find google’s IP:

ping google.com

The result will return 216.58.223.36.

In your hosts file add this line at the bottom:

216.58.223.36 googlefun

Now ping googlefun:

ping googlefun

You will see that you have now pinged the google IP address.

What can you do with the hosts file?

You can point a domain name to an ip. You may want to do to block your computer from communicating with a certain domain name or just have custom domain names set up for fun.

Connect via ssh

Open ssh client then insert command:

ssh username@hostname

Trouble shooting ssh connections

If you’re struggling to connect to ssh, ping the domain name or ip address:

ping [domain/ip]

IP Addresses preset in the hosts file

In some cases you may set the hosts file for a domain name and IP address. If your ssh connects to an incorrect domain/ip, look at your .hosts file and check if you are pointing your ip and domain names correctly.

Server out of memory

I recently dealt with a server that stopped adding to its logs. It was as if the logs were frozen. After a lot of investigating it was found that the server had run out of memory.

Troubleshooting

Check your server space with the following command:

df -h

Investigate where the main memory is being used. This is very often in the /var/log/ folder.

If you would like to see the sizes of the files in a folder use this command:

ls -l  [folder]

eg: ls -l /var/log

If you would like to the see the sizes of folders:

du [folder]

eg: du /var/log

Clear up space by deleting files and folders

To delete a file use the following command:

sudo rm [filename]

To recreate a blank file use the following command:

sudo touch [filename]

Unable to find what is using server space

If you try to delete a file that is still in use by a service, the file will not delete off the server, but also not be displayed in the folder structure.

To investigate if there are any deleted files still in memory use this command:

lsof | grep ‘(deleted)’

You will see a list of files still in use with an id.

Use the following command to “kill” the file using the indicated id value:

kill -9 [id]

Plan your code

I have a mantra at this current point in my life:

If you need documentation to explain your code, your code is too complicated, but if you can’t create the documentation with ease, your logic is too complicated.

I have been reading “Introduction to systems analysis and design, an agile iterative approach”. And it has reinforced that mantra in the way I approach my development.

Planning is a very important part of development. I encourage all developers to pause and take some time to plan your code before you actually put any code to screen.

Also take the time to learn how other developers plan their work. Many use UML as a solution for planning their projects.

When you plan you allow for a big picture approach. Think about the code as a piece to a whole.

The logic should feel simple by the time you start adding code.

Documentation is good to have, especially if you need another developer to take over a project. It lessens the bus factor. I find documentation has become less as a means to explain code, and more of a means to understand the logic of the whole project.

The zen of python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren’t special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one– and preferably only one –obvious way to do it.
Although that way may not be obvious at first unless you’re Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it’s a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea — let’s do more of those!

View your command history

Forgot a useful command you used recently?

View your history of commands you have typed in linux by just typing in “history” in the command line tool:

history

Scaffold a laravel api

This is a quick scaffold solution for setting up a simple laravel api.

This tutorial does not include more advanced api requirements such as user authentication or a database connection. It also does not indicate how to send data through the api.

Prerequisites

You must have php installed on your system and have composer set up.

You will need to use the command line, so some command line knowledge is required.

Add laravel

Use composer to add the laravel installer through the command tool

composer global require “laravel/installer=~1.1”

See the documentation here

In your command line tool, navigate to the folder you would like to place the laravel project and insert the below

laravel new api

The last word, “api”, can be whatever you want to project to be named.

A folder will have been created called api and in that folder will be a fresh install of laravel.

Check your laravel installed successfully by running the following statement in your command line tool:

php artisan serve

That should open a website with the address: http://localhost:8000

If you do not see that, or can not access the laravel site on that url, you will need to check that all the prerequisites are installed properly before continuing.

Adding the api end points

To create your api end points you will need to edit the routes.php files. This file will be in app/Http/

Add these lines of code:

Route::get('/test/', array('uses' => 'APIController@doThisTest'));

Route::get('/anothertest/', array('uses' => 'APIController@doAnotherTest'));

In the code this part array(‘uses’ => ‘APIController@doThisTest’) indicates that it will use a controller called APIController and use a method called doThisTest.

You now need to create a controller called APIController.

In app/Http/Controllers/ create a file called APIController.php and add the following code:

<?php

namespace App\Http\Controllers;

class APIController extends Controller {

    public function doThisTest() {
      return "Hello world";
    }

    public function doAnotherTest() {
      return "Hello moo";
    }

}

Viewing the api

This code will have created two endpoints:

  • http://localhost:8000/test/
  • http://localhost:8000/anothertest/

 

Summary

That’s the start. You can extend the api further by adding user authentication and create an interface for inputting data through the api.