irpas技术客

c++ 经典服务器开源项目 Tinywebserver的使用与配置(百度智能云服务器安装ubuntu18.04可用公网ip访问)_yingLGG_c++ web

网络 8371

1 Tinywebserver介绍

Linux下C++轻量级Web服务器,助力初学者快速实践网络编程,搭建属于自己的服务器.

使用 线程池 + 非阻塞socket + epoll(ET和LT均实现) + 事件处理(Reactor和Proactor均实现) 的并发模型使用状态机解析HTTP请求报文,支持解析GET和POST请求访问服务器数据库实现web端用户注册、登录功能,可以请求服务器图片和视频文件实现同步/异步日志系统,记录服务器运行状态经Webbench压力测试可以实现上万的并发连接数据交换 2 准备环境和源码 系统环境: ubuntu 18.04(在centos上测试了很多次,但是由于环境的问题,安装的mysql一直找不到正确的用户名和密码) 需要用到git apt-get install gitg++环境用来编译:apt-get install build-essential`下载源码git clone https://github.com/qinguoyi/TinyWebServer.git 3. 安装配置mysql

3.1安装mysql

sudo apt-get install mysql-server

3.2 进行初始化配置

sudo mysql_secure_installation

配置项较多,如下

#1 VALIDATE PASSWORD PLUGIN can be used to test passwords... Press y|Y for Yes, any other key for No: N (我的选项) #2 Please set the password for root here... New password: (输入密码) Re-enter new password: (重复输入) #3 By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them... Remove anonymous users? (Press y|Y for Yes, any other key for No) : N (我的选项) #4 Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network... Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y (我的选项) #5 By default, MySQL comes with a database named 'test' that anyone can access... Remove test database and access to it? (Press y|Y for Yes, any other key for No) : N (我的选项) #6 Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y (我的选项)

3.3检查mysql状态

systemctl status mysql.service

3.4 进入mysql

sudo mysql -uroot -p

3.5 根据readme操作mysql,这里其实就是写sql语句了 分别包括创建数据库 yourdb,use相当于进入这个数据库,创建user表

create database yourdb; USE yourdb; CREATE TABLE user( username char(50) NULL, passwd char(50) NULL )ENGINE=InnoDB; INSERT INTO user(username, passwd) VALUES('name', 'passwd');

可以利用以下命令查看表和表的内容:

show databases; //可以查看当前的数据库 show users; select *from user; 4. 编译Tinywebserver

4.1 首先需要确认main.cpp里的数据库和你mysql数据库配置相同。 查看数据库名称和密码

cd /etc/mysql sudo vim debian.cnf

然后打开main.cpp修改对应配置 4.2 编译Tinywebserver(编译运行)

cd Tinywebserver sh ./build.sh

编译时遇到的错误:fatal error: mysql.h: No such file or directory 解决方法:安装链接库 apt-get install libmysqlclient-dev

./server

这时候命令是没有退出的,如果退出且日志里出现Mysql error大部分情况是因为数据库没连上,可以去github看一下相关问题

5查看效果

输入ip:9006就可以进行登录注册操作了,而且mysql数据库是动态更新的。 可以用云服务器公网ip加9006进行访问


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #C #webserver #开源库 #1 #线程池 #非阻塞socket