文章目录

  • Linux云计算架构-源码安装最新版nginx1.19.2
    • 1. 源码安装nginx1.19.2
    • 2. 配置nginx支持php
    • 3. nginx启动并设置自启动

Linux云计算架构-源码安装最新版nginx1.19.2

1. 源码安装nginx1.19.2

下载地址:http://nginx.org/en/download.html

# 源码包上传
nginx-1.19.2.tar.gz
pcre-8.41.tar.gz
[root@master ~]# ll /usr/local/src/pcre-8.41.tar.gz
-rw-r--r--. 1 root root 2068775 6月  19 14:14 /usr/local/src/pcre-8.41.tar.gz
[root@master ~]# ll /usr/local/src/nginx-1.19.2.tar.gz 
-rw-r--r-- 1 root root 1048727 8月  25 09:09 /usr/local/src/nginx-1.19.2.tar.gz

# 安装依赖包
# nginx提供gzip模块,需要zlib支持
# openssl为nginx提供SSL功能
[root@master ~]# yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel

# 创建一个nginx用户用于运行nginx服务
[root@master ~]# useradd -M -s /sbin/nologin nginx

# 解压源码安装
[root@master ~]# cd /usr/local/src/
[root@master src]# tar xzf nginx-1.19.2.tar.gz
[root@master src]# tar xzf pcre-8.41.tar.gz
[root@master src]# cd nginx-1.19.2/
[root@master nginx-1.19.2]# ./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre=/usr/local/src/pcre-8.41 --user=nginx --group=nginx
# --prefix 指定安装目录
# --with-http_dav_module 启用支持,增加PUT\DELETE\MKCOL,编译开启
# --with-http_stub_status_module 获取nginx上次启动以来的工作状态
# --with-http_addition_module 作用一个输出过滤器,支持不完全缓冲,分部分相应请求
# --with-http_sub_module 允许一些其他文本替换nginx中的一些文本
# --with-http_flv_module 支持flv视频格式
# --with-http_mp4_module 支持mp4视频格式
# --with-pcre pcre源码所在目录
# --user nginx用户
# --group nginx用户组
[root@master nginx-1.19.2]# make -j 4 && make install

# 查看下安装目录,可以看到已经安装完成。
[root@master nginx-1.19.2]# ll /usr/local/nginx/
总用量 0
drwxr-xr-x 2 root root 333 8月  25 09:43 conf
drwxr-xr-x 2 root root  40 8月  25 09:43 html
drwxr-xr-x 2 root root   6 8月  25 09:43 logs
drwxr-xr-x 2 root root  19 8月  25 09:43 sbin

2. 配置nginx支持php

# 备份nginx主配置文件
[root@master ~]# cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
[root@master ~]# vim /usr/local/nginx/conf/nginx.conf
# 修改nginx主配置文件
# 修改用户
 2 user  nginx;
# 去掉65行开始的注释
 65         location ~ \.php$ {
 66             root           html;
 67             fastcgi_pass   127.0.0.1:9000;
 68             fastcgi_index  index.php;
 69             fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 70             include        fastcgi_params;
 71         }

3. nginx启动并设置自启动

# 启动服务(启动脚本启动)
# sbin目录下存放着启动脚本,可直接启动。
[root@master ~]# /usr/local/nginx/sbin/nginx
[root@master ~]# netstat -antup | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      29079/nginx: master 

# 配置环境变量文件
[root@master ~]# vim /etc/profile.d/nginx.sh
export PATH=/usr/local/nginx/sbin:$PATH
# 读取变量或者添加软连接
# 若没有执行权限,可以给+x权限
[root@master ~]# chmod +x /etc/profile.d/nginx.sh
[root@master ~]# /etc/profile.d/nginx.sh 
# 若/etc/profile.d/nginx.sh不生效,可以使用source命令让nginx.sh立即生效。
# source /etc/profile.d/nginx.sh
# 快速启动
[root@master ~]# nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
# 检查配置文件是否有语法错误以及查看版本号
[root@master ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@master ~]# nginx -v
nginx version: nginx/1.19.2

# 生成服务启动脚本
[root@master ~]# vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 99 2
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
 start)
 $PROG
 ;;
 stop)
 kill -3 $(cat $PIDF)
 ;;
 restart)
 $0 stop &> /dev/null
 if [ $? -ne 0 ] ; then continue ; fi
 $0 start
 ;;
 reload)
 kill -1 $(cat $PIDF)
 ;;
 *)
 echo "Userage: $0 { start | stop | restart | reload }"
 exit 1
esac
exit 0
[root@master ~]# chmod +x /etc/init.d/nginx

# 配置服务启动和自启动
[root@master ~]# chkconfig --add nginx
[root@master ~]# chkconfig nginx on

# 开放nginx占用的80端口号
[root@master ~]# firewall-cmd --permanent --zone=public --add-port=80/tcp
success
[root@master ~]# firewall-cmd --reload 
success

# 输入网址:http://192.168.8.184/
# 看到以下界面,则表示安装成功。我这里显示zabbix界面,是因为我之前使用过LNMP架构部署过zabbix,问题不大。

学会部署最新版的nginx,可以对较低版本的nginx进行版本升级。

本文地址:https://blog.csdn.net/weixin_36522099/article/details/108214741