在CentOS系统上部署C++ Web应用,可以按照以下步骤进行:
首先,确保你的CentOS系统已经安装了以下软件:
g++
。sudo yum install nginx php-fpm mysql-server g++ -y
编辑Nginx配置文件 /etc/nginx/nginx.conf
或创建一个新的配置文件 /etc/nginx/conf.d/your_app.conf
。
server {
listen 80;
server_name your_domain.com;
root /path/to/your/cpp/app;
index index.html index.php;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location /api/ {
proxy_pass http://localhost:8080; # 假设你的C++应用运行在8080端口
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;
}
}
假设你的C++ Web应用代码在 /path/to/your/cpp/app
目录下,使用 g++
编译:
cd /path/to/your/cpp/app
g++ -o my_app main.cpp -lboost_system -lpthread
启动Nginx和PHP-FPM服务:
sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
确保防火墙允许HTTP(80)和HTTPS(443)流量:
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
假设你的C++应用监听在8080端口,可以使用 systemd
创建一个服务来管理它。
创建一个新的服务文件 /etc/systemd/system/my_app.service
:
[Unit]
Description=My C++ Web Application
After=network.target
[Service]
User=your_user
Group=your_group
ExecStart=/path/to/your/cpp/app/my_app
Restart=always
[Install]
WantedBy=multi-user.target
启动并启用该服务:
sudo systemctl daemon-reload
sudo systemctl start my_app
sudo systemctl enable my_app
打开浏览器,访问 http://your_domain.com
,你应该能够看到你的C++ Web应用。
以上步骤涵盖了在CentOS上部署C++ Web应用的基本流程,包括安装必要的软件、配置Nginx、编译C++应用、启动服务和测试部署。根据你的具体需求,可能还需要进行更多的配置和优化。