Centos 编译安装 Nginx

本贴最后更新于 897 天前,其中的信息可能已经渤澥桑田

Centos 7 编译安装 Nginx


原文信息:

附上 Nginx 官方地址:


1 更新 yum

yum -y update

2 安装 Nginx 编译所需所有依赖项

yum -y install gcc gcc-c++ make zlib-devel pcre-devel openssl-devel

3 编译我们需要下载源代码。

  1. 官网下载
wget http://nginx.org/download/nginx-1.20.1.tar.gz
  1. github 下载
git clone git@github.com:nginx/nginx.git

4 下载完毕后我们需要解压他们

tar -zxvf nginx-1.20.1.tar.gz

5 进入 Nginx 文件夹

cd nginx-1.20.1

6 选择安装的模块(可选择编译)

./configure \
--user=nginx \
--group=nginx \
--prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-pcre \
--with-file-aio \
--with-http_realip_module \
--without-http_scgi_module \
--without-http_uwsgi_module \
--without-http_fastcgi_module

7 执行编译安装

make & make install

8 我们需要在 init.d 文件夹中创建 nginx 启动文件。 这样每次服务器重新启动 init 进程都会自动启动我们的 Web 服务器

cd /etc/init.d

9 创建 nginx 执行配置文件

touch /etc/init.d/nginx

10 编辑 nginx 执行配置文件

vim nginx
  • 文件内容
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# 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.pid
# user:        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

nginx="/usr/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/etc/nginx/nginx.conf"

lockfile=/var/run/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
    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

添加执行权限

chmod +x /etc/init.d/nginx

11 创建 nginx 用户

useradd -r nginx & chkconfig --add nginx  & chkconfig --level 345 nginx on

12 修改 nginx 配置文件

  1. 打开 nginx 配置文件
vim /etc/nginx/nginx.conf
  1. 修改内容如下
worker_processes  1;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
	
	# ========  以下两行为添加内容  ========
	types_hash_bucket_size 64;
	server_names_hash_bucket_size 128;
	# ========  以上两行为添加内容  ========
	
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    # ======  https ssl证书配置模板 【开始】==========
	#server {
	#   #监听443端口
	#	listen 443;
	#	#你的域名
	#	server_name aleivip.com,www.aleivip.com; 
	#	#SSL开启
	#	ssl on;
	#	#ssl证书的pem文件路径
	#	ssl_certificate  /etc/nginx/cert/www.aleivip.com/5751730_aleivip.com.pem;
	#	#ssl证书的key文件路径
	#	ssl_certificate_key /etc/nginx/cert/www.aleivip.com/5751730_aleivip.com.key;
	#	location / {
	#		proxy_pass  http://公网地址:项目端口号;
	#	}
	#}
    # ======  https ssl证书配置模板 【结束】==========

}

13 启动 nginx

service nginx start

14 验证启动

  • 在浏览器输入请求地址
http://{你的IP}/

13 相关指令

  1. 启动 nginx

    service nginx start

  2. 停止 nginx
    systemctl stop nginx


--- END ---


本文转载请携带原文地址
如有问题欢迎指出

  • 环境部署
    2 引用
  • Linux

    Linux 是一套免费使用和自由传播的类 Unix 操作系统,是一个基于 POSIX 和 Unix 的多用户、多任务、支持多线程和多 CPU 的操作系统。它能运行主要的 Unix 工具软件、应用程序和网络协议,并支持 32 位和 64 位硬件。Linux 继承了 Unix 以网络为核心的设计思想,是一个性能稳定的多用户网络操作系统。

    915 引用 • 931 回帖 • 1 关注
  • CentOS

    CentOS(Community Enterprise Operating System)是 Linux 发行版之一,它是来自于 Red Hat Enterprise Linux 依照开放源代码规定释出的源代码所编译而成。由于出自同样的源代码,因此有些要求高度稳定的服务器以 CentOS 替代商业版的 Red Hat Enterprise Linux 使用。两者的不同在于 CentOS 并不包含封闭源代码软件。

    238 引用 • 224 回帖 • 1 关注
  • NGINX

    NGINX 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 NGINX 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本 0.1.0 发布于 2004 年 10 月 4 日。

    311 引用 • 546 回帖 • 34 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...