编译安装nginx
前言
nginx安装简单的不行,官网也非常简洁,但是nginx也有很丰富的玩法,各种配置功能非常推荐去了解。
http://nginx.org/en/docs/
文档有nginx的各种用法,而且配置项也有丰富的描述,包括install nginx。
1、官网获取源码包
http://nginx.org/en/download.html
nginx-1.19.6.tar
2、解压
tar -xf nginx-1.19.6.tar
3、进入编译目录
cd nginx-1.19.6
4、安装依赖
yum install pcre-devel openssl-devel make gcc -y
5、编译前的配置
./configure --prefix=/usr/local/nginx --with-http_realip_module --with-http_ssl_module --with-pcre --with-stream --with-http_stub_status_module
解析:编译配置是我们编译安装的主要目的,为了定制nginx,选用需要的功能进行最小化安装,在这里初步介绍下相关配置,具体配置功能参考官方文档:
http://nginx.org/en/docs/
也可以查看配置帮助:
./configure --help
–prefix定义安装路径,无需多说
–with-启用配置项,后面接需要配置的模版
注:模块不能后期修改配置文件加载,只能在编译安装时选择开启
介绍一些模块:
1、realip:使后端获取访问服务器的真实ip,通过修改http协议生效
2、stream:支持四层负载均衡,http协议默认在应用层,即七层负载均衡
3、ssl:开启https安全访问
4、stub_status:开启后可以打开状态统计功能
5、auth_basic:开启后可以打开访问认证功能
6、rewrite:打开后开启重定向功能
7、limit_conn:打开后开启限速功能
8、limit_seq:访问次数(频率)限制,防爬虫
6、编译及安装
make && make install
7、修改PATH变量,使系统能够找到并执行nginx
PATH=$PATH:/usr/local/nginx/sbin
echo "PATH=$PATH:/usr/local/nginx/sbin" >>/etc/bashrc
8、启动
/usr/local/nginx/sbin/nginx
9、验证
[root@localhost ~]# ps aux|grep nginx
root 6708 0.0 0.0 46092 1144 ? Ss 09:28 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 6709 0.0 0.1 46532 2132 ? S 09:28 0:00 nginx: worker process
root 6719 0.0 0.0 112824 976 pts/0 S+ 09:44 0:00 grep --color=auto nginx
进程成功开启
[root@localhost ~]# curl 192.168.0.12
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
主机内成功访问
10、其他可选安装配置
1、设置开机自启动
echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local
chmod +x /etc/rc.d/rc.local
2、关闭防火墙及selinux使其他主机能访问
service firewalld stop
systemctl disable firewalld
setenforce 0
sed -i 's/=enforcing/=disabled/g' /etc/selinux/config
浏览器可成功访问: