【nginx】nginx编译安装

nginx介绍

Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。
其特性占有内存少,并发能力强,稳定性好,有丰富的功能集和低系统资源的消耗。
官方文档:http://nginx.org/

为什么要使用编译安装

编译安装可以自定义选择需要的模块,不需要的模块可以不添加,这样性能更高,安全性、稳定性也更高。

编译安装详细步骤

解决软件的依赖关系,需要安装相关软件包

yum install epel* -y
yum -y install wget make zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc gcc-c++ autoconf automake psmisc net-tools lsof vim geoip geoip-devel
  • epel源:Linux操作系统提供额外的软件包
  • wget:用来从指定的URL下载文件
  • make:用来编译的,它从Makefile中读取指令,然后编译
  • zlib,zlib-devel:提供数据压缩用的函式库
  • openssl,openssl-devel:开源的安全套接字层的密码库
  • pcre,pcre-devel:perl语言兼容正则表达式
  • gcc,gcc-c++:C/C++编译器
  • autoconf:一个产生可以自动配置源代码包,生成shell脚本的工具
  • automake:一个自动产生Makefile 文件的软件
  • psmisc:一个关于进程相关的软件扩展包
  • net-tools:网络工具包
  • lsof:一个列出当前系统打开文件的工具
  • geoip,geoip-devel:通过来访者的IP定位他的经纬度,国家/地区,省市的库

新建管理nginx的用户和组

id  pp || useradd pp -s /sbin/nologin

下载并解压nginx

#下载nginx软件
mkdir  /pp -p
cd /pp
wget  http://nginx.org/download/nginx-1.21.1.tar.gz

#解压软件
tar xf nginx-1.21.1.tar.gz 

配置自己所需要的nginx的功能和相关配置

#进入解压后的文件夹
cd nginx-1.21.1
./configure --prefix=/usr/local/scpp  --user=pp --group=pp  --with-http_ssl_module   --with-threads  --with-http_v2_module  --with-http_stub_status_module  --with-stream  --with-http_geoip_module --with-http_realip_module
  • –prefix=/usr/local/scpp:nginx编译安装的目录
  • –user=pp :管理nginx的用户
  • –group=pp 管理nginx的组
  • –with-http_ssl_module:启用https支持
  • –with-threads :启用thread pool支持
  • –with-http_v2_module: 启用ngx_http_v2_module支持
  • –with-http_stub_status_module: 支持查看nginx的状态页
  • –with-stream:负载均衡
  • –with-http_geoip_module :编译的MaxMind数据库解析客户端IP地址的模块
  • –with-http_realip_module :在nginx访问日志中去除代理IP,显示客户的真实IP

检查前期工作

#如果上面的编译前的配置失败,直接退出脚本
if (( $? != 0));then
	exit
fi

编译安装

#编译
make -j 2
#编译安装
make  install

修改环境变量

#修改PATH变量
echo  "PATH=$PATH:/usr/local/scpp/sbin" >>/root/.bashrc
#执行修改了环境变量的脚本
source /root/.bashrc

关闭防火墙和selinux

#stop firewall和设置下次开机不启动firewalld
service firewalld stop
systemctl disable firewalld

#临时停止selinux和永久停止selinux
setenforce 0
sed  -i '/^SELINUX=/ s/enforcing/disabled/' /etc/selinux/config

设置开机自启

#开机启动
chmod +x /etc/rc.d/rc.local
echo  "/usr/local/scpp/sbin/nginx" >>/etc/rc.local

根据自己的要求修改配置文件

#修改nginx.conf的配置,例如:端口号,worker进程数,线程数,服务域名

sed  -i '/worker_processes/ s/1/2/' /usr/local/scpp/conf/nginx.conf
sed  -i  '/worker_connections/ s/1024/2048/' /usr/local/scpp/conf/nginx.conf
sed  -i -r '37c \\tserver_name www.pp.com;' /usr/local/scpp/conf/nginx.conf

启动nginx

#启动nginx
/usr/local/scpp/sbin/nginx

查看服务是否启动

查看进程

ps aux|grep nginx

在这里插入图片描述

查看端口

方法一:

netstat -anplut|grep nginx

在这里插入图片描述
方法二:

lsof -i:80

在这里插入图片描述

访问服务

在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值