Linux安装配置Nginx

之所以搭建Nginx,是因为要做一个图片服务器,之前已经搭建好了Ftp,要想实现通过网页的src标签显示图片需要,搭建web服务器(虽然也可以通过在img标签中的src属性里面写“ ftp://用户名@密码/路径 这种方式来访问图片,但是这种方式并不安全,直接暴露了用户名密码和图片的存放路径,故不采用”),这里选用Nginx,主要的原理是,通过Nginx监听端口,将Ftp的根目录映射到Nginx配置的Location中

实现http访问。

1 Nginx简介
Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名

在CentOS安装软件的时候,可能缺少一部分支持库,而报错。这里首先安装系统常用的支持库。那么在安装的时候就会减少很多的错误的出现

[root@instance_4dbde0 conf]#  yum install -y gcc gdb strace gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs patch e2fsprogs-devel krb5-devel libidn libidn-devel openldap-devel nss_ldap openldap-clients openldap-servers libevent-devel libevent uuid-devel uuid mysql-devel

安装Nginx需要检查Linux是否安装了linux常用必备支持库。检查是否安装了g++、gcc。rpm -qa | grep gcc 之后需要出现3个包如下图所示。如果没有出现。需要安装g++、gcc。

如果没有出现图片所示内容,使用命令安装支持库

[root@instance_4dbde0 conf]#  yum install gcc-c++

判断系统是否安装zlib-devel。如果没有安装。使用命令

[root@instance_4dbde0 pcre-8.00]# yum install -y zlib-devel

 判断系统中是否安装openssl-devel,安装后nginx可以使用加密服务

安装命令

[root@instance_4dbde0 local]# yum install openssl-devel -y

安装完成后,开始下载其他支持组件

#正则表达式库下载
[root@instance_4dbde0 conf]# wget https://ftp.pcre.org/pub/pcre/pcre-8.00.tar.gz  
#最新版下载地址:https://sourceforge.net/projects/pcre/files/pcre/

  

#解压
[root@instance_4dbde0 conf]# cd /usr/local/
[root@instance_4dbde0 local]# pwd
/usr/local
[root@instance_4dbde0 local]# ls
bin                   etc    include     jdk1.7  lib64    logs          nexus-2.11.2-03  pcre-8.00         sbin   sonatype-work  tomcat7    zookeeper-3.3.6
dubbo-governance.log  games  index.html  lib     libexec  mysql-5.7.20  nginx-1.8.0      pcre-8.00.tar.gz  share  src            tomcat7_2
[root@instance_4dbde0 local]# tar -zxvf pcre-8.00
#编译安装
[root@instance_4dbde0 pcre-8.00]# ./configure 
[root@instance_4dbde0 pcre-8.00]# make
[root@instance_4dbde0 pcre-8.00]# make install

 安装Nginx

#下载Nginx,并解压
[root@instance_4dbde0 pcre-8.00]# cd /usr/local/
[root@instance_4dbde0 local]# wget http://nginx.org/download/nginx-1.8.0.tar.gz
[root@instance_4dbde0 local]# tar -zxvf nginx-1.8.0.tar.gz
[root@instance_4dbde0 local]# cd nginx-1.8.0/

编译。安装

[root@instance_290388 nginx-1.8.0]# ./configure --user=www --group=www --prefix=/usr/local/nginx-1.8.0 --with-http_stub_status_module --with-http_ssl_module 
--with-http_realip_module --with-http_gzip_static_module 
--with-pcre=/usr/local/pcre-8.00 --conf-path=/usr/local/nginx-1.8.0/nginx.conf
[root@instance_290388 nginx-1.8.0]# make
[root@instance_290388 nginx-1.8.0]# make install

  

检查是否安装成功

[root@instance_4dbde0 local]# cd /usr/local/nginx-1.8.0/sbin/
[root@instance_4dbde0 sbin]# ./nginx -t

 

 首次重启需要使用命令

[root@instance_4dbde0 sbin]# /usr/local/nginx-1.8.0/sbin/nginx -c /usr/local/nginx-1.8.0/conf/nginx.conf
[root@instance_4dbde0 sbin]#  /usr/local/nginx-1.8.0/sbin/nginx -s reload


#① 启动Nginx:
[root@instance_4dbde0 sbin]# ./nginx
#① 方式1,快速停止:
[root@instance_4dbde0 sbin]# ./nginx -s stop
②方式2,完整停止(建议使用):
[root@instance_4dbde0 sbin]# ./nginx -s quit

#重启nginx
#①方式1,先停止再启动(建议使用):
[root@instance_4dbde0 sbin]# ./nginx -s quit
[root@instance_4dbde0 sbin]# ./nginx 
#②方式2,重新加载配置文件:当nginx的配置文件nginx.conf修改后,要想让配置生效需要重启nginx,使用-s reload不用先停止nginx再启动nginx即可将配置信息在nginx中生效,如下:
[root@instance_4dbde0 sbin]# ./nginx -s reload

开机自启动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.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

:wq 保存并退出

设置文件的访问权限

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

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

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

加入到rc.local文件中

vi /etc/rc.local

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

原文地址:https://www.cnblogs.com/kanyun/p/8119454.html