ngnix 安装

1安装PCRE库

  ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ 下载最新的 PCRE 源码包,使用下面命令下载编译和安装 PCRE 包:

cd /data/apps/
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.42.tar.gz
tar -zxvf  pcre-8.42.tar.gz
cd pcre-8.42
./configure
make
make install

查看是否安装pcre    

 rpm    -qa   pcre  

安装pcre

http://mirror.centos.org/centos/6/os/x86_64/Packages/pcre-7.8-7.el6.x86_64.rpm
https://centos.pkgs.org/6/centos-x86_64/pcre-7.8-7.el6.x86_64.rpm.html
rpm -ivh pcre-7.8-6.el6.x86_64.rpm

卸载pcre

 rpm   -e  --nodeps    pcre   

2 有网络的情况下 

2-2、安装编译开发工具类库

   用yum安装、更新开发工具"Development Tools"和"Server Platform Deveopment",而nginx会依赖openssl-devel和pcre-devel类库,安装如下:

[root@node2 nginx]# yum groupinstall "Development Tools" "Server Platform Deveopment"
[root@node2 ~]# yum install openssl-devel pcre-devel

2-3、创建用户和用户组

 分别创建名为"nginx"的用户和组,用来运行nginx的worker进程,操作如下:

[root@node2 nginx]# groupadd -r nginx
[root@node2 nginx]# useradd -r -g nginx nginx

2-4、编译并安装

      安装nginx

  Nginx 一般有两个版本,分别是稳定版和开发版,您可以根据您的目的来选择这两个版本的其中一个

cd /data/apps
wget http://nginx.org/download/nginx-1.14.1.tar.gz
tar -zxvf nginx-1.14.1.tar.gz 
cd nginx
-1.14.1

    先configure指定编译选项,如安装目录、上面创建的运行用户、需要的扩展模块(SSL、FastCGI)等,选项及参数说明:http://nginx.org/en/docs/configure.html,操作如下:

    [root@node2 nginx]# ./configure \
--prefix=/usr \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-pcre

      Configure成功如下:

之后进行安装

make && make install

  

2-5、为nginx提供SysV init服务脚本

  先创建/etc/init.d/nginx服务脚本,这基于ngInx自身提供的命令实现的,脚本内容如下:

vim  /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/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx 
make_dirs() {
    # make required directories
    user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
    options=`$nginx -V 2>&1 | grep 'configure arguments:'`
    for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
    value=`echo $opt | cut -d "=" -f 2`
    if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value 
    fi 
fi 
    done 
}
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    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 
}
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@node2 nginx]# vim /etc/init.d/nginx
[root@node2 nginx]# chmod +x /etc/init.d/nginx
[root@node2 nginx]# chkconfig --add nginx
[root@node2 nginx]# chkconfig nginx on
[root@node2 nginx]# chkconfig --list nginx

2-6、启动并访问测试

    启动nginx,查看网络状态,可以看到nginx正在监听80端口;用测试主机访问nginx主机的IP,可以看到nginx的欢迎页面,过程如下:

[root@node2 nginx]# service nginx start
[root@node2 nginx]# netstat -ntulp | grep nginx

如果启动之后发现报错,错误信息为

[root@XXXXXXXX  nginx-1.14.1]# service nginx start
Starting nginx: /usr/sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory

查询 ll /usr/local/lib  看一看这个目录 如果有就关联上

如果是32位系统

[root@lee ~]#  ln -s /usr/local/lib/libpcre.so.1 /lib

如果是64位系统

[root@lee ~]#  ln -s /usr/local/lib/libpcre.so.1 /lib64

然后在启动nginx就OK了

service nginx start 

猜你喜欢

转载自www.cnblogs.com/jack-Star/p/9930157.html