[3] Nginx Server Setup - Connecting to C++ Web Framework

0. Researching C++ Web Frameworks

To achieve the goal of building a server in C++, I've started by researching suitable C++ web frameworks. According to GPT recommendations, Drogon is popular, so I'll proceed with this framework first.


Framework Advantages Disadvantages
Drogon Supports modern C++14/17 standards
Asynchronous programming and multi-thread support
Supports HTTP2 and WebSocket
High performance and user-friendly API
Active community and support
Initial setup can be complex due to many features
Pistache Lightweight design enables quick web server implementation
Suitable for RESTful API development
Simple code and easy to learn
Slow development and updates
Limited functionality
Crow Intuitive usage similar to Python’s Flask
Includes JSON support and routing functionality
Lightweight design
Limited features, not ideal for complex applications
May lack active maintenance and support
CppCMS Suitable for high-performance web application development
Provides features like session management and internationalization
Complex setup and steep learning curve
More difficult to use compared to other frameworks, with limited documentation
Restbed Suitable for lightweight RESTful API development
Asynchronous and scalable design
Limited functionality, not ideal for large-scale projects
Lacks active updates and community support

1. Installing Framework Dependencies

sudo yum install -y git cmake gcc-c++ libuuid-devel openssl-devel zlib-devel brotli-devel jsoncpp-devel libuuid libuuid-devel

If jsoncpp-devel is unavailable, install it separately.


Separate installation of JSONCPP

# Download the JSONCPP source code  
git clone https://github.com/open-source-parsers/jsoncpp.git  
cd jsoncpp  

# Create build directory  
mkdir build  
cd build  

# Configure and build using CMake  
cmake .. -DCMAKE_BUILD_TYPE=Release  
make -j$(nproc)  

# Install  
sudo make install  

# Link installation path (specify the installed path, in this case, it is /usr/local/lib64)  
echo "/usr/local/lib64" | sudo tee /etc/ld.so.conf.d/jsoncpp.conf  
sudo ldconfig  

# Verification  
ldconfig -p | grep jsoncpp  

[root@vbox build] (master) $ ldconfig -p | grep jsoncpp  
        libjsoncpp.so.27 (libc6,x86-64) => /usr/local/lib64/libjsoncpp.so.27  
        libjsoncpp.so (libc6,x86-64) => /usr/local/lib64/libjsoncpp.so  

2. Building the Framework

git clone https://github.com/drogonframework/drogon.git
cd drogon

mkdir build
cd build 
cmake .. -DCMAKE_BUILD_TYPE=Release
make -j$(nproc)
sudo make install

Error During Installation

CMake Error at CMakeLists.txt:137 (add_subdirectory):  
  The source directory  

    /root/drogon/trantor  

  does not contain a CMakeLists.txt file.
  • Run the following command:
    git submodule update --init --recursive

  • After running the command, go to the build directory and execute:

cmake .. -DCMAKE_BUILD_TYPE=Release
make -j$(nproc)
sudo make install

Then rebuild.

3. Verifying the Framework

To check the version:

[root@vbox drogon_ctl] (master) $ pwd
/root/drogon/build/drogon_ctl
[root@vbox drogon_ctl] (master) $ ./drogon_ctl version
     _
  __| |_ __ ___   __ _  ___  _ __
 / _` | '__/ _ \ / _` |/ _ \| '_ \
| (_| | | | (_) | (_| | (_) | | | |
 \__,_|_|  \___/ \__, |\___/|_| |_|
                 |___/

A utility for drogon
Version: 1.9.8
Git commit: 8541e671430f9605a62f1afbd786c1dbd54197d6
Compilation:
  Compiler: c++
  Compiler ID: GNU
  Compilation flags: -O3 -DNDEBUG -std=c++17 -I/usr/local/include
Libraries:
  postgresql: no  (pipeline mode: no)
  mariadb: no
  sqlite3: no
  ssl/tls backend: OpenSSL
  brotli: no
  hiredis: no
  c-ares: no
  yaml-cpp: no

Run the Built Test Server in the Following Directory

/root/drogon/build/examples

[root@vbox examples] (master) $ ./helloworld  
20241103 16:23:09.594785 UTC 52417 INFO  Server running on 127.0.0.1:8848 - main.cc:95  
20241103 16:23:09.596557 UTC 52418 INFO  setBeforeListenSockOptCallback:10 - main.cc:76  

Internal CURL Test

[root@vbox ~] $ curl -v 127.0.0.1:8848  
*   Trying 127.0.0.1:8848...  
* Connected to 127.0.0.1 (127.0.0.1) port 8848 (#0)  
> GET / HTTP/1.1  
> Host: 127.0.0.1:8848  
> User-Agent: curl/7.76.1  
> Accept: */*  
>  
* Mark bundle as not supporting multiuse  
< HTTP/1.1 200 OK  
< content-length: 13  
< content-type: text/html; charset=utf-8  
< server: drogon/1.9.8  
< date: Sun, 03 Nov 2024 16:22:09 GMT  
<  
* Connection #0 to host 127.0.0.1 left intact  

Web Test

Reloading NGINX is necessary before running the web test.

worker_processes  10;

events {
    worker_connections  1024;
    use epoll;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;
    large_client_header_buffers 4 16k;

    server {
        listen       10099;
        server_name  localhost;

        location / {
            proxy_pass http://localhost:8848;  # Proxy to backend server
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
    }
}

After modifying the configuration, reload or restart NGINX: ./nginx -s reload" or

./nginx -s stop
./nginx


Verifying Operation

nginx

Web(10099) -> VM -> NGINX (10099) -> Drogon (8848) This setup confirms the server operates in the above structure.



댓글

이 블로그의 인기 게시물

윤석열 계엄령 선포! 방산주 대폭발? 관련주 투자 전략 완벽 분석

한국 핵무장 논의와 방위산업 관련주: 핵무기 개발 과정과 유망 종목 분석

[로스트아크] 제작 효율 최적화 위한 영지 세팅