nginx 安装

nginx介绍

nginx是C语言开发,建议在linux上运行,本教程使用Centos6.5作为安装环境。

Nginx是一款高性能的http 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。

官方测试nginx能够支支撑5万并发链接,并且cpu、内存等资源消耗却非常低,运行非常稳定。

nginx安装

下载

进入http://nginx.org/en/download.html 下载nginx1.8.1版本(当前最新稳定版本)

安装

nginx安装环境

nginx是C语言开发,建议在linux上运行,本教程使用Centos6.7作为安装环境。

gcc

安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境;

如果没有gcc环境,需要安装gcc:

yum –y install gcc-c++

PCRE

PCRE(Perl Compatible Regular Expressions)是一个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(即在ssl协议上传输http),所以需要在linux安装openssl库。

yum install -y openssl openssl-devel

 

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$插曲$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

也可以使用从网上下载的包,自己解压,安装,这样会比较快一点。

先安装gcc

yum install gcc-c++

PCRE:

       ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/

ZLIB:

       http://zlib.net/zlib-1.2.8.tar.gz

OPENSSL:

       https://www-origin.openssl.org/source/openssl-1.0.1t.tar.gz

pcre编译安装

/configure
make
make check (非必须)
make install

zlib的编译安装:

./configure
make
make check(非必须)
make install

openssl的编译安装:

需要用到perl5,先安装perl5(坑),然后编译安装openssl

编译安装perl5

./Configure
make
make test(非必须)
make install

编译安装openssl:

/config
make
make depend
make install

 

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$结束$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

编译安装

将nginx-1.8.1.tar.gz拷贝至linux服务器(可以使用ftp传输)

注意:上边将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录
mkdir -p /var/temp/nginx

将文件移动到该目录下

mv nginx-1.8.1.tar.gz /var/temp/nginx

解压:

tar -zxvf nginx-1.8.1.tar.gz
cd nginx-1.8.1

configure

./configure --help查询详细参数(参考本教程附录部分:nginx编译参数)

参数设置如下:

./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

./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

如果安装出现在下面的错误是缺少编译环境。安装编译源码所需的工具和库  
    ./configure: error: C compiler cc is not found  
    yum install gcc gcc-c++ ncurses-devel perl

编译安装

make
make  install

安装完成的目录是

/usr/local/nginx 

进入该目录

 cd /usr/local/nginx 

查看安装完成的目录

启动nginx

cd /usr/local/nginx/sbin/
./nginx

如果启动nginx的时候报错:

[root@node0 sbin]# ./nginx

./nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory

则添加一个软连接,类似于windows中的快捷方式:

ln -s /usr/local/lib/libpcre.so.1 /lib64/

查询nginx进程:

ps aux|grep nginx

5329nginx主进程的进程id5330nginx工作进程的进程id

注意:

执行./nginx启动nginx,这里可以-c指定加载的nginx配置文件,如下:

./nginx -c /usr/local/nginx/conf/nginx.conf

如果不指定-cnginx在启动时默认加载conf/nginx.conf文件,此文件的地址也可以在编译安装nginx时指定./configure的参数(--conf-path= 指向配置文件(nginx.conf))

停止nginx

方式1,快速停止:

cd /usr/local/nginx/sbin
./nginx -s stop

此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。

方式2,完整停止(建议使用)

cd /usr/local/nginx/sbin
./nginx -s quit

此方式停止步骤是待nginx进程处理任务完毕进行停止。

重启nginx

方式1,先停止再启动(建议使用):

对nginx进行重启相当于先停止nginx再启动nginx,即先执行停止命令再执行启动命令。

如下:

./nginx -s quit
./nginx

 

方式2,重新加载配置文件:

当nginx的配置文件nginx.conf修改后,要想让配置生效需要重启nginx,使用-s reload不用先停止nginx再启动nginx即可将配置信息在nginx中生效,如下:

./nginx -s reload

安装测试

如果要在windows浏览器中看到nginx的运行结果,可以直接输入虚拟机IP地址,回车

但是要关掉linux的防火墙:

service iptables stop

此时,nginx输出的是/usr/local/nginx/html/index.html文件到浏览器

修改nginx的默认路径

 cd  /usr/local/nginx/conf
 vi nginx.conf

添加权限:

需要nginx重新加载配置文件:

[root@clc sbin]# ./nginx -s reload

./nginx -s reload
 

开机自启动nginx

编写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.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid
RETVAL=0
prog="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 1
fi
   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" in
start)
        start
        ;;
stop)
        stop
        ;;
reload)
        reload
        ;;
restart)
        stop
        start
        ;;
status)
        status $prog
        RETVAL=$?
        ;;
*)
        echo $"Usage: $prog {start|stop|restart|reload|status|help}"
        exit 1
esac
exit $RETVAL

设置文件的访问权限

chmod a+x /etc/init.d/nginx   (a+x ==> all user can execute  所有用户可执行)

这样在控制台就很容易的操作nginx了:查看Nginx当前状态、启动Nginx、停止Nginx、重启Nginx…

如果修改了nginx的配置文件nginx.conf,也可以使用上面的命令重新加载新的配置文件并运行,可以将此命令加入到rc.local文件中,这样开机的时候nginx就默认启动了

加入到rc.local文件中

vi /etc/rc.local


加入一行  /etc/init.d/nginx start    保存并退出,下次重启会生效。

 
 

https://mp.weixin.qq.com/s/0nc9-T4LiHHyFn1uMBLjyA

 

 

添加权限:

原文地址:https://www.cnblogs.com/chenglc/p/8037813.html