|
| 1 | +--- |
| 2 | +engine: knitr |
| 3 | +title: Application Administration |
| 4 | +--- |
| 5 | + |
| 6 | +## Learning objectives |
| 7 | + |
| 8 | +- How to install packages |
| 9 | +- Where to install packages |
| 10 | +- How to run apps as services |
| 11 | + |
| 12 | + |
| 13 | +## Linux app install and upgrade |
| 14 | + |
| 15 | +::: {.panel-tabset} |
| 16 | + |
| 17 | +### Package manager |
| 18 | + |
| 19 | +1. Update package index |
| 20 | + |
| 21 | +```bash |
| 22 | +sudo apt-get update |
| 23 | +``` |
| 24 | + |
| 25 | +2. Find the package name |
| 26 | + |
| 27 | +```bash |
| 28 | +# apt search {keyword} |
| 29 | +apt search "^r-.*" | sort |
| 30 | +``` |
| 31 | + |
| 32 | +3. Install target package: |
| 33 | + |
| 34 | +```bash |
| 35 | +# sudo apt-get install {package-name} |
| 36 | +# note: not sure which is better `apt` or `apt-get` |
| 37 | +sudo apt install r-base r-base-dev |
| 38 | +``` |
| 39 | + |
| 40 | +### Side-load |
| 41 | + |
| 42 | +1. Download |
| 43 | + |
| 44 | +```bash |
| 45 | +# with curl, download the right Debian package |
| 46 | +export R_VERSION=4.5.2 |
| 47 | +curl -O https://cdn.posit.co/r/ubuntu-2404/pkgs/r-${R_VERSION}_1_$(dpkg --print-architecture).deb |
| 48 | +``` |
| 49 | + |
| 50 | +2. Install |
| 51 | + |
| 52 | +```bash |
| 53 | +# install the Debian package |
| 54 | +sudo apt-get install ./r-${R_VERSION}_1_$(dpkg --print-architecture).deb |
| 55 | +``` |
| 56 | + |
| 57 | +Source: Posit Docs [here](https://docs.posit.co/resources/install-r.html#download-and-install-r) |
| 58 | + |
| 59 | + |
| 60 | +::: |
| 61 | + |
| 62 | +## Configure application |
| 63 | + |
| 64 | +- Read the app's docs |
| 65 | +- Create or modify the file |
| 66 | + |
| 67 | +```bash |
| 68 | +# create a tmux configuration file |
| 69 | +cd ~ |
| 70 | +touch .tmux.conf |
| 71 | + |
| 72 | +# edit it |
| 73 | +vim .tmux.conf |
| 74 | +``` |
| 75 | + |
| 76 | +## Where to find application files |
| 77 | + |
| 78 | +- Read the app's docs |
| 79 | +- Search in some common places (from the book) |
| 80 | + - `/bin`, `/opt`, `/usr/local`, `/usr/bin` – installation locations for software. |
| 81 | + - `/etc` – configuration files for applications. |
| 82 | + - `/var` – variable data, most commonly log files in `/var/log` or `/var/lib`. |
| 83 | +- Some other places: |
| 84 | + - `~` in a dotfile (e.g., `.tmux`) |
| 85 | + - `~/.config/` |
| 86 | + |
| 87 | +## Edit configuration files |
| 88 | + |
| 89 | +- Read the app's docs |
| 90 | +- Use an editor on the server: |
| 91 | + - Nano |
| 92 | + - Vi(m) |
| 93 | + |
| 94 | +## Read logs |
| 95 | + |
| 96 | +::: {.panel-tabset} |
| 97 | + |
| 98 | +### Find them |
| 99 | + |
| 100 | +From the book: |
| 101 | + |
| 102 | +> Most applications write their logs somewhere inside the `/var` directory. Some activities will get logged to the main log at `/var/log/syslog`. Other things may get logged to `/var/log/<application name>` or `/var/lib/<application name>`. |
| 103 | +
|
| 104 | +### Read them |
| 105 | + |
| 106 | +Print: |
| 107 | + |
| 108 | +- Whole log at once: `cat` |
| 109 | +- Few lines at time: `less` |
| 110 | +- First few lines: `head` |
| 111 | +- Last few lines: `tail` |
| 112 | + |
| 113 | +```bash |
| 114 | +# print lines as they are written to the log |
| 115 | +tail my_file -f |
| 116 | +``` |
| 117 | + |
| 118 | +### Search them |
| 119 | + |
| 120 | +- Filter lines with regex patterns: `grep` |
| 121 | +- Filter lines with conditions: `awk` |
| 122 | + |
| 123 | +::: |
| 124 | + |
| 125 | +## Running the right commands |
| 126 | + |
| 127 | +::: {.panel-tabset} |
| 128 | + |
| 129 | +### Where is binary |
| 130 | + |
| 131 | +Either you know (because you put it here) (e.g. `/opt/python3`) |
| 132 | + |
| 133 | +Or you can find out |
| 134 | + |
| 135 | +```bash |
| 136 | +which python3 |
| 137 | +# /usr/bin/python3 |
| 138 | +``` |
| 139 | + |
| 140 | +### Whether on path |
| 141 | + |
| 142 | +Applications need |
| 143 | +To run in the the terminal, either: |
| 144 | + |
| 145 | +- Specify the path |
| 146 | +- Or it is on PATH |
| 147 | + |
| 148 | +To see PATH, print it: |
| 149 | + |
| 150 | +```bash |
| 151 | +echo $PATH |
| 152 | +``` |
| 153 | + |
| 154 | +### How to put on path |
| 155 | + |
| 156 | +Option 1: append app's path to PATH |
| 157 | + |
| 158 | +```bash |
| 159 | +# edit and save `.bashrc` to add your new path |
| 160 | +export PATH=/my/app/path:$PATH |
| 161 | +``` |
| 162 | + |
| 163 | +Option 2: create symlink from app location to an on-PATH place |
| 164 | + |
| 165 | +```bash |
| 166 | +# create a (soft) symlink from installation in `opt` to a place on PATH |
| 167 | +sudo ln -s /opt/R/${R_VERSION}/bin/R /usr/local/bin/R |
| 168 | +sudo ln -s /opt/R/${R_VERSION}/bin/Rscript /usr/local/bin/Rscript |
| 169 | +``` |
| 170 | + |
| 171 | +Source: Posit Docs [here](https://docs.posit.co/resources/install-r.html#create-a-symlink-to-r) |
| 172 | + |
| 173 | +::: |
| 174 | + |
| 175 | +## Running apps as services |
| 176 | + |
| 177 | +::: {.panel-tabset} |
| 178 | + |
| 179 | +### Create a service file |
| 180 | + |
| 181 | +:::: {.columns} |
| 182 | + |
| 183 | +::: {.column width="50%"} |
| 184 | + |
| 185 | +1. Create file |
| 186 | + |
| 187 | +```bash |
| 188 | +# move to directory where startup services |
| 189 | +cd /etc/systemd/system/ |
| 190 | + |
| 191 | +# create a service file |
| 192 | +touch my_app.service |
| 193 | +``` |
| 194 | + |
| 195 | +::: |
| 196 | + |
| 197 | +::: {.column width="50%"} |
| 198 | + |
| 199 | +2. Populate the contents |
| 200 | + |
| 201 | +``` |
| 202 | +[Unit] |
| 203 | +Description=My Application Service |
| 204 | +After=network.target # Or other dependencies |
| 205 | +
|
| 206 | +[Service] |
| 207 | +ExecStart=/path/to/your/application/executable # Or a script that runs it |
| 208 | +WorkingDirectory=/path/to/your/application/directory |
| 209 | +Restart=always |
| 210 | +User=your_username # Optional, run as a specific user |
| 211 | +Group=your_group # Optional, run as a specific group |
| 212 | +
|
| 213 | +[Install] |
| 214 | +WantedBy=multi-user.target |
| 215 | +``` |
| 216 | + |
| 217 | +::: |
| 218 | + |
| 219 | +:::: |
| 220 | + |
| 221 | +### Enable the service |
| 222 | + |
| 223 | +Reload configurations: |
| 224 | + |
| 225 | +```bash |
| 226 | +sudo systemctl daemon-reload |
| 227 | +``` |
| 228 | + |
| 229 | +Enable your service |
| 230 | + |
| 231 | +```bash |
| 232 | +sudo systemctl enable my_app.service |
| 233 | +``` |
| 234 | + |
| 235 | +Start the service |
| 236 | + |
| 237 | +```bash |
| 238 | +sudo systemctl start my_app.service |
| 239 | +``` |
| 240 | + |
| 241 | +::: |
0 commit comments