|
1 | 1 |  |
2 | 2 |
|
3 | | -`kelunik/acme-client` is an ACME client written in PHP. ACME is the protocol that powers the [Let's Encrypt](https://letsencrypt.org) certificate authority. |
| 3 | +`kelunik/acme-client` is a command-line ACME client implemented in PHP, enabling the issuance and renewal of certificates via the ACME protocol used by [Let's Encrypt](https://letsencrypt.org). It supports PHP 8.1+ with OpenSSL and runs on Unix-like systems and Windows. |
4 | 4 |
|
5 | | -## Requirements |
| 5 | +## Installation |
6 | 6 |
|
7 | | - * PHP 7.4+ with OpenSSL |
8 | | - * Works on Unix-like systems and Windows |
| 7 | +### Requirements |
9 | 8 |
|
10 | | -## Documentation |
| 9 | +* PHP 8.1+ with OpenSSL |
| 10 | +* Unix-like system or Windows |
11 | 11 |
|
12 | | - * [Installation](./doc/installation.md) |
13 | | - * [Usage](./doc/usage.md) |
14 | | - * [Migration guide for 0.1.x → 0.2.x](./doc/migrations/0.2.0.md) |
| 12 | +### Installation using PHAR |
| 13 | + |
| 14 | +This is the preferred installation method for usage on a production system. You can download `acme-client.phar` in the [release section](https://github.com/kelunik/acme-client/releases). |
| 15 | + |
| 16 | +#### Instructions |
| 17 | + |
| 18 | +```bash |
| 19 | +# Go to https://github.com/kelunik/acme-client/releases/latest |
| 20 | +# Download the latest release archive. |
| 21 | + |
| 22 | +# Make it executable. |
| 23 | +chmod +x acme-client.phar |
| 24 | + |
| 25 | +# Run it. |
| 26 | +./acme-client.phar |
| 27 | + |
| 28 | +# Or install it globally. |
| 29 | +mv ./acme-client.phar /usr/local/bin/acme-client |
| 30 | +acme-client |
| 31 | +``` |
| 32 | + |
| 33 | +If you want to update, just replace the old `.phar` with a new one. |
| 34 | + |
| 35 | +All commands require a `--storage` argument when using the PHAR. That's the path where your keys and certificates will be stored. |
| 36 | +On Unix you could use something like `--storage /etc/acme`. |
| 37 | + |
| 38 | +You can add a file named `acme-client.yml` next to the `.phar` with the two keys `storage` and `server`. |
| 39 | +These values will be used as default if you don't specify them, but you can still use another server by explicitly adding it as argument. |
| 40 | + |
| 41 | +```yml |
| 42 | +# Sample YAML configuration. |
| 43 | + |
| 44 | +# Storage directory for certificates and keys. |
| 45 | +storage: /etc/acme |
| 46 | + |
| 47 | +# Server to use. Available shortcuts: letsencrypt, letsencrypt:staging |
| 48 | +# You can also use full URLs to the directory resource of an ACME server |
| 49 | +server: letsencrypt |
| 50 | +``` |
| 51 | +
|
| 52 | +### Installation using Composer |
| 53 | +
|
| 54 | +If you plan to actively develop this client, you don't want the PHAR but install the dependencies using [Composer](https://getcomposer.org/). |
| 55 | +
|
| 56 | +#### Instructions |
| 57 | +
|
| 58 | +```bash |
| 59 | +# Clone repository |
| 60 | +git clone https://github.com/kelunik/acme-client && cd acme-client |
| 61 | + |
| 62 | +# Install dependencies |
| 63 | +composer install |
| 64 | +``` |
| 65 | + |
| 66 | +You can use `./bin/acme` as script instead of the PHAR. Please note, that all data will be stored in `./data` as long as you don't provide the `--storage` argument. |
| 67 | + |
| 68 | +## Usage |
| 69 | + |
| 70 | +The client stores your account keys, domain keys and certificates in a single directory. If you're using the PHAR, |
| 71 | +you usually configure the storage in the configuration file. If you're using it with Composer, all data is stored in `./data`. |
| 72 | + |
| 73 | +**Be sure to backup that directory regularly.** |
| 74 | + |
| 75 | +Before you can issue certificates, you have to register an account. You have to read and understand the terms of service |
| 76 | +of the certificate authority you're using. For the Let's Encrypt certificate authority, there's a |
| 77 | +[subscriber agreement](https://letsencrypt.org/repository/) you have to accept. |
| 78 | + |
| 79 | +By using this client you agree to any agreement and any further updates by continued usage. You're responsible to react |
| 80 | +to updates and stop the automation if you no longer agree with the terms of service. |
| 81 | + |
| 82 | +These usage instructions assume you have installed the client globally as a PHAR. If you are using the PHAR, |
| 83 | +but don't have it globally, replace `acme-client` with the location to your PHAR or add that path to your `$PATH` variable. |
| 84 | + |
| 85 | +### Configuration |
| 86 | + |
| 87 | +The client can be configured using a (global) configuration file. The client takes the first available of |
| 88 | +`./acme-client.yml` (if running as PHAR), `$HOME/.acme-client.yml`, `/etc/acme-client.yml` (if not on Windows). |
| 89 | + |
| 90 | +The configuration file has the following format: |
| 91 | + |
| 92 | +```yml |
| 93 | +# Storage directory for certificates and keys. |
| 94 | +storage: /etc/acme |
| 95 | + |
| 96 | +# Server to use. URL to the ACME directory. |
| 97 | +# "letsencrypt" and "letsencrypt:staging" are valid shortcuts. |
| 98 | +server: letsencrypt |
| 99 | + |
| 100 | +# E-mail to use for the setup. |
| 101 | +# This e-mail will receive expiration notices from Let's Encrypt. |
| 102 | +email: me@example.com |
| 103 | + |
| 104 | +# List of certificates to issue. |
| 105 | +certificates: |
| 106 | + # For each certificate, there are a few options. |
| 107 | + # |
| 108 | + # Required: paths |
| 109 | + # Optional: bits, user |
| 110 | + # |
| 111 | + # paths: Map of document roots to domains. Maps each path to one or multiple |
| 112 | + # domains. If one domain is given, it's automatically converted to an |
| 113 | + # array. The first domain will be the common name. |
| 114 | + # |
| 115 | + # The client will place a file into $path/.well-known/acme-challenge/ |
| 116 | + # to verify ownership to the CA |
| 117 | + # |
| 118 | + # bits: Number of bits for the domain private key |
| 119 | + # |
| 120 | + # user: User running the web server. Challenge files are world readable, |
| 121 | + # but some servers might require to be owner of files they serve. |
| 122 | + # |
| 123 | + # rekey: Regenerate certificate key pairs even if a key pair already exists. |
| 124 | + # |
| 125 | + - bits: 4096 |
| 126 | + rekey: true |
| 127 | + paths: |
| 128 | + /var/www/example: |
| 129 | + - example.org |
| 130 | + - www.example.org |
| 131 | + # You can have multiple certificate with different users and key options. |
| 132 | + - user: www-data |
| 133 | + paths: |
| 134 | + /var/www: example.org |
| 135 | +``` |
| 136 | +
|
| 137 | +All configuration keys are optional and can be passed as arguments directly (except for `certificates` when using `acme-client auto`). |
| 138 | + |
| 139 | +Before you can issue certificates, you must create an account using `acme-client setup --agree-terms`. |
| 140 | + |
| 141 | +### Certificate Issuance |
| 142 | + |
| 143 | +You can use `acme-client auto` to issue certificates and renew them if necessary. It uses the configuration file to |
| 144 | +determine the certificates to request. It will store certificates in the configured storage in a sub directory called `./certs`. |
| 145 | + |
| 146 | +If everything has been successful, you'll see a message for each issued certificate. If nothing has to be renewed, |
| 147 | +the script will be quiet to be cron friendly. If an error occurs, the script will dump all available information. |
| 148 | + |
| 149 | +You should execute `acme-client auto` as a daily cron. It's recommended to setup e-mail notifications for all output of |
| 150 | +that script. |
| 151 | + |
| 152 | +Create a new script, e.g. in `/usr/local/bin/acme-renew`. The `PATH` might need to be modified to suit your system. |
| 153 | + |
| 154 | +```bash |
| 155 | +#!/usr/bin/env bash |
| 156 | +
|
| 157 | +export PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin' |
| 158 | +
|
| 159 | +acme-client auto |
| 160 | +
|
| 161 | +RC=$? |
| 162 | +
|
| 163 | +if [ $RC = 4 ] || [ $RC = 5 ]; then |
| 164 | + service nginx reload |
| 165 | +fi |
| 166 | +``` |
| 167 | + |
| 168 | +```sh |
| 169 | +# Cron Job Configuration |
| 170 | +0 0 * * * /usr/local/bin/acme-renew |
| 171 | +``` |
| 172 | + |
| 173 | +| Exit Code | Description | |
| 174 | +|-----------|-------------| |
| 175 | +| 0 | Nothing to do, all certificates still valid. | |
| 176 | +| 1 | Config file invalid. | |
| 177 | +| 2 | Issue during account setup. | |
| 178 | +| 3 | Error during issuance. | |
| 179 | +| 4 | Error during issuance, but some certificates could be renewed. | |
| 180 | +| 5 | Everything fine, new certificates have been issued. | |
| 181 | + |
| 182 | +Exit codes `4` and `5` usually need a server reload, to reload the new certificates. It's already handled in the recommended |
| 183 | +cron setup. |
| 184 | + |
| 185 | +If you want a more fine grained control or revoke certificates, you can have a look at the [advanced usage](./advanced-usage.md) document. The client allows to handle setup / issuance / revocation and other commands |
| 186 | +separately from `acme-client auto`. |
| 187 | + |
| 188 | +## Advanced Usage |
| 189 | + |
| 190 | +Most users should use the `auto` command described above. |
| 191 | + |
| 192 | +### Register an Account |
| 193 | + |
| 194 | +``` |
| 195 | +acme-client setup --agree-terms --email me@example.com |
| 196 | +``` |
| 197 | +
|
| 198 | +After a successful registration you're able to issue certificates. |
| 199 | +This client assumes you have a HTTP server setup and running. |
| 200 | +You must have a document root setup in order to use this client. |
| 201 | +
|
| 202 | +### Issue a Certificate |
| 203 | +
|
| 204 | +``` |
| 205 | +acme-client issue -d example.com:www.example.com -p /var/www/example.com |
| 206 | +``` |
| 207 | +
|
| 208 | +You can separate multiple domains (`-d`) with `,`, `:` or `;`. You can separate multiple document roots (`-p`) with your system's path separator: |
| 209 | +* Colon (`:`) for Unix |
| 210 | +* Semicolon (`;`) for Windows |
| 211 | +
|
| 212 | +If you specify less paths than domains, the last one will be used for the remaining domains. |
| 213 | +
|
| 214 | +Please note that Let's Encrypt has rate limits. Currently it's five certificates per domain per seven days. If you combine multiple subdomains in a single certificate, they count as just one certificate. If you just want to test things out, you can use their staging server, which has way higher rate limits by appending `--server letsencrypt:staging`. |
| 215 | +
|
| 216 | +### Revoke a Certificate |
| 217 | +
|
| 218 | +To revoke a certificate, you need a valid account key, just like for issuance. |
| 219 | +
|
| 220 | +``` |
| 221 | +acme-client revoke --name example.com |
| 222 | +``` |
| 223 | +
|
| 224 | +`--name` is the common name of the certificate that you want to revoke. |
| 225 | +
|
| 226 | +### Renew a Certificate |
| 227 | +
|
| 228 | +For renewal, there's the `acme-client check` subcommand. |
| 229 | +It exists with a non-zero exit code, if the certificate is going to expire soon. |
| 230 | +Default check time is 30 days, but you can use `--ttl` to customize it. |
| 231 | +
|
| 232 | +You may use this as daily cron: |
| 233 | +
|
| 234 | +``` |
| 235 | +acme-client check --name example.com || acme-client issue ... |
| 236 | +``` |
| 237 | +
|
| 238 | +You can also use a more advanced script to automatically reload the server as well. For this example we assume you're using Nginx. |
| 239 | +Something similar should work for Apache. But usually you shouldn't need any script, see [basic usage](./usage.md). |
| 240 | +
|
| 241 | +```bash |
| 242 | +#!/usr/bin/env bash |
| 243 | +
|
| 244 | +acme-client check --name example.com --ttl 30 |
| 245 | +
|
| 246 | +if [ $? -eq 1 ]; then |
| 247 | + acme-client issue -d example.com:www.example.com -p /var/www |
| 248 | +
|
| 249 | + if [ $? -eq 0 ]; then |
| 250 | + nginx -t -q |
| 251 | +
|
| 252 | + if [ $? -eq 0 ]; then |
| 253 | + nginx -s reload |
| 254 | + fi |
| 255 | + fi |
| 256 | +fi |
| 257 | +``` |
0 commit comments