2022-01-16 18:20:39 +00:00
|
|
|
---
|
|
|
|
title: "nginx"
|
2022-01-26 22:35:07 +00:00
|
|
|
tags: [ "Documentation", "Networking" ]
|
2022-01-16 18:20:39 +00:00
|
|
|
---
|
2021-02-09 03:00:52 +00:00
|
|
|
Install nginx:
|
|
|
|
|
2023-06-17 19:28:20 +00:00
|
|
|
```bash
|
|
|
|
sudo apt-get install nginx
|
|
|
|
```
|
2021-02-09 03:00:52 +00:00
|
|
|
|
2023-06-17 19:28:20 +00:00
|
|
|
```bash
|
|
|
|
sudo apt-get enable --now nginx
|
|
|
|
```
|
2021-02-09 03:00:52 +00:00
|
|
|
|
|
|
|
Put a website somewhere:
|
|
|
|
|
2023-06-17 19:28:20 +00:00
|
|
|
```bash
|
|
|
|
mkdir /var/www/html/mysite/
|
|
|
|
```
|
2021-02-09 03:00:52 +00:00
|
|
|
|
|
|
|
Put an index file there:
|
|
|
|
|
2023-06-17 19:28:20 +00:00
|
|
|
```bash
|
|
|
|
vim /var/www/html/mysite/index.html
|
|
|
|
```
|
2021-02-09 03:00:52 +00:00
|
|
|
|
|
|
|
Make the owner `www-data`
|
|
|
|
|
2023-06-17 19:28:20 +00:00
|
|
|
```bash
|
|
|
|
chown -R www-data:www-data /var/www/html/mysite/
|
|
|
|
```
|
2021-02-09 03:00:52 +00:00
|
|
|
|
|
|
|
Make a configuration file for nginx:
|
|
|
|
|
2023-06-17 19:28:20 +00:00
|
|
|
```bash
|
|
|
|
vim /etc/nginx/sites-available/mysite.conf
|
|
|
|
```
|
2021-02-09 03:00:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
server {
|
|
|
|
listen 80;
|
|
|
|
listen [::]:80;
|
|
|
|
root /var/www/html/mysite;
|
|
|
|
index index.html index.htm;
|
|
|
|
server_name mysite.tk;
|
|
|
|
|
|
|
|
location / {
|
|
|
|
try_files $uri $uri/ =404;
|
|
|
|
}
|
|
|
|
|
2023-06-17 19:28:20 +00:00
|
|
|
}
|
|
|
|
```
|
2021-02-09 03:00:52 +00:00
|
|
|
|
|
|
|
Make the site available:
|
|
|
|
|
2023-06-17 19:28:20 +00:00
|
|
|
```bash
|
|
|
|
ln -s /etc/nginx/sites-available/mysite.conf /etc/nginx/sites-enabled/
|
|
|
|
```
|
2021-02-09 03:00:52 +00:00
|
|
|
|
|
|
|
Test it's working:
|
|
|
|
|
2023-06-17 19:28:20 +00:00
|
|
|
```bash
|
|
|
|
nginx -t
|
|
|
|
```
|
2021-02-09 03:00:52 +00:00
|
|
|
|
|
|
|
## Troubleshooting
|
|
|
|
|
|
|
|
If it's not working, the error message ends with the line number of the problem in the .conf file.
|
|
|
|
If the error message says '4', the error message is probably around line 4.
|
|
|
|
|
|
|
|
Check:
|
|
|
|
|
|
|
|
- Missing semicolons
|
|
|
|
- Very long website names
|
|
|
|
* Fixing this requires uncommenting `server_names_hash_bucket_size 64;` in /etc/nginx.conf
|
|
|
|
* If that doesn't work, try changing '64' in that line to '128'.
|
|
|
|
|
|
|
|
## DNS
|
|
|
|
|
|
|
|
Buy some DNS online, then check it's working.
|
|
|
|
|
|
|
|
*Once it's working*, use certbot:
|
|
|
|
|
2023-06-17 19:28:20 +00:00
|
|
|
```bash
|
|
|
|
apt install certbot
|
|
|
|
```
|
2021-02-09 03:00:52 +00:00
|
|
|
|
|
|
|
You may need to install an nginx python module:
|
|
|
|
|
2023-06-17 19:28:20 +00:00
|
|
|
```bash
|
|
|
|
apt install python3-certbot-nginx
|
|
|
|
```
|
2021-02-09 03:00:52 +00:00
|
|
|
|
2023-06-17 19:28:20 +00:00
|
|
|
```bash
|
2024-12-01 13:30:44 +00:00
|
|
|
domain=example.com
|
|
|
|
my_email=me@posteo.uk
|
|
|
|
certbot --nginx -d "$domain" --non-interactive --agree-tos -m "$my_email"
|
2023-06-17 19:28:20 +00:00
|
|
|
```
|
2021-02-09 03:00:52 +00:00
|
|
|
|
|
|
|
When you are asked about redirecting from HTTP to HTTPS, say yes (option "2").
|
|
|
|
|
|
|
|
## Renewal
|
|
|
|
|
|
|
|
Remember to renew the certificate every few months.
|
|
|
|
|
|
|
|
# Multiple Sites
|
|
|
|
|
|
|
|
You can do this with any number of websites at the same time.
|
|
|
|
|