构建 Nginx WEB 服务器
Nginx 有内核和模块组成,其中,内核的设计非常微小和简洁,完成的工作也非常简单,仅仅通过查找配置文件将客户端请求映射到一个 location block(location 是 Nginx 配置中的一个指令,用于 URL 匹配),而在这个location 中配置的每个指令将会启动不同的模块去完成相应的工作。
Nginx 相对于 Apache 优点:
1、高并发响应性能非常好,官方 Nginx 处理静态文件并发 5w/s
2、反向代理性能非常好。(可用于负载均衡)
3、内存和CPU 占用率低。(为 Apache 的 1/5-1/10)
4、功能较 Apache 少(常用功能均有)
5、对 php 可使用 cgi 方式和 fastCGI 方式
安装必须的依赖
yum install gcc gcc-c++ automake pcre pcre-devel zlip zlib-devel openssl openssl-devel
gcc为GNU Compiler Collection的缩写,可以编译C和C++源代码等,它是GNU开发的C和C++以及其他很多种语言 的编译器(最早的时候只能编译C,后来很快进化成一个编译多种语言的集合,如Fortran、Pascal、Objective-C、Java、Ada、 Go等。)
gcc 在编译C++源代码的阶段,只能编译 C++ 源文件,而不能自动和 C++ 程序使用的库链接(编译过程分为编译、链接两个阶段,注意不要和可执行文件这个概念搞混,相对可执行文件来说有三个重要的概念:编译(compile)、链接(link)、加载(load)。源程序文件被编译成目标文件,多个目标文件连同库被链接成一个最终的可执行文件,可执行文件被加载到内存中运行)。因此,通常使用 g++ 命令来完成 C++ 程序的编译和连接,该程序会自动调用 gcc 实现编译。
gcc-c++也能编译C源代码,只不过把会把它当成C++源代码,后缀为.c的,gcc把它当作是C程序,而g++当作是c++程序;后缀为.cpp的,两者都会认为是c++程序,注意,虽然c++是c的超集,但是两者对语法的要求是有区别的。
automake是一个从Makefile.am文件自动生成Makefile.in的工具。为了生成Makefile.in,automake还需用到perl,由于automake创建的发布完全遵循GNU标准,所以在创建中不需要perl。libtool是一款方便生成各种程序库的工具。
pcre pcre-devel:在Nginx编译需要 PCRE(Perl Compatible Regular Expression),因为Nginx 的Rewrite模块和HTTP 核心模块会使用到PCRE正则表达式语法。
zlip zlib-devel:nginx启用压缩功能的时候,需要此模块的支持。
openssl openssl-devel:开启SSL的时候需要此模块的支持。
下载 Nginx
cd /usr/src
wget http://nginx.org/download/nginx-1.4.2.tar.gz
tar xf nginx-1.4.2.tar.gz
cd /usr/src/nginx-1.4.2
./configure && make && make install
上传 文件 用 rz
上传 Windows 上的文件,嘿嘿
linux 没有安装???
yum install lrzsz -y
执行:
rz -y
编译安装 Nginx 完成后,
# 查看端口
netstat -ntlp | grep 80
# 查看 Nginx 配置文件
/usr/local/nginx/conf/nginx.conf
# 检查 Nginx 配置文件语法
/usr/local/nginx/sbin/nginx -t
[root@ nginx]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
# 启动 Nginx
/usr/local/nginx/sbin/nginx
# 平滑重启 Nginx
/usr/local/nginx/sbin/nginx -s reload
# 配置 Nginx 虚拟主机
即修改 其配置文件 nginx.conf
server{
listen 80;
server_name www.a.com;
#access_log logs/host.access.log main;
location / {
root html/a
index index.html index.htm;
}
}
server{ …… }
server{ …… }
等等 多个虚拟主机
# 配置完成 来个平滑重启,嘿嘿
修改Windows host 配置文件,使其解析到 自己瞎编的域名上。
路径:system32\drivers\etc\hosts
添加: 192.168.1.116 www.a.com
192.168.1.116 www.b.com
# 查看 编译参数
/usr/local/nginx/sbin/nginx -v
升级Nginx 操作步骤
# 查看 编译参数
# 下载升级所需的 Nginx
tar xf nginx-1.6.1.tar.gz
cd nginx-1.6.1
/usr/local/nginx/sbin/nginx -V ###查看编译参数
./configure --prefix= #老版本的 编译参数
make # *****不能执行 make install
# 备份旧 Nginx 执行文件
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
# 备份 二进制 文件
cp objs/nginx /usr/local/nginx/sbin/nginx # 对新版本的objs
# 平滑重启 Nginx
/usr/local/nginx/sbin/nginx -s reload
其他相关
9、重读配置文件和关闭服务:
[root@Server1 local]# /usr/local/nginx/sbin/nginx #启动 服务
[root@Server1 local]# /usr/local/nginx/sbin/nginx -s reload #不停止服务重读配置文件
[root@Server1 local]# /usr/local/nginx/sbin/nginx/nginx -s stop #停止服务 #停止服务
10.验证端口是否开启:
[root@Server1 sbin]# ps -ef | grep nginx
root 13228 1 0 Apr23 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx/nginx #nginx的主进程,只有一个主进程
nginx 13229 13228 0 Apr23 ? 00:00:00 nginx: worker process #nginx工作进程,默认只有一个,可以通过修改nginx.conf中的worker_processes 1; 参数启动多个工作进程
root 13295 1400 0 00:01 pts/0 00:00:00 grep --color=auto nginx
[root@Server1 local]# lsof -i:8090 #显示占用8090的进程
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 13337 root 6u IPv4 5932680 0t0 TCP *:8090 (LISTEN)
nginx 13338 nginx 6u IPv4 5932680 0t0 TCP *:8090 (LISTEN)
location ~ \.php$ {
fastcgi_pass 10.170.2.90:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www/html$fastcgi_script_name;
include fastcgi_params;
}
php-fpm 的编译安装
./configure --prefix=/usr/local/php5 --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --enable-fpm --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2
# make && make install
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
pid = /lnmp/php5/var/run/php-fpm.pid
<h1>hello,everybody, i am nginx</h1>
<?php
$link = mysql_connect('192.168.204.132','test141','123123');
if ($link)
echo "Success...";
else
echo "Failure...";
mysql_close();
phpinfo();
?>
nginx启动脚本
#! /bin/bash
# chkconfig: 35 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse
set -e
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
SCRIPTNAME=/etc/init.d/$NAME
test -x $DAEMON || exit 0
d_start(){
$DAEMON || echo -n " already running"
}
d_stop() {
$DAEMON -s quit || echo -n " not running"
}
d_reload() {
$DAEMON -s reload || echo -n " counld not reload"
}
case "$1" in
start)
echo -n "Starting $DESC:$NAME"
d_start
echo "."
;;
stop)
echo -n "Stopping $DESC:$NAME"
d_stop
echo "."
;;
reload)
echo -n "Reloading $DESC configuration..."
d_reload
echo "reloaded."
;;
restart)
echo -n "Restarting $DESC: $NAME"
d_stop
sleep 2
d_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2
exit 3
;;
esac
exit 0
## 将shell脚本放入到 /etc/rc.d/init.d/中,并执行下列命令
## chmod +x /etc/rc.d/init.d/nginx (设置可执行权限)
## chkconfig --add nginx (添加系统服务)