CHAPTER 17

image

Linux

Drupal is usually found within a LAMP (Linux, Apache, MySQL, and PHP) stack. This chapter is dedicated to helping you understand Linux, and it has lots of tips and tricks so you can get comfortable with navigating around your Linux box. My personal machine is a Mac running OSX, which is a Linux flavor. I also like Ubuntu for my production computers, which house the development, stage, and production environments for my projects. Ubuntu is another popular Linux flavor. This chapter will focus on these two flavors of Linux—OSX and Ubuntu—but once you learn one flavor of Linux, it’s not difficult to pick up other Linux variants. Most of the commands are identical.

Image Note  If you’re using Mac OSX from the command line, then you already know some basic Linux. Macs use a Linux core as the base of the operating system, which is the reason many developers love having a Mac. Many of us know the rivalry between Macs and PCs; there is also a friendly rivalry between Linux and PC-based computing in the world of professional computing. In any case Drupal has traditionally been a LAMP-based technology, and the “L” in LAMP stands for Linux. Recently Microsoft has been courting the Drupal community. It has sponsored getting Drush to work in Windows, and it has been trying to jump into the hot open source space. And anything that helps open source, well, I wish them much luck.

Let’s now look at some Linux basics. We’ll begin by accessing a Linux prompt from both a Mac and a Windows machine.

Introduction to Linux on a Mac

With a Mac all you need to do to get to a Linux command-line prompt is open your Terminal program. You can go to Spotlight (the magnifying glass that helps you find things on a Mac), type “terminal,” and click the Terminal program that pops up. You can also go to your Applications list and then into the Utilities folder; you’ll find the Terminal icon there as well. If you wish, you can drag the Terminal icon to your dock for easier access, as I have done (see Figure 17-1).

9781430264668_Fig17-01.jpg

Figure 17-1. The Terminal program on a Mac

Once the Terminal program on a Mac is open, you can start typing in Linux commands. (The section “Getting to Know Some Common Linux Commands” later in this chapter covers some of the most common Linux commands and tasks.) You’ll notice in Figure 17-1 that I have multiple tabs open with multiple Terminal windows in use (one per each tab). To open the first new tab, you click Shell in the Terminal menu bar and then click New Tab. You can open additional new tabs by clicking to the right of the word “bash” in the upper part of the tab furthest to the right. It can be very useful to have multiple tabs open at the same time.

Image Note  You can get your own Linux box to connect to by opening an account with Amazon, Rackspace, Linode, Digital Ocean, or countless other vendors that offer cloud computing at a price.

Connecting to Ubuntu

Ubuntu is the other Linux flavor I use almost every day. This section shows you how to connect to Ubuntu via the Mac and Windows interfaces.

Connecting to Ubuntu from a Mac

Typically, you would connect to a Linux box from a Mac through the Terminal program. If you needed to connect to a Linux box, you would type a command similar to the following—my Linux box is called drupalessentials.babson.edu; your name would differ:

ssh drupalessentials.babson.edu

Then you would be prompted to type your username, followed by another prompt for your password. You could also type the following command, replacing jbarnett and the Linux box name with your own information:

ssh [email protected]

In this case, you would be prompted to type your password, because you provided the username in the initial command to connect to your Linux box. You can see in Figure 17-2 I’ve successfully connected to my Ubuntu Linux box with the domain name drupalessentials.babson.edu.

9781430264668_Fig17-02.jpg

Figure 17-2. Connecting successfully from a Mac to an Ubuntu box that exists out on the Internet

Connecting to Ubuntu from Windows

To connect to an Ubuntu box from Windows, first you will need to download a tool called Putty (www.putty.org/). Once you install Putty on Windows, consider leaving the Putty icon on your desktop for easy access to it.

When you double-click the Putty icon, Putty will open. You’ll see the Putty terminal, which is ready for you to input the connection information to your Linux box (see Figure 17-3).

9781430264668_Fig17-03.jpg

Figure 17-3. Putty in Windows

You can connect to a Linux box by IP (Internet protocol) (four groups of numbers separated by dots that uniquely identify a computer on the Internet) or by its domain name. I usually connect by the domain name. In Figure 17-4 I’m connecting to the Ubuntu Linux box drupalessentials.babson.edu, so for the hostname I type drupalessentials.babson.edu and I make sure that the SSH connection type is selected.

9781430264668_Fig17-04.jpg

Figure 17-4. Inputting connection info into Putty to connect to the Linux box

You can see in Figure 17-4 that I’ve input my username and password. In Figure 17-5 you can see what it looks like after I click Open within Putty. Doing so connects me to the drupalessentials.babson.edu Linux box.

9781430264668_Fig17-05.jpg

Figure 17-5. Successfully logged into a Linux box using the Putty program to connect via the SSH prototcol

Getting to Know Some Common Linux Commands

Now we’re going to go through some common commands and things to know in Linux to help you navigate around and be very effective to get things done.

Navigating Among Directories and Managing Files

You can use the following command to show you the current directory you are working in:

pwd

To look at the contents of a directory, type

ls

To look at a “long” listing of a file or files, add the -l option.

ls –l

The ls –l command will give you much more information about a file, such as the current permissions on a file, who owns the file, and which group owns the file. For instance, take the following line (see Figure 17-6):

-rwxrwxrwx  1 jbarnett  www-data  0 Dec  1 06:53 filename.txt

9781430264668_Fig17-06.jpg

Figure 17-6. A long list (ls –l filename.txt) of filename.txt

The preceding line displays after issuing the command ls –l filename.txt. The user who owns the file filename.txt is jbarnett, and the group that owns the file is www-data. We’ll learn commands to change the owner or group that owns a file shortly.

To look at the contents of a directory in reverse chronological order (I use this command quite often to see what files have changed recently); the files that have changed most recently show at the bottom of the directory listing with the following command:

ls –ltr

To copy a file use

cp

And to copy recursively (all subdirectories and files), use the –r argument to the cp command as follows:

cp -r simple_block /Users/jbarnett/github/Drupal-Web-Essentials

With the preceding command, I copied the simple block directory and all subdirectories and files into the path Users/jbarnett/github/Drupal-Web-Essentials.

To move a file (where you don’t leave a copy behind), use

mv

For example,

mv file.txt /home/jbarnett

The preceding command will move file.txt into the /home/jbarnett directory.

To delete a file, use the rm command.

rm file.txt

The period (.) refers to the current directory; the double period (..) refers to the directory above the one you’re in. Consider the following:

mv filename.txt ..

This command moves the filename.txt file into the directory above the one you’re in currently (i.e., into the parent directory).

Installing LAMP

If you see apt-get in any Linux instructions, you’re calling the Linux package manager to download some new software for your Linux (or OSX) machine. To get LAMP on an Ubuntu box (a favorite Linux flavor) you have to type the following:

sudo apt-get install tasksel

This is not the only way to install the full LAMP stack in Ubuntu; I’m just showing you what apt-get is in case you use the popular Ubuntu flavor of Linux. Other versions of Linux, OSX as well, have their own package manager. For example, OSX uses macports rather than apt-get.

Adding Security with Permissions

If you see the sudo command in front of some Linux commands, it means you need “root” access—“root” in the Linux world is the uber admin user on a Linux box. You can be the root user to run commands, but best practice is for your Linux admin to add you as a sudo user, so the system will log who did what using sudo (root) rights. This is a good security precaution. If you sudo to root, this is synonymous with “I switched temporarily to be the root user.” You would sudo to root if your regular user account doesn’t have enough permissions to complete a particular task.

If you want to simply create a new empty file, you type

touch filename.txt

This preceding code will create the file filename.txt.

To change the permissions of a file, you use the chmod command, for example,

chmod 771

I’ll explain what the numbers 771 are. First, do an ls -l on a file to view the file’s permissions. Alternatively, you can do an ls –ltr to see a whole directory’s list of files and each file’s permissions. Consider the following output:

L12-1007:drupal jbarnett (8.x)$ ls -ltr
total 200
-rw-r--r--   1 jbarnett  1321145334  70280 Oct 17 15:42
   composer.lock
-rw-r--r--   1 jbarnett  1321145334   1156 Oct 17 15:42
   composer.json
-rw-r--r--   1 jbarnett  1321145334   4880 Oct 17 15:42 README.txt
drwxr-xr-x  27 jbarnett  1321145334    918 Oct 17 15:43 core
-rw-r--r--   1 jbarnett  1321145334   4053 Oct 17 15:43 web.config
drwxr-xr-x   3 jbarnett  1321145334    102 Oct 17 15:43 themes
drwxr-xr-x   5 jbarnett  1321145334    170 Oct 17 15:43 sites
-rw-r--r--   1 jbarnett  1321145334   1292 Oct 17 15:43 robots.txt
drwxr-xr-x   3 jbarnett  1321145334    102 Oct 17 15:43 profiles
-rw-r--r--   1 jbarnett  1321145334    564 Oct 17 15:43 index.php
-rw-r--r--   1 jbarnett  1321145334    999 Oct 17 15:43
   example.gitignore
drwxr-xr-x   5 jbarnett  1321145334    170 Oct 17 15:44 modules

You can see README.txt has the permissions -rw-r--r-- next to it on the far left. If the first letter is a d, this signifies that it is not in fact a file but a directory; if the first character on the far left is a dash symbol (-), then it is in fact a file. The next three letters define the permissions for the file for the currently logged-in Linux user. The next three letters define permissions for the group. And the final three letters define the permissions for “other”—the rest of the world outside the group that owns the file and he or she who is not the currently logged-in user.

Now for what the numbers mean: 4, 2, 1—these numbers correspond to the permissions for read-write-execute, respectively. The 4 is read (the right to open and read the file), the 2 is write (the permission to write and make changes to the file), and the 1 is execute (if the file is a command, the right to execute the file). If the group has all three permissions, its members have 4+2+1 = 7. So, for example, if I type #chmod 070, the user has no permissions, the group has all permissions possible, and everyone else has no permissions.

There is another system to set permissions as well, which is common, although I use the numbers 4+2+1 myself. You can add read, write, and execute permissions to a file for the user (whoever is currently logged in) with the following:

chmod +rwx "filename.txt"

You can add just execute permission for the user (whoever is currently logged in) with the following:

chmod +x "filename.txt"

You can likewise add permissions to the group or all other users by prefixing with g or o, for example,

chmod g+wx

This would give the group that owns the file both write and execute permissions. If you didn’t use the plus sign (+) but typed in chmod g-wx, then this would subtract the write and execute permissions from the group-level permissions.

Take a look at Figure 17-7. You can again see the ls –l command on filename.txt.

9781430264668_Fig17-07.jpg

Figure 17-7. The file permissions currently on filename.txt

Figure 17-8 shows the result if I instead issue the command chmod 777 filename.txt. Notice in the far left the permissions read –rwxrwxrwx since now we’ve given all permissions to all users at the user level, the group level, and the other (all other users) level.

9781430264668_Fig17-08.jpg

Figure 17-8. The file permissions on filename.txt after running chmod 777 filename.txt

If I type chmod og-rwx filename.txt to remove read, write, and execute permissions for both the other and group users, the result—after typing ls –l—would look like the display in Figure 17-9.

9781430264668_Fig17-09.jpg

Figure 17-9. Removing some permissions from a file

To change the group that owns a file, you would use

chgrp www-data filename.txt

The preceding command would change the group ownership for filename.txt to be www-data (the name of the group needed to serve up web files on an Apache server without permission errors). If you are a member of a group that has permissions to a file but are not the owner of the file, you will still be able to manage the file depending on what file permissions are given to the file at the group level. If at the group level you have read, write, and execute permissions, then even though you’re not the owner of the file you still have full permissions. To add users to a group, if you have root access to the box or sudo access (you’re in the list of users with sudo rights—which means you have root/administer level status—you can edit the /etc/group file to add folks to groups.

To change the owner of a file you would use

chown student2 filename.txt

The preceding code changes the user who owns the file to be student2.

You can search for a file with the following command:

find . –name filename.txt

The find command can also take wildcard characters.

find . –name file*

The preceding code would find all files that start with file as the first four letters, like filename.txt, filename.php, filename.html, or filezilla.txt.

Searching for Specific Files

One of my favorite commands, which is really useful, is grep.

grep –lri "James" *

This command searches all files recursively starting in the present directory for any files that contain the word James in them. You can omit the l in the command and it will list more than just filenames; it will also show you the context of where it found “James” in the file. Also you can omit the i to make the search case sensitive. You can omit the r from the command and the search will no longer be recursive through all files, and all files within subdirectories under the present directory.

These are some of the most basic, most common, and most important Linux commands, and this should take you pretty far in being effective in your work with Linux. There are whole courses available in Linux administration, but you would be surprised how far you can get just with the preceding knowledge.

Creating Shell Scripts

You can also create “shell scripts,” which allow you to put a bunch of Linux commands in a series to run one after another. And there are looping structures and other features you find in programming languages available when shell scripting. Here is an example shell script in my Mac Vim editor which loops through all files with the .txt extension and replaces any instances of the word “needle” in the files with the word “haystack” (see Figure 17-10).

9781430264668_Fig17-10.jpg

Figure 17-10. A linux shell script

The name of this shell script file is find_replace.sh. To run this shell script on the command line, type

sh find_replace.sh

You can, of course, call your shell script file anything you like and then just give it the sh extension. To run your script, type

sh filename.sh

You’ll notice at the top of my shell scripts I put in the following line:

#!/bin/sh

This code defines which Linux shell to run, as each Linux shell has minor variations on the syntax and commands it takes. Actually, even on the command line, you can switch shells. I usually use the bash shell because if you start typing a command and press the Tab key it will often autocomplete what you’re about to type.

Editing Files on a Linux Box

When you want to edit a file on a Linux box, you have a few options. If it’s your local Mac, then you can use whatever editor you like. All editors can edit local files quite well. But if your file is on a remote Linux machine, your options are a bit more limited. You can use an editor like Komodo Edit, which allows you to edit remote files (files not located on your own computer), and you can connect via sftp protocol, which some editors support. But you also might find yourself wanting to just make a few quick edits. This section shows you how to do that using Komodo Edit and Vim.

Editing Files with Komodo Edit

If you want to use the Komodo editor to edit local windows, local Mac, or remote Linux files, you would first open up the Komodo editor. You’ll see a screen that looks like the one in Figure 17-11.

9781430264668_Fig17-11.jpg

Figure 17-11. Using Komodo Edit to edit Linux files

Then once Komodo Edit is open, you’ll want to open up the file browser, which you can learn how to do by looking at Figure 17-12.

9781430264668_Fig17-12.jpg

Figure 17-12. Opening a file browser in Komodo Edit to open a local or remote file

Once you have the file browser open, you can click Open Directory and easily browse for a local file on your Mac or Windows machine to edit.

If you want to open a remote file on a Linux box, you need to configure an sftp connection first. Take a look at Figure 17-13 to see how to configure an sftp connection to a remote Linux box.

9781430264668_Fig17-13.jpg

Figure 17-13. Configuring Komodo Edit to edit a remote file

Then once you click the icon to configure your sftp connection, you’ll see the screen shown in Figure 17-14.

9781430264668_Fig17-14.jpg

Figure 17-14. Configuring an sftp connection in Komodo Edit to edit remote Linux files

You can see I’ve put in the hostname of my Linux machine, which is drupalessentials.babson.edu. I put in 22 as the port, which is the default port for sftp connections. I also provided the username and password to connect with, and I provided the default directory path to open files within, which I put in as /var/www/student1. I also put in the name of the connection as drupalessentials, because the Komodo editor will save the connection for later use under this name. When you’re done, click the Add button so that this connection will be saved for later use. Then choose your new connection, as shown in Figure 17-15.

9781430264668_Fig17-15.jpg

Figure 17-15. Choosing your already saved, configured remote connection

Now you’ll see the default remote directory you configured to open, in the left pane of your Komodo editor. You can choose files to edit by clicking them in the left pane; the files will open in the main pane of the editor on the right (see Figures 17-16 and 17-17).

9781430264668_Fig17-16.jpg

Figure 17-16. Picking a file to edit from the remote directory, a local file directory will display this way as well and files can be opened in the right pane of the editor in the same manner

9781430264668_Fig17-17.jpg

Figure 17-17. The selected file available for editing

You can see I’ve double-clicked the test.php file on the left and the file has opened for editing on the right side. You can make your edits and then save your file just as you would in Microsoft Word or any other editor you’re used to. The file with your changes will be saved on your local computer or remote computer, depending on where you opened the file from.

Opening Files with VIM

You can also edit files directly on the remote Linux box using a program called vi editor. There a few other choices as well, like emacs, but I favor vi editor. Opening files right on the remote Linux box you’ve connected to via SSH protocol using Putty or the Mac Terminal can be great for a quick edit, rather than having to hook up your more bulky editor like Komodo Edit.

vi is annoying, geeky, and cool all at the same time! (You can tell hard-core programmers you prefer vi as your editor and they will know you are one of them and are hard core!) I actually use VIM, which is a slightly more graphical and friendly version of vi, over 50% of the time. Once you know it, it is very powerful and you probably won’t want to use anything else, but there is a learning curve. I’m going to stick primarily to vi in this tutorial, but you can download vim (or the Mac version mvim) and play around with it.

When you open a file with vim, you type

vi filename.txt

The file opens for editing, as shown in Figure 17-18.

9781430264668_Fig17-18.jpg

Figure 17-18. Opening a file for editing using vi

Once you’ve opened a file with the vi editor, you can use the arrow keys on the keyboard to move around the screen. Initially you’re in “command mode” and you can press a key like the “i” key (a command) to go into insert mode and begin typing. Then you can press the escape key again to get out of insert mode and back into command mode. You can then press the “x” key to delete a character or “dd” to delete a whole line.

When in command mode—rather than insert mode (you can always press the escape key to make sure you’re in command mode)—you can press “:w” to save the file or “:w!” to “force save” if the editor gives you any warnings about saving. You can also type “:q” to quit the file or “:q!” to force quit to avoid any warnings. Or you can combine these two commands and type “:wq” to save (write) the file and then quit right afterward, or you can type “:wq!” to force a write (save of the file) and then quit.

The vi editor is not known for being very user friendly, but it is still loved by many. As I said, for the desktop there is a very popular version of vi called VIM (www.vim.org/) and there’s also a Mac version called mvim (https://code.google.com/p/macvim/). This version of vi is more user friendly and has tons of options and integrations with other libraries and projects to aid with code completion, error checking, integration with Git version control, and so on. I prefer mvim and use it 99% of the time when editing a file. I usually recommend Komodo Edit for newbies but it’s a powerful full-featured editor in its own right as well, certainly not second best.

Summary

In this chapter, you learned how to connect remotely to your Linux box using either a Mac or a Windows machine. You then discovered the most important Linux commands so you can find your way around a Linux box. In addition, you learned how to list files and directories, and you worked with permissions, group ownership, and individual ownership of files. Commands like find and grep are essential to help you find what you’re looking for and grep in particular is great if you see a word on your web page and need to modify that page where you see that word or words appear. You then can use the grep command to try to figure out what files you need to modify to make the changes you need to make. Finally, you explored the various methods of editing files either directly on the Linux box using vi editor or by connecting with an editor with sftp capabilities like Komodo Edit.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset