Windows linux命令 微软 wordpress Firefox Python Ubuntu 开源 centos php 程序员 HTML5 mysql java Android apache google nginx shell linux

Nginx实战基础篇一 源码包编译安装部署web服务器

一、安装Nginx必须的依赖包

[root@rhel6u3-7 ~]# yum -y install gcc openssl-devel pcre-devel zlib-devel  //yum创建过程略,安装略

二、安装编译nginx,目前系统测试环境为rhel6.3 软件版本为nginx-1.27

[root@rhel6u3-7 ~]# useradd nginx -s /sbin/nologin //给nginx服务器创建后台进程管理用户
[root@rhel6u3-7 ~]# tar zxvf nginx-1.2.7.tar.gz //解压缩
[root@rhel6u3-7 ~]# cd nginx-1.2.7
[root@rhel6u3-7 nginx-1.2.7]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx/ --with-http_stub_status_module --with-http_ssl_module
// --user=nginx –group=nginx 设置允许nginx允许的用户和组为nginx
// --prefix=/usr/local/nginx/ 设置nginx安装路径
// --with-http_stub_status_module 安装允许状态模块
// --with-http_ssl_module 安装ssl模块
//更多参数请参看 ./configure --help
…… //安装显示略,如果配置正确,最后会显示以下信息。
Configuration summary
+ using system PCRE library
+ using system OpenSSL library
+ md5: using OpenSSL library
+ sha1: using OpenSSL library
+ using system zlib library
nginx path prefix: "/usr/local/nginx/"
nginx binary file: "/usr/local/nginx//sbin/nginx"
nginx configuration prefix: "/usr/local/nginx//conf"
nginx configuration file: "/usr/local/nginx//conf/nginx.conf"
nginx pid file: "/usr/local/nginx//logs/nginx.pid"
nginx error log file: "/usr/local/nginx//logs/error.log"
nginx http access log file: "/usr/local/nginx//logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
[root@rhel6u3-7 ~]# /usr/local/nginx/sbin/nginx –V //安装完成后查看Nginx的相关环境配置信息是否正确
nginx version: nginx/1.2.7
built by gcc 4.4.6 20120305 (Red Hat 4.4.6-4) (GCC)
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --prefix=/usr/local/nginx/ --with-http_stub_status_module --with-http_ssl_module
[root@rhel6u3-7 ~]#
[root@rhel6u3-7 nginx-1.2.7]# make & make install //编译安装过程略

三、通过nginx自身脚本机器nginx服务器,并通过各种命令查看是否启动成功

[root@rhel6u3-7 ~]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf // -c指向nginx主配置文件
[root@rhel6u3-7 ~]# lsof -i :80 //查看nginx进程号及运行情况
command PID USER FD type DEVICE SIZE/OFF NODE NAME
nginx 4080 root 6u IPv4 21467 0t0 TCP *:http (LISTEN)
nginx 4081 nginx 6u IPv4 21467 0t0 TCP *:http (LISTEN)
[root@rhel6u3-7 ~]# ps -ef | grep nginx //查看nginx进程号及运行情况
root 4080 1 0 22:24 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nginx 4081 4080 0 22:24 ? 00:00:00 nginx: worker process
root 4088 1433 0 22:27 pts/0 00:00:00 grep nginx
[root@rhel6u3-7 ~]# netstat -nltp | grep 80 //查看nginx进程监听端口
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4080/nginx

四,测试nginx配置的默认web服务器是否能正常运行

通过Linux自带命令links 测试

[root@rhel6u3-7 ~]# links 127.0.0.1 //出现 welcome to nginx!说明nginx服务启动成功

通过windows服务器IE浏览器测试

扩展知识:

1、设置nginx开机自动启动,将nginx启动命令添加到/etc/rc.local

[root@rhel6u3-7 ~]# echo "/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf" >> /etc/rc.local
 [root@rhel6u3-7 ~]# cat /etc/rc.local | grep nginx
 /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 

2、通过设置System V 脚本,使用server命令启动nginx服务

[root@rhel6u3-7 init.d]# vim nginx //在/etc/init.d/下创建nginx启动脚本文件
 #!/bin/sh
 #
 # nginx - this script starts and stops the nginx daemon
 #
 # chkconfig: - 85 15
 # description: Nginx is an HTTP(S) server, HTTP(S) reverse 
 # proxy and IMAP/POP3 proxy server
 # processname: nginx
 # config: /etc/nginx/nginx.conf
 # config: /etc/sysconfig/nginx
 # pidfile: /var/run/nginx.pid
 # Source function library.
 . /etc/rc.d/init.d/functions
 # Source networking configuration.
 . /etc/sysconfig/network
 # Check that networking is up.
 [ "$NETWORKING" = "no" ] && exit 0
 nginx="/usr/local/nginx/sbin/nginx"
 prog=$(basename $nginx)
 NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
 [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
 lockfile=/var/lock/subsys/nginx
 start() {
 [ -x $nginx ] || exit 5
 [ -f $NGINX_CONF_FILE ] || exit 6
 echo -n $"Starting $prog: "
 daemon $nginx -c $NGINX_CONF_FILE
 retval=$?
 echo
 [ $retval -eq 0 ] && touch $lockfile
 return $retval
 }
 stop() {
 echo -n $"Stopping $prog: "
 killproc $prog -QUIT
 retval=$?
 echo
 [ $retval -eq 0 ] && rm -f $lockfile
 return $retval
 killall -9 nginx
 }
 restart() {
 configtest || return $?
 stop
 sleep 1
 start
 }
 reload() {
 configtest || return $?
 echo -n $"Reloading $prog: "
 killproc $nginx -HUP
 RETVAL=$?
 echo
 }
 force_reload() {
 restart
 }
 configtest() {
 $nginx -t -c $NGINX_CONF_FILE
 }
 rh_status() {
 status $prog
 }
 rh_status_q() {
 rh_status >/dev/null 2>&1
 }
 case "$1" in
 start)
 rh_status_q && exit 0
 $1
 ;;
 stop)
 rh_status_q || exit 0
 $1
 ;;
 restart|configtest)
 $1
 ;;
 reload)
 rh_status_q || exit 7
 $1
 ;;
 force-reload)
 force_reload
 ;;
 status)
 rh_status
 ;;
 condrestart|try-restart)
 rh_status_q || exit 0
 ;;
 *)
 echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
 exit 2
 esac   
[root@rhel6u3-7 init.d]# chmod 755 nginx //修改脚本文件nginx的权限
[root@rhel6u3-7 init.d]# chkconfig --add nginx //将脚本文件加入chkconfig中
[root@rhel6u3-7 init.d]# chkconfig --level 35 nginx on //设置nginx开机在3和5级别自动启动
[root@rhel6u3-7 init.d]# chkconfig --list | grep nginx
nginx

延伸阅读

评论