centos7_ linux : Nginx安装手册

一: nginx安装环境

1: oracle vm虚拟机+Centos7系统的yum环境的安装

  1 配置本地yum库(用root用户操作)
  2     创建挂载目录
  3    
mkdir /mnt/cdrom
  4  查看挂载目录
  5      
ls /mnt/cdrom
  6 把oracle linux7.iso安装 系统文件挂载
  7      
mount /dev/sr0 /mnt/cdrom/
  8  查看挂载目录
  9     
ls /mnt/cdrom
 10 进入yum 文件
 11     
cd /etc/yum.repos.d/
 12 查询文件信息
 13  [root@localhost yum.repos.d]#     ls
 14 备份系统文件
 15  [root@localhost yum.repos.d]#     mv public-yum-ol7.repo public-yum-ol7.repo.bak
 16 查询文件信息
 17  [root@localhost yum.repos.d]#     ls
 18 创建新的 public-yum-ol7.repo 文件
 19  [root@localhost yum.repos.d]#     touch public-yum-ol7.repo
 20 编辑 public-yum-ol7.repo 文件
 21  [root@localhost yum.repos.d]#     vi public-yum-ol7.repo
 22 编写内容为:
 23 [server]
 24 name=Oracle  Linux7
 25 baseurl=file:///mnt/cdrom
 26 enabled=1
 27 gpgcheck=0
 28 
 29  检测是否存在 yum安装信息
 30   [root@localhost yum.repos.d]#    yum list all
 31 

2:gcc  

  1 
  2 安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,
  3 需要安装gcc: 
yum install gcc-c++

检查 gcc的版本:

clip_image002

3: PCRE

PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。

  1  [root@localhost yum.repos.d]#     yum install -y pcre pcre-devel

检查是否安装了pcre:

clip_image002[9]

注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。

4: zlib

zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。

  1  [root@localhost yum.repos.d]#     yum install -y zlib zlib-devel

5:openssl

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。

nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。

  1  [root@localhost yum.repos.d]#  yum install -y openssl openssl-devel

 

 

二: 编译安装

1:将 nginx-1.8.0.tar.gz  拷贝至linux服务器。

解压:

  1 [root@localhost yum.repos.d]# tar -zxvf nginx-1.8.0.tar.gz
  2 
  3 [root@localhost yum.repos.d]#  cd nginx-1.8.0
  4 

2:配置参数

1、 configure

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

  1 参数设置如下:
  2 [root@localhost ]#  ./configure 
  3 --prefix=/usr/local/nginx 
  4 --pid-path=/var/run/nginx.pid 
  5 --lock-path=/var/lock/nginx.lock 
  6 --error-log-path=/var/log/nginx/error.log 
  7 --http-log-path=/var/log/nginx/access.log 
  8 --with-http_gzip_static_module 
  9 --http-client-body-temp-path=/var/temp/nginx/client 
 10 --http-proxy-temp-path=/var/temp/nginx/proxy 
 11 --http-fastcgi-temp-path=/var/temp/nginx/fastcgi 
 12 --http-uwsgi-temp-path=/var/temp/nginx/uwsgi 
 13 --http-scgi-temp-path=/var/temp/nginx/scgi
 14 
 15 注意:上边将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录
 16 

3: 编译安装

  1 [root@localhost ]#  make
  2 [root@localhost ]#  make  install
  3 

安装成功查看安装目录 :

clip_image001

4: 启动nginx

  1 [root@localhost ]# cd /usr/local/nginx/sbin/
  2 [root@localhost ]# ./nginx 
  3 

查询nginx进程:

clip_image002[11]

15098是nginx主进程的进程id,15099是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))

5: 停止nginx

  1 方式1,快速停止:
  2 [root@localhost ]# cd /usr/local/nginx/sbin
  3 [root@localhost ]#  ./nginx -s stop
  4 此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。
  5 
  6 方式2,完整停止(建议使用):
  7 [root@localhost ]# cd /usr/local/nginx/sbin
  8 [root@localhost ]# ./nginx -s quit
  9 此方式停止步骤是待nginx进程处理任务完毕进行停止。
 10 

6: 重启nginx

  1 方式1,先停止再启动(建议使用):
  2 对nginx进行重启相当于先停止nginx再启动nginx,即先执行停止命令再执行启动命令。
  3 如下:
  4 [root@localhost ]# ./nginx -s quit
  5 [root@localhost ]# ./nginx
  6 
  7 方式2,重新加载配置文件:
  8 当nginx的配置文件nginx.conf修改后,要想让配置生效需要重启nginx,使用-s reload不用先停止nginx再启动nginx即可将配置信息在nginx中生效,如下:
  9 [root@localhost ]#  ./nginx -s reload
 10 

7: 测试

nginx安装成功,启动nginx,即可访问虚拟机上的nginx:

clip_image002[13]

到这说明nginx上安装成功。

 

8: 开机自启动nginx

  8-1:编写shell脚本

  1 这里使用的是编写shell脚本的方式来处理
  2 
  3 [root@localhost ]#  vi /etc/init.d/nginx (输入下面的代码)
  4 

  编辑脚本如下:

  1 #!/bin/bash
  2 # nginx Startup script for the Nginx HTTP Server
  3 # it is v.0.0.2 version.
  4 # chkconfig: - 85 15
  5 # description: Nginx is a high-performance web and proxy server.
  6 #              It has a lot of features, but it's not for everyone.
  7 # processname: nginx
  8 # pidfile: /var/run/nginx.pid
  9 # config: /usr/local/nginx/conf/nginx.conf
 10 nginxd=/usr/local/nginx/sbin/nginx
 11 nginx_config=/usr/local/nginx/conf/nginx.conf
 12 nginx_pid=/var/run/nginx.pid
 13 RETVAL=0
 14 prog="nginx"
 15 # Source function library.
 16 . /etc/rc.d/init.d/functions
 17 # Source networking configuration.
 18 . /etc/sysconfig/network
 19 # Check that networking is up.
 20 [ ${NETWORKING} = "no" ] && exit 0
 21 [ -x $nginxd ] || exit 0
 22 # Start nginx daemons functions.
 23 start() {
 24 if [ -e $nginx_pid ];then
 25    echo "nginx already running...."
 26    exit 1
 27 fi
 28    echo -n $"Starting $prog: "
 29    daemon $nginxd -c ${nginx_config}
 30    RETVAL=$?
 31    echo
 32    [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
 33    return $RETVAL
 34 }
 35 # Stop nginx daemons functions.
 36 stop() {
 37         echo -n $"Stopping $prog: "
 38         killproc $nginxd
 39         RETVAL=$?
 40         echo
 41         [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
 42 }
 43 # reload nginx service functions.
 44 reload() {
 45     echo -n $"Reloading $prog: "
 46     #kill -HUP `cat ${nginx_pid}`
 47     killproc $nginxd -HUP
 48     RETVAL=$?
 49     echo
 50 }
 51 # See how we were called.
 52 case "$1" in
 53 start)
 54         start
 55         ;;
 56 stop)
 57         stop
 58         ;;
 59 reload)
 60         reload
 61         ;;
 62 restart)
 63         stop
 64         start
 65         ;;
 66 status)
 67         status $prog
 68         RETVAL=$?
 69         ;;
 70 *)
 71         echo $"Usage: $prog {start|stop|restart|reload|status|help}"
 72         exit 1
 73 esac
 74 exit $RETVAL
 75 
  1 [root@localhost ]#  :wq  保存并退出

8-2:设置文件的访问权限

  1 [root@localhost ]#  chmod a+x /etc/init.d/nginx      
  2 # (a+x ==> all user can execute  所有用户可执行)
  3 

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

clip_image001[5]

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

8-3:加入到rc.local文件中

  1 [root@localhost ]#   vi /etc/rc.local
  2 
  3 #加入一行 
  4 [root@localhost ]#    /etc/init.d/nginx start 
  5 #保存并退出,下次重启会生效。
  6 
  7 
  8 #别忘记了(该命令保证了在启动后自动启动服务):
  9 [root@localhost ]#   chkconfig nginx on 
 10 #以后可以以下熟悉的命令管理:
 11 [root@localhost ]#  service nginx status
 12 [root@localhost ]#  service nginx start
 13 [root@localhost ]#  service nginx stop
 14 [root@localhost ]#  service nginx restart
 15 

 

 

 9:关于如何能否访问到ftp用户上传的ftp服务器的图片资源,并成功显示出来”敬请参考下文

centos7 nginx图片 服务器可以访问ftp用户上传的图片资源的配置

 

 

 

 

 

 

 

 

 

 

 

 

原文地址:https://www.cnblogs.com/ios9/p/linux_nginx_install.html