1、nginx安装环境
nginx是 c 语言开发的,建议在Linux 上运行。本教程使用的是Centos6.5 作为安装环境。
- gcc
安装 nginx 需要先将官网下载的源码进行编译,编译依赖gcc 环境,如果没有gcc 环境,需要安装gcc:
yun
install gcc-c++
- pcre
pcre 是一个Perl 库,包括 Perl 兼容的正则表达式库。nginx 的http 模块使用 pcre 来解析正则表达式,所以需要在Linux 上安装pcre 库。
yum
install -y pcre pcre-devel
pcre-devel 是使用pcre 开发的一个二次开发库,nginx 也需要此库。
- zlib
zlib 库提供了很多种压缩和解压缩的方式,nginx 使用zlib 对http 包的内同进行gzip ,所以需要在Linux 上安装 zlib 库
yum
install -y zlib zlib-devel
- openssl
OpenSSL 是一个强大的暗钻套接字层密码库,囊括主要的密码算法、常用的秘钥和整数封装管理功能以及SSL 协议,并提供丰富的应用程序供测试或其他目的的使用。
nginx 不仅支持http 协议,还支持 https 所以需要在Linux 安装openssl 库。
yum
install -y openssl openssl-devel
2、编译安装
将nginx-1.8.0.tar.gz 拷贝到Linux 服务器。
解压:
tar
-zxvf
nginx-1.8.0.tar.gzcd
nginx-1.8.0
1、configure
./configure --help 查询详细参数
参数设置如下:
./configure \--prefix=/usr/local/nginx
\--pid-path=/var/run/nginx/nginx.pid \--lock-path=/var/lock/nginx.lock
\--error-log-path=/var/log/nginx/error.log \--http-log-path=/var/log/nginx/access.log
\--with-http_gzip_static_module \--http-client-body-temp-path=/var/temp/nginx/client \--http-proxy-temp-path=/var/temp/nginx/proxy
\--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \--http-uwsgi-temp-path=/var/temp/nginx/uwsgi
\--http-scgi-temp-path=/var/temp/nginx/scgi
注意:上边将临时文件目录指定为 /var/temp/nginx ,需要在 /var 下创建 temp 及 nginx 目录。
2、编译安装
make install
安装成功查看安装目录:
ll 命令查看,会有conf,html,sbin 至少三个文件夹。
3、启动 nginx
cd /usr/local/nginx/sbin/./nginx
查询nginx进程:
ps aux|grep nginx
15098 是 nginx 主进程的进程id,10599 是nginx 工作进程的进程id
注意:执行./nginx 启动 nginx, 这里可以 -c 指定加载的 nginx 配置文件,如下: ./nginx -c /usr/local/nginx/conf/nginx.conf
如果不指定 -c,nginx 在启动时默认加载 conf/nginx.conf 文件,此文件的地址也可以再编译安装 nginx 时指定 ./configure 的参数(--conf-path= 指向配置文件(nginx.conf))
4、停止nginx
方式1. 快速停止:
cd /usr/local/nginx/sbin./nginx
-s stop
此方法相当于先查出nginx 进程id 在使用 kill 命令强制杀掉进程。
方式2. 完整停止(建议使用)
cd /usr/local/nginx/sbin./nginx
-s quit
此方式停止步骤是待nginx 进程处理任务完毕进行停止。
5、重启nginx
1.先停止再启动(建议使用)
对nginx进行重启相当于先停止nginx再启动nginx,即先执行停止命令再执行启动命令。
./nginx -s quit./nginx
2.重新加载配置文件
当nginx的配置文件nginx.conf修改后,要想让配置生效需要重启nginx,使用-s reload不用先停止nginx再启动nginx即可将配置信息在nginx中生效,如下:
./nginx -s reload
6、开机启动nginx
1. 编写shell 脚本
这里使用的是编写shell 脚本的方式处理
vi /etc/init.d/nginx
#!/bin/bash#
nginx Startup script for the Nginx HTTP Server# it is v.0.0.2 version.#
chkconfig: - 85 15# description: Nginx is a high-performance web and proxy server.#
It has a lot of features, but it's not for everyone.# processname: nginx#
pidfile: /var/run/nginx.pid# config: /usr/local/nginx/conf/nginx.confnginxd=/usr/local/nginx/sbin/nginxnginx_config=/usr/local/nginx/conf/nginx.confnginx_pid=/var/run/nginx.pidRETVAL=0prog="nginx"#
Source function library.. /etc/rc.d/init.d/functions#
Source networking configuration.. /etc/sysconfig/network# Check that networking is up.[
${NETWORKING} =
"no" ] &&
exit
0[ -x
$nginxd ] ||
exit
0# Start nginx
daemons functions.start()
{if [ -e
$nginx_pid ];then
echo
"nginx already running...."
exit
1fi
echo -n $"Starting
$prog: " daemon
$nginxd -c
${nginx_config} RETVAL=$?
echo [
$RETVAL =
0 ] && touch /var/lock/subsys/nginx
return
$RETVAL}# Stop
nginx daemons functions.stop()
{ echo -n $"Stopping
$prog: " killproc
$nginxd RETVAL=$?
echo [
$RETVAL =
0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid}#
reload nginx service functions.reload()
{ echo -n $"Reloading
$prog: "
#kill -HUP `cat ${nginx_pid}` killproc
$nginxd -HUP RETVAL=$?
echo}# See how
we were called.case
"$1"
instart) start ;;stop) stop ;;reload) reload ;;restart) stop start ;;status) status
$prog RETVAL=$? ;;*)
echo $"Usage:
$prog {start|stop|restart|reload|status|help}"
exit
1esacexit
$RETVAL
:wq 保存并退出
2. 设置文件的访问权限
chmod a+x /etc/init.d/nginx (a+x ==> all user can execute 所有用户可执行)
这样在控制台就很容易的操作nginx 了。查看 nginx 当前状态,启动 nginx,停止nginx,重启nginx。。。
如果修改了nginx的配置文件nginx.conf,也可以使用上面的命令重新加载新的配置文件并运行,可以将此命令加入到rc.local文件中,这样开机的时候nginx就默认启动了
3.加入到rc.local 文件中
vi /etc/rc.local
加入一行 /etc/init.d/nginx start 保存并退出,下次重启生效