[Webserver]httplib webserver+https(Let’s Encrypt)(2)(MacOS,Linux)

๐Ÿ‘‰๐Ÿป ๋ฆฌ๋ˆ…์Šค์„œ๋ฒ„์—์„œ ํ…Œ์ŠคํŠธ ํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.
This is the content tested on a Linux server.

๐Ÿ‘‰๐Ÿป Let’s Encrypt๋กœ https๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.
This is how to implement HTTPS with Let’s Encrypt.

๐Ÿ‘‰๐Ÿป ๋ฐฉํ™”๋ฒฝ์˜คํ”ˆ / Firewall open

— ์•„๋ž˜๋Š” iptables ๋ฐฉํ™”๋ฒฝ ์˜คํ”ˆ ํ•˜๋Š” ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.
Below is the content for opening the iptables firewall.

$ sudo iptables -I INPUT -p tcp --dport 80 -j ACCEPT
$ sudo iptables -I INPUT -p tcp --dport 443 -j ACCEPT
$ sudo iptables -I INPUT -p tcp --dport 5080 -j ACCEPT

# iptables-persistent์„ค์น˜์•ˆํ•œ๊ฒฝ์šฐ
$ sudo apt install iptables-persistent

# ์„ค์ • ๋‚ด์šฉ์ €์žฅ / Save settings
$ sudo netfilter-persistent save

๐Ÿ‘‰๐Ÿป openssl์ด์„ค์น˜๋˜์–ด ์žˆ์ง€ ์•Š์œผ๋ฉด ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.
If openssl is not installed, install it.

sudo apt update
sudo apt install g++ libssl-dev openssl wget -y

โญ๏ธ openssl์€ 3.0์ด์ƒ์„ ์‚ฌ์šฉํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.
You must use openssl 3.0 or higher.

โญ๏ธ openssl 3.x์ด์ƒ์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์šฐ๋ถ„ํˆฌ 22.04๋ฒ„์ „์ด์ƒ์ด์–ด์•ผํ•ฉ๋‹ˆ๋‹ค.
To use openssl 3.x or higher, you must be running Ubuntu version 22.04 or higher.

๐Ÿ‘‰๐Ÿป Let’s Encrypt์ธ์ฆ์„œ ๋ฐ›๊ธฐ / Get Let’s Encrypt Certificate

— ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ™”๋ฉด์ด ๋ณด์ด๋ฉด ์„ฑ๊ณตํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.
If you see the following screen, you have succeeded.

–๋ฐฉํ™”๋ฒฝ์ด๋‚˜ ํฌํŠธ๊ฐ€ ๋ง‰ํžˆ๋ฉด ์ธ์ฆ์„œ ๋ฐœ๊ธ‰์ด ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
If the firewall or ports are blocked, the certificate cannot be issued.


$ sudo certbot certonly --standalone -d host.domain.com
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Requesting a certificate for host.domain.com

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/host.domain.com/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/host.domain.com/privkey.pem
This certificate expires on 2028-10-01.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you like Certbot, please consider supporting our work by:
 * Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
 * Donating to EFF:                    https://eff.org/donate-le
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
$ 

๐Ÿ‘‰๐Ÿป ํ˜„์žฌ ์„œ๋ฒ„ ๋””๋ ‰ํ† ๋ฆฌ๋กœ ์ธ์ฆ์„œ๋ฅผ ๋ณต์‚ฌํ•ฉ๋‹ˆ๋‹ค.
Copy certificate to current server directory

$ sudo cp /etc/letsencrypt/live/host.domain.com/fullchain.pem ./
$ sudo cp /etc/letsencrypt/live/host.domain.com/privkey.pem ./

# ๋‚ด ๊ณ„์ •์œผ๋กœ ์†Œ์œ ๊ถŒ ๋ณ€๊ฒฝ / Change ownership to my account
$ sudo chown username:username *.pem  

๐Ÿ‘‰๐Ÿป ์ฝ”๋“œ/Code

โœ”๏ธ ๊ธฐ์กด์˜ ์„œ๋ฒ„ ์ฝ”๋“œ์—์„œ ์ธ์ฆ์„œ ๋ถ€๋ถ„๋งŒ ์ˆ˜์ •ํ•ฉ๋‹ˆ๋‹ค.
Modify only the certificate part of the existing server code.

#define CPPHTTPLIB_OPENSSL_SUPPORT
#include "httplib.h"
#include <iostream>

int main( ) {
    // ์ธ์ฆ์„œ์™€ ํ‚ค ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
    // Specify the certificate and key file paths.
    httplib::SSLServer svr("./fullchain.pem", "./privkey.pem" );

    if (!svr.is_valid()) {
        std::cerr << "SSL ์„œ๋ฒ„ ์„ค์ • ์‹คํŒจ / SSL server setup failed!" << std::endl;
        return 1;
    }

    svr.Get("/", [](const httplib::Request&, httplib::Response& res ) {
        res.set_content("<h1> ๐Ÿ‘‰ Linux  HTTPS Server is Running! ๐Ÿ““ </h1>", "text/html; charset=utf-8");
    });

    std::cout << "Server Listening: https://0.0.0.0:5080" << std::endl;
    svr.listen("0.0.0.0", 5080 );

    return 0;
}

๐Ÿ‘‰๐Ÿป ์ปดํŒŒ์ผ / Compiling

g++ -o server server.cpp -std=c++11 -pthread -lssl -lcrypto

๐Ÿ‘‰๐Ÿป ์„œ๋ฒ„์‹คํ–‰ / Run server

./server

๐Ÿ‘‰๐Ÿป ๋ธŒ๋ผ์šฐ์ €์ ‘์† / Browser access

Screenshot

Leave a Reply