Learning Ubuntu A Beginners Guide To Using Linux - Nathan Neil
Learning Ubuntu A Beginners Guide To Using Linux - Nathan Neil
UBUNTU
A B EGINNERS G UIDE TO U SING L INUX
N ATHAN J AMES N EIL
Copyright © 2016
See More Books by Nathan Neil:
[Link]
CHAPTER 1
WHAT IS LINUX & UBUNTU?
You may be familiar with Microsoft Window’s and Apple’s MacOSX. Both of these are
examples of operating systems.
Merriam-Webster defines operating system as the main program in a computer that
controls the way the computer works and makes it possible for other programs to function.
Essentially, the operating system is the program that lets you interact with a computer, and
the computer to understand what you want to do. It goes deeper than that, but I want to
keep this topical.
Linux is an open-source operating system. Being open-source is a major difference
between Linux and the other popular operating systems. Open-source means that the
software is provided with the original source code and is made freely available to be
redistributed and modified. This is what makes Linux unique. It has a strong community
out there modifying the code, participating in forums, and fixing bugs much quicker than
the alternatives.
If you are looking to add a new skill to your portfolio, learning how to use Linux, can be
very useful. According to ZDNet in 2014, 87% of enterprise businesses added at least one
Linux server and 82% planned on adding more in 2015. Linux and its distributions are
very popular for database, web, and other types of servers.
A distro or distribution is a version of Linux that uses the Linux kernel as its foundation.
There are hundreds of Linux distros, with most all being free. Ubuntu is a distro of Linux,
which is distributed by a company called Canonical. They provide the operating system
for free, but charge for commercial support of the product. Nearly a quarter of servers
running Linux, are using Ubuntu. This makes it a great choice for your first Linux
experience.
WHAT DO I NEED TO GET STARTED?
The tools that you will need, vary on the route that you would like to go. There are four
methods, that you could use to experiment with the lessons in this book. One of them, I
prefer over the other two options, especially if you are still new to the process. Let’s skim
over the first three and focus on the forth, which is my preferred method for those of you
reading this book.
The first three methods, would require you to manually setup the installation of Ubuntu
and configure it. There are a lot of tutorials on YouTube that you can reference if you
want to install it your self, but I will caution you that it might not always go the way you
anticipated, especially if you are new to the field.
Before we talk about these methods to get started, lets first go over the version of Ubuntu
that we will be using for this book. This book, while it can apply to other versions of
Ubuntu, is best suited for Ubuntu Server 14.04 LTS. LTS stands for Long Term Support.
That means that it has support from Canonical for updates and patches for five years.
Many people do not realize that an operating system’s support expires.
Once the support expires, there may not be security patches and other upgrades for it
available.
In my book, Securing Business Data: Establishing a Core Value for Data Security, we talk
about operating system support more in depth. If you work with technology or manage a
business, I highly recommend that you read it.
Ubuntu 15.10 is also out now, but it is not an LTS version. I prefer to work with versions
that have long term support.
Ubuntu 14.04 LTS is certified for AWS, Microsoft Azure, Joyent, IBM, and the HP
Cloud. It is also supported by nearly all VPS providers. VPS stands for Virtual Private
Server. A Virtual Private Server is a machine sold as a hosting service and is often more
affordable than purchasing a server and the bandwidth that it requires. With the popularity
of cloud based services, many businesses now run more virtual private servers in order to
conserve costs and scale more quickly. With a VPS you pay for what you need to use and
can add more resources overtime, if you find you need more processing power or memory.
The one thing that I like about Ubuntu 14.04 LTS is that it has updates to popular
packages, many of which we will go over in this book. Ubuntu 14.04 LTS can also be
installed on most devices, supporting: x86, x86-64, ARM v7, ARM64, and Power. If you
are not sure what those things mean that is fine, but you can be confident in knowing that
Ubuntu supports most devices, which is a huge advantage.
For the first three methods to setup Ubuntu on your own, you will need to download the
iso file. An iso is the file extension used for files that can be burned to a CD or DVD,
depending on the size. Essentially it is an image of the disc that you will need to install
Ubuntu.
You can download Ubuntu server by going to:
[Link]
Once you download the iso file, you will need to burn it to a disc. There are a variety of
iso burning tools. In the past, I have used Ashampoo Burning Studio Free, with success.
Method 1: Install Ubuntu on a Spare Machine
If you have a spare computer that you can use to run Ubuntu, that could be the best way to
learn how to install and set it up. Since it’s a spare machine, there is low risk if you make
an error in the installation. What you will need is the spare computer, along with the disc
that we burned the Ubuntu image to.
Make sure that the computer is setup to boot from the disc drive and from there you can
boot to the installation disc and install Ubuntu. The install is pretty easy, but there is
always the possibility of running into a snag. I have found that the Ubuntu forums and
YouTube has great resources for troubleshooting common problems.
Once the installation is complete, remove the disc, and the system will boot into Ubuntu.
Method 2: Install Ubuntu using Virtualization Software
If you do not have a spare machine, but have a fair amount of memory on your computer,
you can install it using virtualization software. A few examples of this software include
the free VirtualBox by Oracle, and the paid version of VMWare.
This method is good for a beginner as it lowers the risk of making an error that may
accidentally wipe your machine.
I have used VirtualBox, which is from Oracle and free, but it can be glitchy from time to
time. This is an option, but not my preferred choice for beginners reading this book.
To move forward with this and use the free tools, you will need to download and install
VirtualBox, which can be found at the following link:
[Link]
You also can configure the virtual machine to boot from the iso file for Ubuntu, rather than
burning it to a disc.
Method 3: Dual Boot your Computer
To be honest, I did not want to list this as an option. It is a popular one amoung those who
are skilled and know how to properly set up a machine to run multiple operating systems,
but if you are not familiar with this, I would not explore this option.
This one is the most risky, as you could accidentally break the boot cycle of the machine
or overwrite your existing operating system if you are not careful.
*Preferred Choice for this Book*
Method 4: Use a Low Cost VPS
For this book and the nature of it, targeting to educate beginners, I recommend using a
VPS. It is more affordable than buying a spare computer if you do not have one, more
stable than using virtualization software, and no risk to breaking your primary computer as
there is with a dual boot. The main thing that I love about using a VPS is that you can
access it from anywhere, since it is hosted in the cloud.
Another advantage is that the VPS provider will install the operating system for you. We
will still need to install packages and learn how to use it, but you can setup and deploy an
Ubuntu Server very quickly this way.
For creating the tutorials in this book, I will be using DigitalOcean. They have a starter
package for $5 a month, including: 1TB transfer, 512 memory, 1 CPU, and 20 GB of
storage. With DigitalOcean, you can get started as quick as 55 seconds.
Another option that I would recommend is Linode. Linode’s starter plan has double the
resources, but it is also $10 per month. Since we are just learning and not deploying
anything, the starter package from DigitalOcean seems the most practical for our
purposes.
There are a lot of other VPS providers out there so feel free to do your research, but I have
found these two have the best support and pricing.
With using this option, we do not have to download or burn the Ubuntu iso and we also do
not need to do anything special to our computer. Setup is quick and relatively easy. All
you need is a credit card and you can cancel at anytime.
If you decide to use methods one, two, or three, your training will start after the next
chapter. The next chapter, which is a short one, will walk you through setting up a VPS
on DigitalOcean.
QUICK RECAP
1) An operating system is the main program in a computer, which controls
the way the computer works, making it possible for other programs to function.
1. Microsoft Windows, Mac OSX, and Linux are examples of
operating systems.
2) Open-source means that the software is provided with the original source
code and is made freely available to be redistributed and modified.
3) A distro or distribution is a version of Linux that uses the Linux kernel as
its foundation.
4) A Virtual Private Server is a machine sold as a hosting service and is
often more affordable than purchasing a server and the bandwidth that it
requires
5) The recommend method for you to experiment with Ubuntu, is by setting
up a VPS.
1. This books instructions focus on using DigitalOcean as your host,
but you can choose other providers as well.
CHAPTER 7
SETTING UP A WEB SERVER
APACHE2
A common configuration for Linux servers is to set them up to be web servers. 80% of
servers that host websites are running Linux. To establish a good foundation, learning
how to install these features are important.
To get started in setting this server up as a web server, we need to install Apache2.
Apache2 is the most commonly used web server on Linux systems. Web servers are used
to serve up web pages requested by client computers. To run website software like
WordPress we will need more packages than this, but installing Apache2 is the best
starting place.
Installing Apache2
Now if we open our web browser and type in our IP address, we will see the Apache2
landing page.
Command cd /var/www/html; ls
If you want to, you can open the default [Link] file in nano.
Note: To save and make changes, you may need to open the file using sudo.
Now let’s change back to our home directory by using cd.
INSTALLING PHP5
PHP is a popular web server-side scripting language. It is very popular and used by most
content management systems, such as WordPress.
Installing PHP5
INSTALLING MYSQL
Along with Apache2 and PHP, your server will need MySQL for many web applications
that need to store data. This includes the widely popular WordPress CMS.
Installing MySQL Server
Once you run this command, you will need to enter your password in addition to agreeing
to continue as we did before. After that is done the install will prompt you for a password
for the ‘root’ user. It is highly recommended you use a different password for MySQL.
Enter your password, then tab to ok and press enter. After that you will need to reenter the
password again.
Now, we need to install another package to allow PHP to talk with MySQL.
Next we need to install a package to allow apache2 to talk and authenticate to MySQL.
To do so, use the following command:
sudo apt-get install libapache2-mod-auth-mysql
In some circumstances you may use contact forms on your site for people to reach you. In
order to send mail from the server we need to install another package: postfix.
INSTALLING POSTFIX
Postfix is a mail server that started out of IBM research labs as an alternative to
Sendmail. Postfix aims to be fast, easy to use (great for beginners), and secure. This is
my recommendation if you want to be able to have mail services running on your server.
The installation is easy.
Installing Postfix
After you approve the install, you will see the window below:
Tap to hit enter on the OK button and then select Internet Site in the next prompt. Once
that is done press tab to ok and then enter. The next step asks for your fully qualified
domain (FQDN). If you do not have one yet just tab to ok and use the default.
Now it is installed! We won’t cover the other various configuration settings in this book,
but you can find various helpful resources online.
INSTALLING PHPMYADMIN
A great way to test to see if we configured all of this properly, is by installing
phpmyadmin. Phpmyadmin is a great tool to provide a web interface for managing your
MySQL server, its tables and databases.
Installing phpmyadmin
During the phpmyadmin installation, it will prompt you to
select which web server you would like to configure. Next to
apache2, press the spacebar, then tab to ok, and press enter.
Shortly after, the installation will ask you if you want to
configure database for phpmyadmin with dbconfig-common.
Make sure that yes is selected and press enter. Afterwards you
will be prompted to enter the password for the administrative
user. Complete the prompts and continue.
Now that phpmyadmin is installed, we can see if it works by
going to [Link] and entering our credentials.
If your installation was successful, you should see a screen like the one below:
We will get back to more of this later, but there are a few more things that we should learn
first.
By now we know some of the basics of installing, updating, navigating, and setting up a
VPS.
Next we will take a look at understanding, Linux file and directory permissions.
CHAPTER 8
UNDERSTANDING PERMISSIONS
To understand how to allocate permissions to users and groups, we must first understand
how the Linux permissions work. It is much easier than determining Window’s Server
permissions, once you establish an understanding for it.
On a Window’s Server there are a variety of permission types to allocate. See the figure
below:
Ubuntu and other Linux systems have three basic permission groups and three basic
permission types.
PERMISSION GROUPS
Owner – The owner permissions apply only to the owner of the file or directory.
Group – The group permissions apply only to the group that has been assigned to the file
or directory.
All Users – The all user’s permissions apply to all users on the system. This is the
permission group you will want to be the most careful of.
PERMISSION TYPES
Read – The read permission refers to a user’s capability to read the contents of the file,
but does not allow them to write or save changes.
Write – The write permissions refer to a user’s capability to write or modify a file or
directory.
Execute – The execute permission affects a user’s capability to execute a file or view the
contents of a directory.
We can view the existing permissions and check them by using the CLI and our familiar ls
command, with the –l flag.
Command ls -l
Dissecting the output:
-rw-rw-r— 1 milo milo 196 Feb 13 20:48 [Link]
The first section that is displayed, -rw-rw-r—, shows the permissions for the file.
When we are viewing the permissions by using the ls –l command, we can understand
them by using the following key.
Read r
Write w
Execute x
As we view the permission for our [Link] file, we can make a few observations based on
the output: -rw-rw-r—.
The first three characters –rw, tells us that the user milo has read and write permissions for
the file [Link]. The second set of characters shows us that the group milo, also has read
and write permissions. The last, shows us the all users permissions. From looking at the
output, all users can read the file, but not write to it.
Read 4
Write 2
Execute 1
We get the permission number by adding the integers together. We must do this for each
of the three permission groups.
For example:
Read Access = 4
Read and Write Access = 6
Read, Write, and Execute Permission = 7
Each permission group needs a number. The number we make for setting permissions will
be 3 digits. Remember, the order is user, group, and all users.
Exercise 1: What does a permission of 777 mean?
This means that the user, group, and all users have read, write, and execute access. Using
777 as a permission setting is not good for establishing strong security.
Exercise 2: What does a permission of 655 mean?
This means that the user has permission to read and write. The group has permission to
read and execute. All users can also read and execute.
Exercise 3: What does a permission of 444 mean?
This means that both the user, group, and all users have only read access to a file.
Exercise 4: What does a permission of 755 mean?
This means that the user has read, write, and execute permission. The group and all users
can only read and execute. This is often the permission setting recommended for the web
directory for those using WordPress.
Before we go over the steps of applying permissions we should first learn how to transfer
files. That process will open the door for applications in which we will learn to set
permissions.
FTP TRANSFER
Assuming that you want to install WordPress, an easy way to do that is by using FTP.
FTP stands for file transfer protocol and is used to transfer files between a client and
server.
To connect and send files to our server we will use a program called FileZilla. FileZilla
offers both a FTP client and server. For us to upload files, we need the client. You can
download FileZilla from: [Link]
Our DigitalOcean VPS already has FTP capability preloaded for SFTP, which is what we
will be using. SFTP, uses SSH to transfer files using secure shell. There are other
options available that you could install to your server for FTP, if you decide not to use the
one that I recommend.
One that I have used and would recommend is VSFTPD, but we are not going to walk
through the setup of that in this beginner’s book.
Let’s make a file on our computer to transfer. Open notepad and create a file named
[Link]. You can type whatever you like within the file, but we will use this to test file
transfer.
Once you have that file created and have the FileZilla Client installed we can continue by
launching the FileZilla Client.
To connect some information will need to be entered in the top of FileZilla: Host,
Username, Password, and Port.
In the host field we will need to enter our IP address. In the username field and password
field we will enter the user milo and his password. The port number for SFTP is 22. Once
that is entered we can click Quickconnect.
Once you connect, you will see the home directory for milo on the right of FileZilla. On
the left you will see your local computers file structure. Navigate to the folder on your PC
that you made the [Link] file and drag it over to milo’s home directory. In a few
seconds you will see that the transfer was successful. Now if we log into our server with
PuTTY and use the ls command, we will see the file.
If you wish to use your VPS for a web server, we will need to put files into the
/var/www/html directory. In FileZilla, change the remote site directory to:
/var/www/html. See below for reference:
Now let’s try to upload our [Link] file to this directory.
Your upload will fail as seen in the image below.
Why is that? Well, we do not have the permissions for milo to upload to that directory.
To review the command above we must note that we need to use sudo. This elevates the
permissions for milo. Without the use of sudo, we will get errors and the command will
fail.
After sudo, we use chown which allows us to change the owner of the directory. The
command chown, requires a user (milo) and a directory (/var/www). The flag –R will
make milo the owner of all the subdirectories as well. By using this, milo also owns the
html directory.
While we do not currently have an example to show the changing of what user owns a file,
you can follow the table below. It is much similar to changing the owner of a directory.
Experiment with this to practice.
For the next steps in this example guide, we will need to download WordPress. You can
download this at: [Link]
Once you have downloaded the .zip file with the WordPress files, then upload it to the
/var/www/ directory using FileZilla. Remember, you can browse to it or drag and drop it
into the interface.
When you finish your upload, it should look like the image below.
Important Note: WordPress updates constantly so your version may be different.
If you have this, then you can log into your VPS. We can now install a package to unzip
the WordPress files if one is not already included.
UNZIPPING .ZIP FILES
Install the Unzip Package
The DigitalOcean VPS does not come with this, so we will need to install unzip. The
installation process should begin to look familiar. Again, we use sudo to give milo extra
access to install packages and make system changes.
Once the installation is complete, change to the /var/www directory.
Command cd /var/www/
By now a lot of this should be getting easy with navigating and installing software.
Once we are in the /var/www directory, use the ls command. By using this we can see our
WordPress zip file. Now let’s use unzip to extract those files.
Important Note Recap: WordPress updates constantly so your version may be different.
Your .zip file name may vary.
Run ls again and you will notice that we now have a WordPress folder.
In the next several pages, by changing the Apache configuration file and continue the
discussion on permissions for this practical use.
MODIFYING APACHE2 SITE CONFIGURATION
Since WordPress’s zip file creates a new directory in the www directory, we need to tell
Apache to point to the new directory. You can host a variety of sites on your server by
creating configuration files in the sites-available directory, which is located at
/etc/apache2/sites-available.
Let’s change to this directory using cd and see what is in there using the ls command.
Command cd /etc/apache2/sites-available; ls
CLI milo@ubuntu:~$ cd
Display /etc/apache2/sites-available; ls
[Link] [Link]
If you host multiple sites, you will need multiple configuration files. This book will
explain how to enable and disable these configurations, but to understand them more in-
depth, I would recommend finding a detailed guide on Apache configurations. That’s a
book in itself, but I will try to cover the basics.
For what we are doing, we will want to modify the [Link] file. Just as a
point of reference, this is the default Apache site configuration file. Remember that to use
nano to edit this file milo will need super user access by using sudo.
Using nano to Modify [Link]
[Link]
<VirtualHost *:80>
# The ServerName directive sets the request scheme,
hostname and port t$
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the
ServerName
# specifies what hostname must appear in the request’s Host:
header to
# match this virtual host. For the default virtual host (this file)
this
# value is not decisive as it is used as a last resort host
regardless.
# However, you must set it for any further virtual host
explicitly.
#ServerName [Link]
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
# Available loglevels: trace8, …, trace1, debug, info, notice,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g. LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/[Link]
CustomLog ${APACHE_LOG_DIR}/[Link] combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For
example
# the following line enables the CGI configuration for this
host
# only after it has been globally disabled with “a2disconf”.
#Include conf-available/[Link]
</VirtualHost>
As we look at this file it may appear overwhelming. What is important to know is that
lines that start with the pound/hash sign (#) are comments used to help users understand
how to use it.
If we omit the comments in this file, it is much easier to digest. Leave the comments in
your file, you may need them for reference, but below you can see the substance of this
configuration file without them.
[Link]
<VirtualHost *:80>
#ServerName [Link]
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/[Link]
CustomLog ${APACHE_LOG_DIR}/[Link] combined
</VirtualHost>
This is the basic framework in the configuration that outlines: domain name, server admin,
document root location, and the location for error logs.
I left the one comment “#ServerName [Link]”. We will get back to this in a later chapter. It is
commented out because we do not have a domain name pointing to this server yet.
What we need to update is the DocumentRoot line and the ServerAdmin line. You may enter your email address in the
ServerAdmin line.
Change the DocumentRoot to the following: /var/www/wordpress
When you are done, the configuration (excluding comments) should look like the one on the next page.
[Link] (Updated)
<VirtualHost *:80>
#ServerName [Link]
ServerAdmin info@[Link]
DocumentRoot /var/www/wordpress
ErrorLog ${APACHE_LOG_DIR}/[Link]
CustomLog ${APACHE_LOG_DIR}/[Link] combined
</VirtualHost>
Once the changes have been made, use control and x to exit. Hit Y and Enter to save the
changes.
Before we move back into talking about changing permissions, we should consider how to
enable and disable site configurations.
CLI milo@ubuntu:/var/apache2/sites-
Display available$ sudo a2dissite 000-
[Link]
Site 000-default disabled
To activate the new configuration,
you will need to run service
apache2 reload.
milo@ubuntu:/var/apache2/sites-
available$
When you make changes to Apache2 configuration files, you will almost always need to
restart apache. You can do so by using the following command: sudo service apache2
restart
When that is complete the new settings will take hold. Now since we want to use the 000-
[Link] site configuration, we need to re-enable it. Follow the table below.
CLI milo@ubuntu:/var/apache2/sites-
Display available$ sudo a2ensite 000-
[Link]
Enabling site 000-default.
To activate the new configuration,
you will need to run service
apache2 reload.
milo@ubuntu:/var/apache2/sites-
available$
Once again, we need to restart apache with the following command: sudo service apache2
restart
Now if we load our IP address into the browser, we will see the WordPress installation
landing page. Do not click “Let’s Go!” yet as we still have some permissions to update.
RECAP ON CHANGING OWNERSHIP AND PERMISSIONS
Now before we can continue, we need to make some ownership and permissions changes.
This is a great way to pull the whole chapter together.
Once you are logged into the server, we must first change the owner of the folder to a non-
privileged user, who is already built into the Apache2 install. This user is www-data.
Now we need to set the permissions for the directories.
WordPress recommends using 755 as the permission binary.
This means that the user (www-data) has read, write, and execute permission. The group
and all users can only read and execute. This allows www-data to make changes to the
files in the directories, but other users can only read and execute. These permissions are
needed for a user to load the website.
Changing Permissions of a Directory
Remember using the –R flag makes the settings apply to all subdirectories as well. If we
do not use this, we will not be giving the permissions to other subdirectories in the
wordpress parent.
Now that the permissions are set, we can move forward in creating a database in MySQL.
CHAPTER 8
CREATING A DATABASE IN MYSQL
There are a multitude of books on MySQL. This chapter is going to be very short, but will
cover how to make a database for installing almost any popular content management
system or other web software that needs a database to use.
I typically use the CLI for managing MySQL, but since this is geared towards beginners, it
may be easier for us to use phpMyAdmin to create the database.
From the web browser, enter your IP address/phpmyadmin (Example:
[Link] Replace [Link] with your IP address. Then
login to phpMyAdmin.
You should see a home dashboard like the one below:
What we are going to do is create a special user for WordPress to use and a database that it
is limited to. This is a good security practice.
In the top left, click on Users.
Now it is going to ask you for the following information. If you followed my example
earlier, it should look similar to this:
Database Name: website
User Name: website
Password: [Your randomly generated password]
Database Host: localhost
Table Prefix: wp_
Once that information is entered you will be prompted to run the install.