您的位置:首页 > 理论基础 > 计算机网络

用Lighttpd 建设web服务器

2014-11-01 15:56 85 查看
(写在前面的话:文章转载自互联网,仅供作者学习使用,不得用于商业用途。。。)

这里的web服务器没有选择Apache也没有选择nginx,这里笔者选择Lighttpd 。Lighttpd提供一个专门针对高性能网站,安全、快速、兼容性好并且灵活的web 服务器环境。具有非常低的内存开销、cpu占用率低、效能好以及丰富的模块等特点。下面看看它和php以及MariaDB 数据库的整合配置过程:

1、安装MariaDB/ mysql数据库

#yum install mysql mysql-server 

启动服务:

#systemctl enable mysqld.service 
#systemctl start mysqld.service 

下面是初始化操作:

# mysql_secure_installation 

操作过程主要是 初始化数据目录和授权表,设置root口令等。

2、安装Lighttpd

安装软件包:

#yum install lighttpd 

启动服务:

#systemctl enable lighttpd.service 
#systemctl restart lighttpd.service 

3、简单测试一下web服务器

测试web服务是否成功安装并启动,浏览器访问IP 显示如下即可如图5:




图5 简单测试一下web服务器
4、安装PHP5软件包

#yum install php-fpm lighttpd-fastcgi 

说明一下PHP-FPM 是FastCGI server ,使用端口是9000。

启动服务:

#systemctl enable php-fpm.service 
#systemctl start php-fpm.service 

5、修改相关的配置文件

首先修改/etc/php.ini:

把下面一行的注释去掉 
[...] 
cgi.fix_pathinfo=1 
[...] 
然后修改/etc/lighttpd/conf.d/fastcgi.conf配置文件为如下格式: 
[...] 
server.modules += ( "mod_fastcgi" ) 
[...] 
注意fastcgi.server 字段内容如下: 
[...] 
## 
## PHP Example 
## For PHP don't forget to set cgi.fix_pathinfo = 1 in the php.ini. 
## 
## The number of php processes you will get can be easily calculated: 
## 
## num-procs = max-procs * ( 1 + PHP_FCGI_CHILDREN ) 
## 
## for the php-num-procs example it means you will get 17*5 = 85 php 
## processes. you always should need this high number for your very 
## busy sites. And if you have a lot of RAM. :) 
## 
fastcgi.server += ( ".php" => 
(( 
"host" => "127.0.0.1", 
"port" => "9000", 
"broken-scriptfilename" => "enable" 
)) 

#fastcgi.server = ( ".php" => 
#                   ( "php-local" => 
#                     ( 
#                       "socket" => socket_dir + "/php-fastcgi-1.socket", 
#                       "bin-path" => server_root + "/cgi-bin/php5", 
#                       "max-procs" => 1, 
#                       "broken-scriptfilename" => "enable", 
#                     ) 
#                   ), 
#                   ( "php-tcp" => 
#                     ( 
#                       "host" => "127.0.0.1", 
#                       "port" => 9999, 
#                       "check-local" => "disable", 
#                       "broken-scriptfilename" => "enable", 
#                     ) 
#                   ), 

#                   ( "php-num-procs" => 
#                     ( 
#                       "socket" => socket_dir + "/php-fastcgi-2.socket", 
#                       "bin-path" => server_root + "/cgi-bin/php5", 
#                       "bin-environment" => ( 
#                         "PHP_FCGI_CHILDREN" => "16", 
#                         "PHP_FCGI_MAX_REQUESTS" => "10000", 
#                       ), 
#                       "max-procs" => 5, 
#                       "broken-scriptfilename" => "enable", 
#                     ) 
#                   ), 
#                ) 
), 
[...] 

然后还要激活这个模块,修改配置文件/etc/lighttpd/modules.conf

[...] 
## FastCGI (mod_fastcgi) 
## 
include "conf.d/fastcgi.conf" 
[...] 

就是去掉include "conf.d/fastcgi.conf"前面的注释符号。

下面测试一下php脚本:

# vi /var/www/lighttpd/info.php 
<?php 
phpinfo(); 
?> 
# systemctl restart lighttpd.service 

然后使用浏览器查看如图6 ,大家看红色圈框部分(Server API FPM/FastCGI )




图6 测试一下php脚本
图6显示则表明web服务器可以解析静态页面和php页面(但目前还无法连接mysql数据库)。

下面设置为PHP安装MySQL支持:

php5支持MySQL很简单,只要安装php-mysql软件包即可;但php程序要运行可能需要多个php模块的支持

# yum install php-mysqlnd php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-magickwand php-mbstring php-mcrypt php-mssql php-shout php-snmp php-soap php-tidy php-opcache 

安装完成后重启:

#systemctl reload php-fpm.service 

重新访问浏览器查看安装的php模块 如mysql:(如下图7)




图7 为PHP安装MySQL支持
Unix域Socket通信设置

Unix域Socket因为不走网络,的确可以提高web服务器和php-fpm通信的性能,但在高并发时会不稳定。设置如下:

vi /etc/php-fpm.d/www.conf 
修改为如下内容 
[...] 
;listen = 127.0.0.1:9000 
listen = /tmp/php5-fpm.sock 
[...] 
然后重启服务: 
# systemctl reload php-fpm.service 
下面修改etc/lighttpd/conf.d/fastcgi.conf 文件,修改为如下内容 
vi /etc/lighttpd/conf.d/fastcgi.conf 
fastcgi.server += ( ".php" => 
(( 
"socket" => "/tmp/php5-fpm.sock", 
"broken-scriptfilename" => "enable" 
)) 

然后重启服务: 
# systemctl restart lighttpd.service  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息