nginx 编译安装与配置详解

简介:

一、编译安装

1、使用yum安装所需的包,虽然需要编译几个依赖包,pcre、zlib、openssl

yum -y groupinstall "Development Tools""Server Platform Development"

yum install pcre-devel zlib-devel openssl-devel gcc* (一般系统装好了,以下的这几个包openssl、zlib、pcre都已经安装上了,注意: pcre-devel zlib-devel openssl-devel这几个devel包要装上,不然编译的时候会报错)


2、添加用户和组

groupadd -r nginx

useradd -r -g nginx nginx

 

3、解压安装文件

tar -zxvf nginx-1.10.1.tar.gz

cd nginx-1.10.1


4、配置选项

./configure --prefix=/usr/local/nginx #nginx家目录

--sbin-path=/usr/sbin #nginx主程序路径

--conf-path=/etc/nginx/nginx.conf #nginx主配置文件路径

--error-log-path=/var/log/nginx/error.log #错误日志路径

--http-log-path=/var/log/nginx/access.log #访问日志路径

--pid-path=/var/run/nginx/nginx.pid #进程ID路径

--lock-path=/var/lock/nginx.lock #nginx锁文件路径

--user=nginx #worker进程所属的用户名称

--group=nginx #worker进程所属的组名称

--with-http_ssl_module #启用http_ssl模块

--with-http_flv_module #启用服务端伪流媒体模块

--with-http_stub_status_module #启用健康检查模块

--with-http_gzip_static_module #启用ngnix 的静态缓存模块

--http-client-body-temp-path=/var/tmp/nginx/client #启用为http连接的请求实体临时文件设置路径

--http-proxy-temp-path=/var/tmp/nginx/proxy #为http代理临时文件设置路径

--http-fastcgi-temp-path=/var/tmp/nginx/fcgi #为http fastcgi临时文件设置路径

--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi #为http uwcgi临时文件设置路径

--http-scgi-temp-path=/var/tmp/nginx/scgi  #为http scgi临时文件设置路径

--with-pcre #使用pcre库文件

--with-stream #启用TCP/UDP代理模块



运行配置检查:

Configuration summary

  + using system PCRE library

  + using system OpenSSL library

  + md5: using OpenSSL library

  + sha1: using OpenSSL library

  + using system zlib library

 

  nginx path prefix: "/usr/local/nginx"

  nginx binary file: "/usr/sbin/nginx"

  nginx modules path: "/usr/local/modules"

  nginx configuration prefix: "/etc/nginx"

  nginx configuration file: "/etc/nginx/nginx.conf"

  nginx pid file: "/var/run/nginx/nginx.pid"

  nginx error log file: "/var/log/nginx/error.log"

  nginx http access log file: "/var/log/nginx/access.log"

  nginx http client request body temporary files: "/var/tmp/nginx/client/"

  nginx http proxy temporary files: "/var/tmp/nginx/proxy/"

  nginx http fastcgi temporary files: "/var/tmp/nginx/fcgi/"

  nginx http uwsgi temporary files: "/var/tmp/nginx/uwsgi"

  nginx http scgi temporary files: "/var/tmp/nginx/scgi

 

5、编译和安装

make && make install


6、在/var/tmp目录下建立nginx目录

mkdir -p /var/tmp/nginx


7、检查配置文件是否有语法错误

[root@localhost ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful


8、编辑服务启动脚本

vim /etc/init.d/nginx

#! /bin/bash

#

# 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

# pidfile:       /var/run/nginx/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/nginx.lock

start() {

  [ -x $nginx ] || exit 5

  [ -f $NGINX_CONF_FILE ] || exit 6

  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


9、启动nginx

[root@localhost init.d]# service nginx start
正在启动 nginx:                                           [确定]

10、停止nginx

[root@localhost init.d]# service nginx stop
停止 nginx:                                               [确定]



11、查看进程情况

[root@localhost init.d]# ps -ef|grep nginx
root      10342      1  0 02:04 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     10344  10342  0 02:04 ?        00:00:00 nginx: worker process                   
root      10350   1613  0 02:07 pts/0    00:00:00 grep nginx

12、查看监听的端口

[root@localhost init.d]# netstat -tnlp|grep nginx
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      10342/nginx 


13、查看测试页

wKiom1gu07XAJvUFAAC1oYZcZtU172.png-wh_50

本文转自服务器运维博客51CTO博客,原文链接http://blog.51cto.com/shamereedwine/1874601如需转载请自行联系原作者


neijiade10000

相关文章
|
4月前
|
缓存 应用服务中间件 网络安全
Nginx中配置HTTP2协议的方法
Nginx中配置HTTP2协议的方法
249 7
|
2月前
|
存储 应用服务中间件 Linux
nginx配置证书和私钥进行SSL通信验证
nginx配置证书和私钥进行SSL通信验证
69 4
|
4月前
|
负载均衡 监控 应用服务中间件
配置Nginx反向代理时如何指定后端服务器的权重?
配置Nginx反向代理时如何指定后端服务器的权重?
243 61
|
4月前
|
安全 应用服务中间件 网络安全
如何测试Nginx反向代理实现SSL加密访问的配置是否正确?
如何测试Nginx反向代理实现SSL加密访问的配置是否正确?
255 60
|
4月前
|
安全 应用服务中间件 网络安全
配置Nginx反向代理实现SSL加密访问的步骤是什么?
我们可以成功地配置 Nginx 反向代理实现 SSL 加密访问,为用户提供更安全、可靠的网络服务。同时,在实际应用中,还需要根据具体情况进行进一步的优化和调整,以满足不同的需求。SSL 加密是网络安全的重要保障,合理配置和维护是确保系统安全稳定运行的关键。
332 60
|
3月前
|
存储 应用服务中间件 nginx
nginx反向代理bucket目录配置
该配置实现通过Nginx代理访问阿里云OSS存储桶中的图片资源。当用户访问代理域名下的图片URL(如 `http://代理域名/123.png`)时,Nginx会将请求转发到指定的OSS存储桶地址,并重写路径为 `/prod/files/2024/12/12/123.png`。
118 5
|
4月前
|
缓存 负载均衡 算法
如何配置Nginx反向代理以实现负载均衡?
如何配置Nginx反向代理以实现负载均衡?
|
4月前
|
存储 负载均衡 中间件
Nginx反向代理配置详解,图文全面总结,建议收藏
Nginx 是大型架构必备中间件,也是大厂喜欢考察的内容,必知必会。本篇全面详解 Nginx 反向代理及配置,建议收藏。
Nginx反向代理配置详解,图文全面总结,建议收藏
|
3月前
|
负载均衡 前端开发 应用服务中间件
负载均衡指南:Nginx与HAProxy的配置与优化
负载均衡指南:Nginx与HAProxy的配置与优化
199 3
|
4月前
|
应用服务中间件 网络安全 nginx
轻松上手Nginx Proxy Manager:安装、配置与实战
Nginx Proxy Manager (NPM) 是一款基于 Nginx 的反向代理管理工具,提供直观的 Web 界面,方便用户配置和管理反向代理、SSL 证书等。本文档介绍了 NPM 的安装步骤,包括 Docker 和 Docker Compose 的安装、Docker Compose 文件的创建与配置、启动服务、访问 Web 管理界面、基本使用方法以及如何申请和配置 SSL 证书,帮助用户快速上手 NPM。
1328 1