Linux 下Nginx安装

1. 软件下载

安装Nginx需要软件包
将下载好的软件放到 /soft/nginx 目录下。
如果该目录不存在请先创建目录

[root@HM311~]# mkdir nginx

2. 安装

2.1 首先需要安装

[root@HM311~]# yum -y install gcc*
[root@HM311~]# yum install -y pcre pcre-devel
[root@HM311 ~]# yum install -y zlib zlib-devel
[root@HM311~]# yum install -y openssl openssl-devel 

2.2 安装APR、APR-Util、PCRE、Apache Http Server

  • 将这些都安装在/usr/local中,先mkdir目录如下
[root@HM311 soft]# mkdir /usr/local/nginx-1.10.3
[root@HM311 soft]# mkdir /usr/local/openssl-1.1.10
[root@HM311 soft]# mkdir /usr/local/pcre-8.38
[root@HM311 soft]# mkdir /usr/local/zlib-1.2.8
  • 然后步骤如下
[root@HM311 nginx]# tar -zxf nginx-1.10.3.tar.gz
[root@HM311 nginx]# tar -zxf openssl-1.1.0-pre1.tar.gz
[root@HM311 nginx]# tar -zxf zlib-1.2.8.tar.gz
[root@HM311 soft]# tar -jxvf pcre-8.38.tar.bz2

[root@HM311 nginx]# cd pcre-8.38
[root@HM311 pcre-8.38]# ./configure --prefix=/usr/local/pcre-8.38
[root@HM311 pcre-8.38]# make
[root@HM311 pcre-8.38]# make install

[root@HM311 pcre-8.38]# cd ../openssl-1.1.0-pre1
[root@HM311 openssl-1.1.0-pre1]# ./config --prefix=/usr/local/openssl-1.1.10
[root@HM311 openssl-1.1.0-pre1]# make
[root@HM311 openssl-1.1.0-pre1]# make install

[root@HM311 apr-util-1.5.4]# cd ../zlib-1.2.8
[root@HM311 zlib-1.2.8]# ./configure --prefix=/usr/local/zlib-1.2.8
[root@HM311 zlib-1.2.8]# make
[root@HM311 zlib-1.2.8]# make install

[root@HM311 zlib-1.2.8]# cd ../nginx-1.10.3
[root@HM311 nginx-1.10.3]# ./configure --prefix=/usr/local/nginx-1.10.3
[root@HM311 nginx-1.10.3]# make
[root@HM311 nginx-1.10.3]# make install

验证Nginx是否安装成功

[root@HM311 nginx-1.10.3]# cd /usr/local/nginx-1.10.3/sbin
[root@HM311 sbin]# ./nginx -t
nginx: the configuration file /usr/local/nginx-1.10.3/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.10.3/conf/nginx.conf test is successful

启动Nginx

[root@HM311 sbin]# cd /usr/local/nginx-1.10.3/sbin
[root@HM311 sbin]# ./nginx -c /usr/local/nginx-1.10.3/conf/nginx.conf
[root@HM311 sbin]#  ps -ef|grep nginx
root     22117     1  0 15:00 ?        00:00:00 nginx: master process ./nginx -c /usr/local/nginx-1.10.3/conf/nginx.conf
nobody   22118 22117  0 15:00 ?        00:00:00 nginx: worker process
root     22124 31467  0 15:00 pts/0    00:00:00 grep nginx

停止Nginx

[root@HM311 sbin]# ./nginx -s stop
[root@HM311 sbin]# ps -ef|grep nginx
root     22221 31467  0 15:02 pts/0    00:00:00 grep nginx

3. 配置开机启动Nginx

# 编辑/etc/rc.local 添加 /etc/init.d/nginx start
[root@HM311 sbin]# vi /etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local

su oracle -lc "/u01/app/oracle/product/11.2.0/db_1/bin/lsnrctl start"
su oracle -lc  /u01/app/oracle/product/11.2.0/db_1/bin/dbstart

sleep 5
/etc/init.d/nginx start

[root@HM311 sbin]# cd /etc/init.d
[root@HM311 init.d]# vi 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: /usr/local/nginx-1.10.3/logs/nginx.pid
# config: /usr/local/nginx-1.10.3/conf/nginx.conf
nginxd=/usr/local/nginx-1.10.3/sbin/nginx
nginx_config=/usr/local/nginx-1.10.3/conf/nginx.conf
nginx_pid=/usr/local/nginx-1.10.3/logs/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 /usr/local/nginx-1.10.3/logs/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

设置文件权限

[root@iZbp1h0hzrtzkjfh778xnmZ init.d]# chmod a+x /etc/init.d/nginx

#通过该脚本对nginx服务进行管理了:
[root@HM311 init.d]# /etc/init.d/nginx start
Starting nginx:                                            [  OK  ]
[root@HM311 init.d]# ps -ef|grep nginx
root     22927     1  0 15:26 ?        00:00:00 nginx: master process /usr/local/nginx-1.10.3/sbin/nginx -c /usr/local/nginx-1.10.3/conf/nginx.conf
nobody   22928 22927  0 15:26 ?        00:00:00 nginx: worker process
root     22939 31467  0 15:26 pts/0    00:00:00 grep nginx
[root@HM311 init.d]# /etc/init.d/nginx stop
Stopping nginx:                                            [  OK  ]
[root@HM311 init.d]# ps -ef|grep nginx
root     22957 31467  0 15:26 pts/0    00:00:00 grep nginx

把Nginx加入服务

[root@HM311 init.d]# chkconfig --add /etc/init.d/nginx
# 加完这个之后,就可以使用service对nginx进行启动,重启等操作了。

[root@HM311 init.d]# service nginx  start
Starting nginx:                                            [  OK  ]

[root@HM311 init.d]# ps -ef|grep nginx
root     23118     1  0 15:29 ?        00:00:00 nginx: master process /usr/local/nginx-1.10.3/sbin/nginx -c /usr/local/nginx-1.10.3/conf/nginx.conf
nobody   23119 23118  0 15:29 ?        00:00:00 nginx: worker process
root     23124 31467  0 15:29 pts/0    00:00:00 grep ngin

[root@HM311 init.d]# service nginx  stop
Stopping nginx:                                            [  OK  ]

[root@HM311 init.d]# ps -ef|grep nginx
root     23151 31467  0 15:29 pts/0    00:00:00 grep nginx

Nginx安装完成。

猜你喜欢

转载自blog.csdn.net/yk10010/article/details/81238286