So I’m teaching a friend how to set up Linux, Apache, MySQL, and PHP. Here’s my version of teaching you how to do it.
• Download and install Ubuntu (Server or Desktop.) See instructions here. Install with the default options, or just run the Desktop version from USB.
• Remember that if you have trouble running a command, you might need to type sudo before it to run it as root (the administrator account on Linux machines. Most installing/repairing/uninstalling/configuring work requires root privileges.)
• To edit files, use nano (easiest), vim (hardcore), or gedit (if you’re running desktop not server). In Ubuntu Desktop, you access programs using the “Dash Home” (aka Ubuntu Menu at the top left) and typing a command or search keyword. For this guide we’ll be working out of the terminal, so type “terminal” and open the terminal program.
• From terminal, to edit a file like /etc/apache2/httpd.conf you’ll type
vim /etc/apache2/httpd.conf. Other useful commands include
cd for changing directories,
ls for listing files in a directory, and
find * -name "*a*" for finding everything under the current directory with “a” in the name.
• Also remember that the text to the left of your cursor (user@computer:folder$) shows you the current folder you’re in (such as ~ which is a shortcut for your home folder, /home/yourusername). Everything under Linux is stored as a file under the root directory (/) and so there are no C: or D: prefixes like in Windows. If you see a preceding slash (/home/user) that means “the user folder, under the home folder, under the root folder.” This is an important distinction because “home/user” could mean any home folder anywhere, depending on context. If your current folder is /var/www then you’d be talking about /var/www/home/user which might not even exist. So pay attention to slashes and context.
• Use of the arrow keys, and home/end/pgup/pgdn, will help a lot. You can press the up arrow to re-type previously entered commands, press Ctrl+R to do a reverse-search of previous commands, and open the ~/.bash_history file to see logs of your previous sessions.
• Use the Tab key to auto-complete things on the command line. To type “vim /etc/apache2/httpd.conf you can actually type “vim /etc/apahtt” (if there is no auto-completion, then you need to type more to be more specific.)
• Rule #1 of Linux is RTFM — this means either read the manual (by typing man in the terminal, for example “man vim” or “man find” or “man life”, OR just Google it. Copy the error or key words from the error into Google and hunt away! Finally, don’t be afraid to ask– but not before reading the man page and Google (or if you’re in a class, ask your neighbor or the instructor.) Open source software like Linux is all-volunteer, so help volunteers by asking intelligent questions — questions that you still have after attempting to find the answer yourself. For further reading, check out “How to Ask Questions the Smart Way” by ESR: http://is.gd/smartquestions
• To copy-paste from the Ubuntu Desktop terminal, highlight something with your mouse and press Ctrl+Shift+C (copy) or Ctrl+Shift+V (paste). You can also right-click and choose the copy or paste options.
(END LINUX PRIMER)
• Make sure you’re connected to the internet. Use the Wifi icon in the top right to connect to networks, or in terminal type
ping 188.8.131.52 to check what might be wrong.
• Let’s start by typing
sudo -i so that we become root and don’t have to type sudo after half this stuff. If you’ve got a password, it’ll ask you to type it.
• A brand-new Ubuntu install might not have its software repositories updated. Type
apt-get update to fetch info about the latest software available.
• Run this command to install Vim, Apache, Git, MySQL, and PHP with my favorite plugins.
apt-get install vim apache2 git-core mysql-server php5 libapache2-mod-php5 php5-gd php5-curl php5-mysql php5-xsl php5-cli
• Pay attention to any errors during this process; problems here can result in a piece of software not working later on in this guide and retracing your steps sucks.
• MySQL should ask you for a root password. Make up something extremely long and random, and store it in a safe place for future reference. (In this case, a sticky note is actually more secure than a text file.) You’ll need to type this in when we get to the MySQL section and any other time you need to administer the MySQL server.
• This is my backup script (crontab) — to make a crontab, type crontab -e
# Below is for regular backups.
10 19 * * * /usr/bin/mysqldump --opt -uUSER DBNAME > /backups/DBNAME.sql
14 19 * * * /usr/bin/mysqldump --opt -uUSER mysql > /backups/mysql.sql
16 19 * * * tar -zcf /backups/etc-backup.tgz /etc
18 19 * * * tar -zcf /backups/www-backup.tgz /var/www
For the root user, create a /root/.my.cnf file (as root) with the following content:
Note that USER, PASSWORD, and DBNAME should be replaced appropriately. Formerly the crontab script included a -pPASSWORD option (without a space) but this is less secure and reliable than the .my.cnf method.
If your backups aren’t created properly (0 filesize) it may be because the backup user’s password isn’t getting found. You can add
--defaults-extra-file=/root/.my.cnf to the mysqldump command in your crontab.
Make sure you create /backups and that it is writeable by root (or whoever the cron job runs as.) Test the command before putting it in your crontab.
You’ll see that I’m doing a mysqldump of the database named ‘mysql’ — this is intentional as it is the database that holds configuration information including database users which you’ll probably want during a restoration.
Also, I’m doing a backup of /etc which is where most configuration files are stored. Half of a website is how it’s configured, so you’ll definitely want that during a restoration.
This should take care of most Apache/MySQL websites as long as all websites are stored in /var/www — if not, modify the script as needed.
• Finally, it would be a good idea to encrypt these files or at least make sure they’re chmod 700 — readable only by the folder owner and nobody else — because they will invariably contain passwords in the config files and mysql tables. Your mileage may vary and it’s your responsibility to make sure you’re adequately secured.
Don’t forget to copy your backups offsite. I typically add this line to a remote crontab -e:
15 21 * * 1 scp -r firstname.lastname@example.org:/backup /backups/latest/example
You’ll need to copy ~/.ssh/id_rsa.pub from the remote machine to the /root/.ssh/authorized_keys file on example.com though, to let scp run without a password. (Test scp manually so you can accept example.com’s key first.)
• Save, exit, and check back tomorrow to make sure the contents of /backups looks good (in nano, save is Ctrl+O and exit is Ctrl+X. In vim, you insert text by typing i, exit from insert mode with Escape, save is :w and exit is :q)