Linux commands every Web developer needs to know.

Getting oriented

Determining the installed version of Apache, PHP & MySQL

apache2 -v
php -v
mysql -p -e 'select version()'

Determining if Apache, .. is running .

ps aux | grep http

Updating the MySQL root password.

sudo /etc/init.d/mysql stop
sudo mysqld_safe --skip-grant-tables &
mysql -u root
USE mysql;
UPDATE user SET password=PASSWORD("newpassword") WHERE user='root';
flush privileges;
quit;
sudo /etc/init.d/mysqld start
mysql -u root -p

Keys & Permissions

Creating encryption keys

ssh-keygen -t rsa

Uploading your public encryption key to the web server (two ways)

ssh-copy-id [email protected]
cat ~/.ssh/id_rsa.pub | ssh [email protected] "mkdir -p ~/.ssh && cat >>  ~/.ssh/authorized_keys"

Logging into a server using SSH

ssh -p port-number  [email protected]

Adding "super" permissions to a user

adduser new-ssh-user
usermod -aG sudo  your-ssh-user-name

Allowing access to a service (e.g apache) through the firewall

sudo ufw app list
sudo ufw app info "Apache Full"
sudo ufw allow in "Apache Full"

Database

Connecting to a MySQL database

mysql -h your-database-host-name -u your-database-user-name -p

Exporting from a MySQL database

mysqldump -h your-database-host-name -u your-database-user-name  -p your-database-name | gzip -9 > your-export-name.sql.gz

Importing to a database

gunzip < your-export-name.sql.gz | mysql -u your-database-user-name -p your-database-name
mysql -u your-database-user-name -p your-database-name <  your-export-name.sql

Files & Directories

Taking a backup of the web root directory

tar  --exclude='/path/to/excluded/directory' -cf - /path/to/web/root/directory | gzip > backup.tar.gz

Downloading files from another web server

scp -r [email protected]:/path/to/web/root/directory .

Synchronizing your files directory on the development server with the files directory on the production server

rsync -azP [email protected]:/path/to/web/files/directory  /path/to/local/directory/on/development/server
rsync -a --delete [email protected]:/path/to/web/files/directory  /path/to/local/directory/on/development/server

Free disk space ?

df -h .

Size of a directory

du -sh

Largest files and directories (under a directory)

du -a /path/to/directory/ | sort -n -r | head -n 10

Searching for files (e.g tar.gz) with sizes

find / -name *.tar.gz -exec du -sh {} \;

List all files modified in the last 7 days

find ./ -type f -mtime -7

Number of lines that match a given search query

grep -r something /path/to/directory | grep something-else | wc -l
zgrep something  /path/to/gzip/file | wc -l

Monitoring

Monitoring log files

tail -f /var/log/syslog /var/log/messages
tail -n 10 /var/log/syslog

IP addresses assigned to a web server

ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

Git

Show all modified files.

git status

Reversing a git commit that has not been pushed.

git reset --soft HEAD~1

Reversing a git commit that has been pushed.

git reset --hard HEAD~1
git push --force origin master

If you need Website support for your Website or Web server: please send me an email [email protected], and let me know what services you need.