Why website builders should know
a bit of Linux
Stop clicking, start understanding
by Peter Martin /
db8.nl
slides:
https://petermartin.nl
--- ### Before we start Who uses:
cPanel, Plesk or DirectAdmin
FileZilla or WinSCP
Shared hosting or a VPS
Congratulations.
You already use Linux every day.
Note: Almost every hand goes up. The punchline: all those panels and hosts run Linux underneath. You're not learning a new OS today โ you're learning to talk directly to the OS you already depend on, instead of clicking through a middleman. ---- ### Overview 1. The Linux basics 2. The filesystem
(
spoiler
: everything is a file)
3. Permissions & ownership 4. The Unix principle
(=
small tools
with
one task
)
5. Practical examples: grep, rsync, bash 6. a bit more difficult: scripts, cron, Docker
**Easy first. Bit more difficult at the end.**
---
Part 1: The Linux basics
---- ### Linux = The
kernel
๐ kernel
The core of Linux. It
talks to
the hardware:
memory
,
disks
,
network
,
processes
.
**Stable. Boring. Invisible.**
The
same Linux kernel
runs on your
Android phone, router, Raspberry Pi, car navigation,
mail server, and the server that hosts your website.
Note: You will never touch the kernel directly. You just need to know it exists. Everything else โ the terminal, the tools โ is a layer on top of it. ---- ### The
distribution
Linux Kernel + tools + package manager + "opinions"
Linux distribution
Commonly found on
Ubuntu
Most VPS hosts, WSL
Debian
Shared hosting
Red Hat Enterprise Linux (RHEL)
Corporate data centres
AlmaLinux / Rocky Linux
cPanel servers
Alpine Linux
Docker containers
Note: Your website is almost certainly running on one of these. Ask your host โ they'll tell you. The differences matter less than you'd think: the commands in this talk work on all of them. ---- ### The
shell
(terminal)
๐ shell
A text conversation with the operating system ```bash $ pwd /var/www/mysite $ ls administrator/ images/ templates/ index.php ```
You type something.
The computer answers.
That's all it is.
Note: The terminal is the window, the shell is the conversation. The shell most servers use is called bash. Don't worry about the distinction โ people use the words interchangeably. ---- ### Your first five commands
๐ commands
```bash pwd # print working directory: where am I? ls -lah # list: what is here? cd images/ # change directory: go somewhere cat index.php # concatenate: show me a file nano index.php # edit a file ```
**That's 80% of daily terminal use.** Note: Nano shows its commands at the bottom of the screen: Ctrl+O to save, Ctrl+X to exit. Start with nano, ignore vim discussions for now. ---- ### The terminal types for you ```bash cd /var/w
# completes to /var/www/ ```
- `Tab` โ
autocomplete
commands and paths - `โ` โ walk through
previous commands
- `Ctrl+R` โ search your command history - `!!` โ repeat the last command
Typing far less than you fear.
Note: Live demo this โ Tab completion alone removes the biggest beginner frustration: typing long paths without typos. Ctrl+R gets the biggest "wow": type a few letters, and last month's rsync command reappears. ---- ### Getting onto your server:
SSH
S
ecure
SH
ell = shell on distant computer ≈ FileZilla for commands ```bash ssh user@yourserver.com ```
Same credentials as SFTP.
Most hosting panels can enable SSH access.
```bash # secure copy one file over the same connection scp backup.tar.gz user@yourserver.com:/tmp/ ```
๐ป live demo
Note: If you use SFTP in FileZilla, you already have everything you need โ SFTP runs over SSH. Live demo: log in to a real server here, run pwd and ls, nothing scary. ---- ###
Installing software
๐ sudo
**The Windows/MacOS way:**
> Find website. Download installer.
Hope it's not malware.
Click next, next, next, finish.
**The Linux way:** ```bash sudo apt install rsync ```
One curated, signed catalog: the
repository
.
Note: sudo means "do this as administrator". apt is the package manager on Ubuntu/Debian. The repository model is why Linux servers stay clean: software comes from one trusted, maintained source. ---- ###
Open source
You already know this model:
a repository is your CMS's
extension directory
,
but for the whole server.
Linux is to operating systems
what your CMS is to websites.
Same philosophy. Same licenses.
Same community. Same reasons to trust it.
Note: Nothing on this slide should surprise you โ you build on open source every day. The whole stack underneath your site shares that ethos: Linux, Apache or nginx, PHP, MariaDB, your CMS. Curated, reviewed, one place to get it, and you can read the source when you need to. Learning Linux is not stepping outside your world; it is the same world, one layer down. ---
Part 2: Everything is a file
---- ### The
filesystem
is
one big tree
``` / โ the root, everything starts here โโโ etc/ โ configuration โโโ home/ โ your files โโโ var/ โ โโโ www/ โ your websites โ โโโ log/ โ the answers to your questions โโโ tmp/ โ temporary files ```
No `C:\` drives. Everything hangs under `/` Note: For website builders, three places matter: /var/www (your site), /var/log (what went wrong), /etc (how the server is configured). Learn those three and you can find your way on any server. ---- ###
Everything is a file
- Configuration โ a file - Logs โ a file - Devices โ a file - Processes โ a file - Network connections โ a file
**So every tool that works on files
works on *everything*.**
Note: This is the most useful sentence in web development. One mental model, infinite applications. The tools you learn for text files also inspect logs, config, even running processes under /proc. ---- ### Why it matters for your website ```bash # Your site's config? A file. cat /var/www/mysite/configuration.php # Why is the site broken? Read a file. tail -f /var/log/apache2/error.log # Where is that old domain still used? Search files. grep -Rn "oldsite.com" /var/www/mysite ```
**Same tools. Three different jobs.**
๐ป live demo
Note: tail -f follows a log live: keep it open, reload the broken page, and watch the actual error appear. This alone replaces hours of guessing in a control panel. Demo: break a PHP file on the test site, reload, watch the fatal error scroll by. ---
Part 3: Permissions & ownership
---- ### The
three questions
Linux asks Every file. Every folder. Every time.
What can
1. the
owner
do? 2. the
group
do? 3.
everyone else
do?
For each:
r
ead,
w
rite, e
x
ecute
---- ### Reading `ls -l`
๐ ls
```bash $ ls -l -rw-r--r-- 1 peter www-data 1524 index.php drwxr-xr-x 2 peter www-data 4096 images/ ```
Owner:
peter
Group:
www-data
`-rw-r--r--` =
owner `rw-`
group `r--`
others `r--`
---- ### The
octal
numbers | # | Rights | Meaning | |----|----|----| | 7 | `rwx` | read, write, execute | | 6 | `rw-` | read, write | | 5 | `r-x` | read, execute | | 4 | `r--` | read only | | 0 | `---` | nothing at all |
**755** = folders **644** = files Note: r=4, w=2, x=1, add them up. 644: owner edits, everyone else reads. 755: same, but folders need x to be entered. Write these two numbers down โ they solve most permission questions. ---- ###
chmod
changes permissions
๐ chmod
CH
ange file's permissions
MOD
e ```bash # Files: 644 find /var/www/mysite -type f -exec chmod 644 {} \; # Folders: 755 find /var/www/mysite -type d -exec chmod 755 {} \; # Your config file: 444 โ read-only, even for you chmod 444 /var/www/mysite/configuration.php ```
#### Never `chmod 777` "Anyone on this server may do anything to this file"
Note: 444 on the config file is cheap insurance: it holds your database credentials, and nothing needs to write to it after installation. Your CMS will complain when it wants to change a setting โ that is the point, you chmod it back to 644 for a minute and then lock it again. When a forum post says "just chmod 777" they are telling you to leave the front door open. More hacked sites trace back to 777 than to any other single cause. There is almost always a better answer โ and it is usually ownership, next slide. ---- ###
chown
changes owner
๐ chown
CH
ange
OWN
ership ```bash sudo chown -R www-data:www-data /var/www/mysite ```
| Symptom | Real cause | |----|----| | "Cannot write to config" | wrong owner | | Upload to images/ fails | wrong owner | | Cache won't clear | wrong owner |
**90% of "permission" problems
are ownership problems.**
Note: The web server runs as a user, often www-data. If your files are owned by your FTP user, the web server cannot write to them. That's why uploads fail โ not because the permission number is wrong. ---
Part 4: Small tools,
one task
---- ### The
Unix philosophy
> "Make each program do one thing well." >
>
โ Doug McIlroy, Bell Labs, 1978
Older than the web.
Note: The reason this idea survived: small tools that combine are more powerful than big tools that don't. Every impressive command you'll see next is just this idea applied. ---- ### The pipe: `|`
connects
the
output
of one tool
to
the
input
of the next tool ```bash du -sh */ | sort -h ```
- `du -sh */` โ size of every folder - `sort -h` โ sort by human-readable size
**"Which folder is eating my disk space?" โ answered.**
Note: Compare with the GUI way: right-click every folder, properties, write the number down... The pipe answers it in one line, sorted, on any server. ---- ###
grep
โ find text in files
๐ grep
g/re/p = command in old "ed" line editor
G
lobal /
R
egular
E
xpression /
P
rint
```bash # Which template file prints this string? grep -Rn "Copyright 2019" /var/www/mysite # How often was this IP in the access log? grep "203.0.113.42" access.log | wc -l # All PHP errors of today grep "$(date +%d-%b-%Y)" error.log ```
Note: grep -r searches recursively, -n shows line numbers. Combined with pipes you get instant answers to questions a control panel cannot even ask. ---- ### Detective work: Was I
hacked?
๐ find
```bash # Which files changed in the last 24 hours? find . -type f -mtime -1 # Any suspiciously large files? find . -type f -size +10M # Look for a common PHP malware pattern grep -Rn "eval(base64_decode" . ```
**Your control panel cannot answer these questions.** **Be aware:
mtime can be tampered with
!**
Note: This is the moment for the skeptics in the room. After a hack, the first question is always "what changed, and when?" โ find answers it in seconds. These three commands are a real incident-response starter kit. ---- ### Task: find the
10 biggest images
on the site
๐ find
GUI: open every folder in FileZilla, sort by size,
write down, compare... โ ๏ธ
Linux: ```bash find . -name "*.jpg" -size +1M | head -10 ```
The terminal saves you hours of clicking.
---
Part 5: Migrating
with rsync
---- ### The
FileZilla
migration
Download 20,000 files (2 hours)
Connection drops at 87% (start over?)
Upload 20,000 files (3 hours)
Which files failed? (No idea.)
There has to be a better way
---- ###
rsync
โ copy, but smart
๐ rsync
R
emote
SYNC
hronise ```bash rsync -avz /var/www/mysite/ user@newserver:/var/www/mysite/ ```
- Copies
only what changed
- Survives dropped connections โ just run it again - Keeps permissions and timestamps - Runs
server-to-server
, your laptop stays out of it
Note: -a = archive (keep permissions, times, everything), -v = verbose, -z = compress. First run copies everything, second run only the differences โ so you can pre-sync days before, and do the final sync in minutes during the maintenance window. ---- ###
Careful
: the trailing slash
๐ rsync
```bash # Copies the CONTENTS of mysite/ rsync -av /var/www/mysite/ backup/ # Copies the folder mysite itself rsync -av /var/www/mysite backup/ ```
One character difference.
Test first with
--dry-run
๐ป live demo
Note: This bites everyone once. --dry-run shows what would happen without doing it. Make it a habit for every rsync you did not run before. Demo: run the same rsync with and without the trailing slash in --dry-run mode and compare the file lists. ---- ### The
database
comes along too ```bash # Export on the old server mysqldump -u dbusername -p dbname > my-db-export.sql # Import on the new server mysql -u dbusername -p dbname < my-db-export.sql ```
`>` and `<`
redirect
output and input, yes files again! Note: Note the theme: even the database export is just a file, moved with the same tools. rsync the .sql file across like everything else. ---- ### Before you change anything
๐ cp
Small edit?
Make it
reversible
: ```bash cp configuration.php configuration.php.bak nano configuration.php diff configuration.php.bak configuration.php ```
Big change? Backup
with today's date: ```bash tar -czf site-$(date +%F).tar.gz /var/www/mysite mysqldump -u user -p mydb > mydb-$(date +%F).sql ```
**The best admin is not fearless.
The best admin is reversible.**
Note: diff shows exactly what you changed โ and the .bak file means any mistake is a one-command rollback. $(date +%F) puts today's date in the filename, so backups never overwrite each other. This habit costs ten seconds and saves weekends. ---- ### The last step: the
DNS switch
Files synced โ Database synced โ Now the domain: ```bash # Which IP does the domain point to right now? $ dig a petermartin.nl +short 203.0.113.42 ```
Changed the DNS record?
Watch it propagate, live: ```bash watch dig a petermartin.nl ``` `watch` re-runs any command every 2 seconds
๐ป live demo
Note: dig asks the DNS directly โ no "is it live yet?" guessing, no flush-your-cache voodoo. Leave watch running on the beamer and the whole room sees the old IP flip to the new one. Tip for real migrations: lower the record's TTL to 300 a day in advance, so the switch propagates in minutes instead of hours. And note watch itself: a small tool that does one thing โ repeat another tool โ the Unix philosophy again. ---
Part 6: a bit more difficult
---- ### A
bash script
= commands in a file `backup.sh`: ```bash #!/bin/bash DATE=$(date +%Y%m%d) mysqldump -u user -p"$PASS" mydb > /backup/db-$DATE.sql tar -czf /backup/files-$DATE.tar.gz /var/www/mysite rsync -az /backup/ user@backupserver:/backups/mysite/ ```
```bash chmod +x backup.sh # remember execute permission? ./backup.sh ```
Note: Nothing new here โ three commands you've already seen, saved in a file. That x permission from part 3 is what makes a file runnable. This is where the earlier basics pay off. ---- ###
cron
โ run it every night ```bash crontab -e ``` ```bash # min hour day month weekday command 30 3 * * * /home/user/backup.sh ```
**Every night at 03:30. While you sleep.** Note: Five time fields, then the command. crontab.guru is a great site to decode the syntax. A nightly automated backup puts you ahead of most agencies. ---- ###
Docker
, a server in a box A complete test environment in seconds: ```bash docker run -d -p 8080:80 joomla ```
- Try new CMS version **before** upgrading production - Same environment on every developer's laptop - Broke it? Delete it, start fresh Note: Docker packs an application plus its whole environment into a container. Under the hood it's pure Linux: kernel features, and yes โ containers are configured with files. Show docker ps and docker logs if there's time. ---- ### docker-compose โ the whole stack `docker-compose.yml`: ```yaml services: web: image: joomla ports: ["8080:80"] db: image: mariadb environment: MYSQL_ROOT_PASSWORD: example ``` ```bash docker compose up -d ```
**Website + database. One file. One command.** Note: The configuration of the entire stack is โ of course โ a file. Which means you can version it in git, share it, and reproduce it anywhere. Everything is a file, all the way down. ---- ###
Five common mistakes
1. Running everything as root 2. "Just" `chmod 777` 3. No backup before editing 4. Editing production first 5. Ignoring the logs
Every one of them has a slide in this talk.
Note: Quick recap disguised as a warning list: sudo for one-off commands only (part 1), ownership instead of 777 (part 3), backups (part 5), Docker as your test environment instead of production (part 6), and tail -f (part 2). ---
Monday morning
---- ###
Windows
: install WSL2
```powershell wsl --install ``` Restart, open "Ubuntu" from the Start menu. **Real Linux inside Windows. Free. Ten minutes.**
###
macOS
: Terminal is already there
`Cmd + Space` โ "Terminal" โ Enter
Note: WSL2 is not a simulator โ it's a real Linux kernel running inside Windows, built by Microsoft. For Mac users: macOS is a Unix, nearly all of this talk works out of the box. ---- ###
Three commands
to try tonight ```bash # Where am I, what is here? pwd && ls -lah # Read the manual of any command (q = quit) man rsync # Ask your host to enable SSH, # then log in to your own server ssh user@yourserver.com ``` ---- ### Take this home
Every command from this talk, on one page
petermartin.nl/en/focus-on/linux
Note: Leave this slide up during the questions โ that is when people actually reach for their phone. If the venue lets you, put the printed cheat sheet in the conference bags as well. ---
Website broken...
---- ### Final exercise > Client calls: *"The site is broken
after the migration!"*
What do you check?
1. Can I get in?
ssh
2. Are the files where I expect them?
ls -lah
3. What do the logs say?
tail -f error.log
4. Ownership and permissions OK?
ls -l
+
chown
5. Did anything not arrive?
rsync --dry-run
Note: Let the audience shout answers before revealing the list. Every step uses a command from this talk โ that's the point: they already know the whole diagnostic workflow now. ---- ###
The ideas to take home
- 1. Everything is a file - 2. Small tools, pipe them together - 3. Ownership before permissions, never 777
Every Linux trick you'll ever learn
is one of these ideas in disguise.
---- ###
Your starter toolkit
`ssh` ยท `pwd` ยท `ls` ยท `cd` ยท `cat` ยท `nano` `grep` ยท `find` ยท `tail` ยท `chmod` ยท `chown` `rsync` ยท `mysqldump` ยท `dig` ยท `watch` ยท `docker`
**Sixteen commands. That was the whole talk.**
Note: This slide doubles as the cheat sheet โ photograph it. Point out that none of these commands is longer than six letters. ---
Questions?
Note: Common questions to prepare for: - "Which distro?" โ Ubuntu LTS on desktop/WSL; on the server, whatever your host runs. - "Is the terminal dangerous?" โ Only rm and anything with sudo. Read twice, press enter once. - "How do I learn more?" โ "The Linux Command Line" by William Shotts, free online.