diyhosting/maintenance.html

122 lines
6.1 KiB
HTML

<!DOCTYPE html>
<html lang=en>
<head>
<title>Maintaining a Server &ndash; diyhosting.bhh.sh</title>
<!--# include file=".nav.html" -->
</head>
<body>
<header><h1>Maintaining a Server</h1></header>
<nav></nav>
<main>
<p>Here are some important topics you should be familiar with whenever you are managing a server.</p>
<h2 id="update">Keep packages up to date.</h2>
<p>All GNU/Linux distributions use package managers to easily be able to install and update packages without manually downloading them.
On Debian, which we use here for these tutorial the package manager is <code>apt-get</code> or <code>apt</code> for short.
</p>
<p>
It's a good idea to use <code>apt</code> to keep your software reasonably up to date.
</p>
<pre><code>apt update
apt upgrade</code></pre>
<p>
Not only do up-to-date packages often come with more features, but they can also fix any possible security bugs.
</p>
<h2>Troubleshooting general problems</h2>
<p>
Often when you are installing something new, you might miss a step and run into an error, so it's important to know how to check and see what errors have happened on your computer.
</p>
<p>On Debian and other GNU/Linux distributions that use systemd (most of them), you can use the command <code>journalctl</code> to look at the system's general log.
You will probably want to run <code>journalctl -xe</code> as the <code>-x</code> and <code>-e</code> as that gives the most information and starts you at the bottom of the log to see the most recent errors.
</p>
<p>
Some programs do not use this system log, but have their own logs stored in <code>/var/log/</code>, or sometimes it's more convenient to look at a specific program's log to see only its issues.
</p>
<p>For example, we can see that in <code>/var/log/nginx/</code>, nginx produces both <code>error</code> and <code>access</code> files.
The <code>access</code> files show you all the times people connect to files on your server and much more.
We can look at the most recent errors by running:
</p>
<pre><code>tail -n 25 /var/log/nginx/error.log</code></pre>
<p>
The command <code>tail -n 25</code> means "show me the last 25 lines of this file."
You can replace that with <code>less</code> to browse the whole file.
In <code>less</code>, navigate with arrows or vim-keys and exit with <code>q</code>.
</p>
<h3>systemctl</h3>
<p>
Another tool on systemd distributions is <code>systemctl</code>.
At a basic level, use <code>systemctl status put-service-name-here</code>
to see if a system service is running and its most recent log.
But there's much more to <code>systemctl</code>.
</p>
<p>
For example, you can run <code>systemctl stop nginx</code> to stop NginX and <code>systemctl start nginx</code> to start it back up (or use <code>restart</code> for both).
When you make changes to a program's configuration files, <code>reload</code> well make them reload them.
If you no longer want a service to start when the system is rebooted, use <code>disable</code>, or conversely, to make a service start on reboot use <code>enable</code>.
</p>
<h2>Finding Files</h2>
<p>
Especially if you're new to how a GNU/Linux system is arranged, you might need help finding files.
To find program-related files, you can just use <code>whereis</code>:
</p>
<pre><code>$ whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx /usr/share/man/man8/nginx.8.gz</code></pre>
<p>This command lists the directories related to that program. For example, <code>/etc/nginx</code> is where the configuration files are and <code>/usr/share/nginx</code> is where the library and module-like files are.</p>
<p>But <code>whereis</code> can be used only with installed programs.
A more general tool is the pair of <code>updatedb</code> and <code>locate</code>.
</p>
<p>
<code>updatedb</code> is a command that quickly indexes every file and directory on your computer.
Then you can run <code>locate</code> to find a file containing a given name.
After running <code>updatedb</code>, try running <code>locate nginx</code> to find all files with "nginx" in their name.
</p>
<p>
You can make your search more specific by chaining other Unix commands through pipes.
For example, <code>grep</code> takes input and returns only lines that match an extra argument.
In the example below, we <code>locate</code> all files with "nginx" in the name, but we use <code>grep</code> to only show us those with the word "available" in them.
</p>
<pre><code>root@landchad:~# locate <strong>nginx</strong> | grep <strong>available</strong>
/etc/nginx/modules-available
/etc/nginx/sites-available
/etc/nginx/sites-available/default
/etc/nginx/sites-available/landchad
/usr/share/nginx/modules-available
/usr/share/nginx/modules-available/mod-http-auth-pam.conf
/usr/share/nginx/modules-available/mod-http-dav-ext.conf
/usr/share/nginx/modules-available/mod-http-echo.conf
/usr/share/nginx/modules-available/mod-http-geoip.conf
/usr/share/nginx/modules-available/mod-http-image-filter.conf
/usr/share/nginx/modules-available/mod-http-subs-filter.conf
/usr/share/nginx/modules-available/mod-http-upstream-fair.conf
/usr/share/nginx/modules-available/mod-http-xslt-filter.conf
/usr/share/nginx/modules-available/mod-mail.conf
/usr/share/nginx/modules-available/mod-stream.conf</code></pre>
<p>
<code>updatedb</code> is an ideal candidate for a <a href="cron.html">cronjob</a> so you don't have to worry about running each time.
For example, adding the following to your crontab will run <code>updatedb</code> every 30 minutes:
</p>
<pre><code>*/30 * * * * /usr/bin/updatedb</code></pre>
<p>
</p>
</main>
<!--# include file=".footer.html" -->
</body>
</html>