Scroll To Top

Apache

a2enmod

The a2enmod command (or rather script) allows you to install modules for Apache.

To install a module using a2enmod, type:

sudo a2enmod modulename

Then restart Apache for this to take effect:

sudo service apache2 restart

Here are a few common module names: expires, php5, rewrite, ssl

To see which modules are currently installed and enabled, type:

apache2ctl -M Source
Troubleshooting:
  • How to enable expires header in Apache

To enable the expires header in Apache you need to type:

sudo a2enmod expires

You then need to go into the virtual host file for each of your websites and add the following for each element you want to apply the header to:

ExpiresActive On ExpiresByType [type]/[extension] "access plus [duration] [scale]"

Possible values:

Type: text|application|image Extension: css/jpeg/png/gif/ico/x-icon/javascript Duration: number(int) Scale: years/months/weeks/days/hours/minutes/seconds

You can then optionally set the default cache time to 1 day for all other content by adding the following just after the 'ExpiresByType' lines:

ExpiresDefault "access plus 1 days"

Example use in your virtual host file:

ExpiresActive On
ExpiresByType image/gif "access plus 1 months"
ExpiresByType image/jpg "access plus 1 months"
ExpiresByType image/jpeg "access plus 1 months"
ExpiresByType image/png "access plus 1 months"
ExpiresByType image/vnd.microsoft.icon "access plus 1 months"
ExpiresByType image/x-icon "access plus 1 months"
ExpiresByType image/ico "access plus 1 months"
ExpiresByType application/javascript "now plus 1 months"
ExpiresByType application/x-javascript "now plus 1 months"
ExpiresByType text/javascript "now plus 1 months"
ExpiresByType text/css "now plus 1 years"
ExpiresDefault "access plus 1 days"

You then need to restart Apache to apply these changes, to do so type:

sudo service apache2 restart Source
a2ensite

The a2ensite command (or rather script), allows you to enable a website in Apache's configuration. It does this by creating symlinks within /etc/apache2/sites-enabled.

In Apache websites are loaded in alphanumerical order, this means numbers are loaded in numerical order and then followed by letters. So 000-a would be loaded before 001-a.conf, as 000-a.conf would be loaded before 000-b.conf. This is important to keep in mind especially if you're enabling config for a subdomain, as the subdomain has to be loaded before the domain for it to work.

So you could use 000-x.example.com.conf and example.com.conf, and things would be loaded in the correct order; whereas x.example.com.conf and example.com.conf would not, as the x would be loaded after the d.

To enable a website, type:

sudo a2ensite example.com.conf Source
Troubleshooting:
  • How To Configure A Name-Based Virtual Host

To add a name-based virtual host, you first need to disable the default Apache virtual host by typing:

sudo a2dissite *default

Then you need to decide where you want your website files to be. We suggest storing things in your user directory, so under /home/user/www, as this will allow you full access to files within it without using sudo; so you can set up FTP easily. Although it's also possible to use the default /var/www directory. This tutorial will assume you're using our recommendation, but will also work for /var/www (just substitute where necessary).

So first you need to move to your user directory:

cd /home/username

Then you need to make a folder to store everything, we'll call it www, but the name doesn't really matter:

mkdir www

Then move to this folder:

cd www

Now you need to make a few files to store things website-related. We'll make these within a folder called 'example.com' so we can support multiple domains later on.

Here are the folders you'll need:

mkdir -p example.com/public mkdir -p example.com/private mkdir -p example.com/log

Other useful folders might be (but aren't required):

mkdir -p example.com/backups (for website backups) mkdir -p example.com/lib (for 3rd party libraries)

Next you have to create the virtual host file for your website by typing:

sudo nano /etc/apache2/sites-available/example.com.conf (or example.co.uk.conf, etc.)

One thing to note is that on newer versions of Apache (2.4 onwards) you must have the .conf at the end of this file.

Now we're going to add some config settings inside this new file. Replace 'example.com' in the below example to match your domain.

<VirtualHost *:80>
  ServerAdmin webmaster@example.com
  ServerName example.com
  ServerAlias www.example.com
  DocumentRoot /home/simon/www/example.com/public
  ErrorLog /home/simon/www/example.com/log/error.log
  CustomLog /home/simon/www/example.com/log/access.log combined

  #Things like expires/rewrite config go here (both explained under a2enmod troubleshooting).
</VirtualHost>

Save this by typing Ctrl+C and then press Y.

To enable this we now use the a2ensite command, so type:

sudo a2ensite example.com.conf

This creates a symbolic link to your example.com.conf virtual host file.

The final step is to restart Apache:

sudo service apache2 restart

Following these same steps you can run as many websites as you like on the same server.

Source
a2dismod

The a2dismod command (or rather script), allows you to disable a module in Apache's configuration.

To disable a module, type:

sudo a2dismod modulename

Then restart Apache for this to take effect:

sudo service apache2 restart Source
a2dissite

The a2dissite command (or rather script), allows you to disable a website in Apache's configuration.

To disable a site, type:

sudo a2dissite example.com.conf

Then restart Apache:

sudo service apache2 restart

Note that this removes the symlink in Apache, but doesn't delete the actual file. So you can disable a website without losing all your config.

If you'd like to then delete this file, just type:

sudo rm example.com.conf Source

Delete Files Or Directories

rm

The rm command can be used to delete files & folders. By default with no arguments it will delete a single file that you target, and with arguments can be used to delete an entire folder. By default the target file is relative to your current directory, you can specify an absolute path by appending the file/folder with /.

An important note is that if you append the target with /, it will delete relative to your users base directory - so be very careful to never delete / on its own by accident (as it has the potential to delete your whole server). A good rule of thumb is to only delete things relative to your current directory!

To delete a single file, type:

rm filename

To delete a folder recursively, type:

rm -rf foldername

The arguments used here are:

r tells it to delete all directories and their contents recursively f tells it to ignore non-existent files (without this it may interrupt the deletion to prompt you) Source

Directory Navigation

cd

The cd command allows you to change your current directory. You can use this command relative to where you currently are or relative to the root directory.

To change directory locally, type:

cd folder1/folder2

To change directory relative to the root directory, type:

cd /folder1/folder2

Both uses have their advantages which you'll learn as you use them.

To go up a level, type:

cd ..

To go to your home directory, type:

cd ~ Source
ls

The ls command allows you to list the contents of your current directory. By default results are sorted alphabetically, but they can be filtered in a variety of ways.

To list the contents of your current directory, type:

ls

Here are a few useful filters for sorting results:

Sort by file size (only applicable to files, not folders): ls -S Sort by modification time: ls -t List one file per line: ls -1 Source

File Compression

tar

There are two widely used commands for compressing files on Linux - zip and tar. tar is the most widely used because the files it produces when combined with gzip compression are slightly smaller than what zip would produce, and is generally more widely used and considered more compatable on Linux.

To start with either of these, first install the required packages using:

sudo apt-get install zip gzip tar

Then to compress a file quickly type:

zip -r "targetfile.zip" foldertobezipped

The -r tells it to recursively select all the files in the foldertobezipped folder, alternatively you can target a specific file without this flag.

Then to uncompress it type:

unzip "targetfile.zip"

To compress using tar (and so with the slightly better compression and compatability), type:

tar cvzf "targetfile.tar.gz" foldertobezipped

And then to uncompress type:

tar xvzf "targetfile.tar.gz"

There are several flags being used in this tar command:

c means to "create" a new compressed file x means to "extract" a compressed file v means to be "verbose" z means to use (GNU)zip for compression f means the next argument is declaring the compressed file name (as we're using gzip we append the end with the .tar.gz extension) Source

File Permissions

chmod

The chmod command allows you to set the permissions for a file. This means limiting access as to who can read/write to that file.

The general syntax for permissions is nnn, each n standing for a number. Example use:

chmod nnn filename

When using chmod, you need to be aware that there are three types of Linux users that you are setting permissions for. Therefore, when setting permissions, you're assigning them for yourself as the server administrator, your group and everyone else in the world. These users are technically know as 'owner', 'group' and 'world'.

Think of the syntax like so:

chmod owner-perm group-perm world-perm filename

Now that you understand the three levels of permissions you need to take into account, here are how to use the permissions themselves.

There are three types of permissions that Linux allows for each file; read, write and execute. Each of these are represented by one of the numbers mentioned (n).

The numeric equivalents for each of these are 4 - read, 2 - write, 1 - execute and 0 - block all access.

Here are a few examples:

Readable by server only: chmod 400 filename Readable & writable by the server and readable by the group: chmod 640 filename Readable, writable & executable by the server and readable by both the group and the rest of the world: chmod 744 filename Readable, writable & executable by everyone; never do this: chmod 777 filename Source
chown

A file's owner and group can be changed using the chown command.

To change the owner of a file, type:

sudo chown username filename

Be very careful that you change the ownership on the correct file and to the correct user. Mistakes made with this command can have serious consequences and often can't be undone.

A useful user to know is 'www-data', which is usually (not always) the user under which Apache runs. This means things like PHP scripts can be given this user permission to work correctly.

You can also change a file's group by typing:

sudo chgrp groupname filename

or:

sudo chown :groupname filename

Or change both the file's owner and group in one command using:

sudo chown username:groupname filename Source
Troubleshooting:
  • Allow Image Uploading On Website

Assuming the user 'www-data' on your server is used by Apache, you simply need to change the ownership of the folder where images are uploaded to and then set its permissions.

To set ownership to Apache, type:

chown www-data foldername

Then to make the folder writable to your server (Apache) and readable by users, type:

chmod 755 foldername Source

Installing & Updating Programs

apt-get

The apt-get command allows you to install new programs (packages) to your server. APT (Advanced Packaging Tool) is standardised across most lunux distributions for installing packages; although refer to yum if you're using CentOS, as apt-get isn't installed by default.

To install a package using apt-get, type:

apt-get install packagename

apt-get is also capable of installing multiple packages at once, example usage:

apt-get install packagename1 packagename2 packagename3

To update all packages currently installed, type:

sudo apt-get update sudo apt-get upgrade --show-upgraded

To remove a package but leave configuration files, type:

apt-get remove packagename

To remove a package as well as associated configuration files, type:

apt-get purge packagename Source
Troubleshooting:
  • Getting Message Saying 'Packages Have Been Kept Back'

If you get this message, all it means is that the dependencies have changed for one or more of the packages you have installed; and those new dependencies need to be installed.

To update all packages that you get this message for, type:

sudo apt-get dist-upgrade

Alternatively; to update them individually, type:

sudo apt-get install packagename dependencypackagename

This will force install the new dependencies and then the newer packages.

Source
yum

The yum (Yellowdog Updater Modified) is a package management solution for linux systems running CentOS, Red Hat or Fedora.

It can be used to search for, install/remove and check for additional information about packages available. It's the CentOS/Red Hat counterpart of apt-get on Ubuntu/Debian.

To install a package using yum, type:

sudo yum install packagename

yum can also be used to install multiple packages at once using:

sudo yum install packagename1 packagename2 packagename3

To check if updates are available for any packages currently installed:

sudo yum check-update

To update all packages currently installed, type:

sudo yum update

To update all packages and remove obsolete packages, type:

sudo yum upgrade

Use this with caution, as removing obsolete packages can be risky (there's a chance that it might remove one that you're using).

To remove a package and its dependencies, type:

sudo yum remove packagename Source

Monitoring

top

The top command allows you to view a dynamic real-time list of processes running on a server. By default, it displays the most CPU-intensive tasks running on the server and updates the list every five seconds.

To view the list of processes running on your server, type:

top

To close the view, type Ctrl+C on your keyboard.

Source
uptime

The uptime command can be used to see how long a server has been running, the current time, how many users are currently logged on and the system load averages for the past 1, 5, and 15 minutes.

To view these stats, type:

uptime Source

Restart

reboot

The reboot command can be used to restart your server. If using a physical server rather than a cloud server beware that this is risky, as occasionally they won't reboot and you may need to get it started up again by your host.

To restart your server, type:

sudo reboot

By default some distributions don't require root for this, if this is the case be aware that any new users likely also have this ability.

Source

Starting & Stopping Programs

service

To start or stop software (services) running on your server, you can use the service command. Although the new and preferred way to stop start and restart services is through /etc/init.d; this is explained below.

To start a service, type:

sudo service servicename start

To stop a service, type:

sudo service servicename stop

These commands will work for all services, but it's always good to use the most modern practices. The new way to start and stop services is to use /etc/init.d. Keep in mind that this new method won't work for all services, although it will for most.

To start a service using /etc/init.d, type:

sudo /etc/init.d/servicename start

To stop a service using /etc/init.d, type:

sudo /etc/init.d/servicename stop

To see what services use an init.d script (will work with it), type:

sudo sysv-rc-conf

Here are a few common service names: apache2, mysql

Source

Text Editors

nano

GNU nano is a popular command line text editor for Unix-based systems and BSD variants. It's a popular editor for users who may find vi or emacs commands to be non-intuitive.

Nano is included with many Linux distributions by default, but some users may need to install it through their distribution’s package management tool.

To install nano on Debian/Ubuntu, type:

sudo apt-get install nano

To install nano on CentOS/Fedora, type:

sudo yum install nano

To open a file in nano, type:

sudo nano folder/file.txt

This same command can be used to create a blank file. Just type the same command, but for a file that doesn't exist, and a blank file will be created and opened to be edited.

To open a file at a specific line or column number, type:

sudo nano +LINE,COLUMN folder/file.txt

Or just:

sudo nano +LINE folder/file.txt

To open a file as read only, type:

sudo nano -v folder/file.txt

When editing files used to configure applications or system utilities, it's important to disable line wrapping when opening. This stops nano wrapping lines that are too long to fit on your screen (which can create problems if config directives are saved across multiple lines). To do this, type

sudo nano -w folder/file.txt

By default, nano won't save anything unless you tell it to. So to save your work, type Ctrl+O, or type Ctrl+X to exit, and follow the on-screen prompt to save.

Source
vi

Vim (Vi IMproved) is a text editor that is upwards compatible to Vi. It can be used to edit all kinds of plain text. It is especially useful for editing config files and programs written in shell, python, perl, c/c++ and more.

To install vim, type:

sudo apt-get install vim

To open a file using vim, type:

sudo vi filename Source

Time Synchronisation

ntp

The ntp daemon (ntpd) is currently the best way to keep your server clock in 'sync'. It constantly adjusts your server clock to stop it drifting. The extra processing and memory used by ntpd is negligible, so you don't need to worry about it.

To install ntpd, type:

sudo apt-get install ntp

On installation, ntpd is enabled automatically.

Source
Troubleshooting:

To correct ntp if the server clock goes out of sync, type the following commands:

sudo service ntp stop sudo ntpd -gq sudo service ntp start

The -gq tells the ntp daemon to correct the time regardless of the offset (g) and exit immediately (q).

Source

ntp uses a remote machine to retrieve the correct date and time.

To change the machine used for this, type:

sudo nano /etc/ntp.conf

Modify the line starting with 'server' to you desired configuration. For example, to use Google ntp servers, modify this to:

server time1.google.com

Save this by typing Ctrl+C and then press Y.

Now any changes to ntp will use this server.

Source

Time Zones

tzdata

When you first setup your server, its time zone will likely be set to UTC (Coordinated Universal Time). So all the timestamps for things running on it are in UTC. If you're not working in this time-zone, then working with it can be a real nuisance. tzdata allows you to change this time zone to something more appropriate to where your servers are geographically.

To check your current time zone, type:

date

To change your time zone, type:

sudo dpkg-reconfigure tzdata

Then follow the on screen instructions.

If you have cron jobs running, be sure to restart them so they run on this new time zone. To do so, type:

/etc/init.d/cron stop /etc/init.d/cron start Source

User Permissions

root

In Linux, there is a 'superuser' named root. The superuser can do anything and everything, so doing daily work as the superuser can be dangerous. You could type a command incorrectly and destroy your system. Ideally, you run as a user that has only the privileges needed for the task at hand. In some cases root is required, but most of the time only a regular user is needed.

To create a new user, type:

adduser username

Enter a secure password where prompted, and be sure to make a note of it somewhere!

To allow this user administrator (root) access, type:

usermod -a -G sudo username

You're now able to login as this new user and run commands as 'root' when needed.

Source
sudo

Recommended use on Linux is to use a regular user, but when needed allow it to run commands with root-level privileges. This can be done using the 'sudo' command.

To run a command as root, type:

sudo command

When running a command using 'sudo', you'll have to re-enter the password for the user you're currently using. This will then last 15 minutes before you have to re-enter it.

Source