微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

nginx的搭建

编译安装

安装Tengine-2.3.0版本

1、下载淘宝Nginx
[root@web-7 /opt/tngx]#wget https://tengine.taobao.org/download/tengine-2.3.3.tar.gz
2、下载 C语言,golang语言环境
yum install -y gcc make gcc-c++ ncurses-devel
yum install golang -y
3   解压缩tengine-2.3.3.tar.gz
[root@localhost tenx230]# tar -zxvf tengine-2.3.3.tar.gz 
4、需要linux系统支持https的模块,就是安装openssl模块
yum -y install openssl openssl-devel pcre pcre-devel zlib zlib-devel
执行编译参数,让Nginx的安装,可以扩展其他功能指明路径
[root@localhost tengine-2.3.3]# ./configure  --prefix=/opt/tngx/  --with-http_ssl_module
5、开始编译安装
make &&make install
6、 编译成功后就会全部在/opt/tngx230
[root@localhost tengine-2.3.3]# ls /opt/tngx230/
conf  html  logs  sbin
[root@localhost tengine-2.3.3]# 

7、开启Nginx的ssl功能

[root@localhost ~]# /opt/tngx230/sbin/Nginx -V
Tengine version: Tengine/2.3.3
Nginx version: Nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/opt/tngx230/ --with-http_ssl_module
[root@localhost ~]# 
Nginx官网
https://Nginx.org/en/docs/configure.html
步骤
1、创建www用户
[root@web-7 ~]#groupadd www -g 666
[root@web-7 ~]#useradd www -u 666 -g 666 -M -s /sbin/nologin

2、安装依赖
[root@web-7 ~]#yum install pcre pcre-devel openssl openssl-devel  zlib zlib-devel gzip  gcc gcc-c++ make wget httpd-tools vim -y
3、下载源码 https://Nginx.org/download/,且解压缩
[root@web-7 ~]#cd /opt
[root@web-7 /opt]#mkdir ngx
[root@web-7 /opt]#ls
network_init.sh  ngx
[root@web-7 /opt]#cd ngx/
[root@web-7 /opt/ngx]#

[root@web-7 /opt/ngx]#wget https://Nginx.org/download/Nginx-1.20.2.tar.gz
[root@web-7 /opt/ngx]#ls
Nginx-1.20.2.tar.gz
解压
[root@web-7 /opt/ngx]#tar -zxf Nginx-1.20.2.tar.gz 
[root@web-7 /opt/ngx]#ls
Nginx-1.20.2  Nginx-1.20.2.tar.gz
[root@web-7 /opt/ngx]#


4、编译参数
[root@web-7 /opt/ngx]#cd Nginx-1.20.2/
[root@web-7 /opt/ngx/Nginx-1.20.2]#ls
auto     CHANGES.ru  configure  html     man     src
CHANGES  conf        contrib    LICENSE  README
[root@web-7 /opt/ngx/Nginx-1.20.2]#./configure --user=www --group=www --prefix=/opt/Nginx-1.20.2 --with-http_stub_status_module --with-http_ssl_module --with-pcre

5、编译安装
[root@web-7 /opt/ngx/Nginx-1.20.2]#make && make install
检查语法
[root@web-7 /opt]#/opt/Nginx-1.20.2/sbin/Nginx -t
Nginx: the configuration file /opt/Nginx-1.20.2/conf/Nginx.conf Syntax is ok
Nginx: configuration file /opt/Nginx-1.20.2/conf/Nginx.conf test is successful

启动Nginx
-c 参数指定配置文件启动
[root@web-7 /opt]#/opt/Nginx-1.20.2/sbin/Nginx -c /opt/Nginx-1.20.2/conf/Nginx.conf
[root@web-7 /opt]#ps -ef | grep Nginx
root       8142      1  0 23:34 ?        00:00:00 Nginx: master process /opt/Nginx-1.20.2/sbin/Nginx -c /opt/Nginx-1.20.2/conf/Nginx.conf
www        8143   8142  0 23:34 ?        00:00:00 Nginx: worker process
root       8149   2589  0 23:35 pts/0    00:00:00 grep --color=auto Nginx


测试Nginx

停止Nginx
[root@web-7 /opt]#/opt/Nginx-1.20.2/sbin/Nginx -s stop
[root@web-7 /opt]#ps -ef | grep Nginx
root       8159   2589  0 23:37 pts/0    00:00:00 grep --color=auto Nginx

yum源安装

1、epel源安装
1.配置阿里云yum epel源
2.yum install Nginx -y
2、Nginx
官网
https://Nginx.org/en/linux_packages.html#RHEL-CentOS
1、安装yum工具包
yum install yum-utils -y
2.设置yum仓库
cat > /etc/yum.repos.d/Nginx.repo << 'EOF'
[Nginx-stable]
name=Nginx stable repo
baseurl=http://Nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://Nginx.org/keys/Nginx_signing.key
EOF
3、yum安装的Nginx服务
[root@web-7 /etc/yum.repos.d]#yum clean all
[root@web-7 ~]#yum install Nginx -y

4、启动Nginx
[root@web-7 ~]#systemctl start Nginx
[root@web-7 ~]#ps -ef | grep Nginx
root       8293      1  0 23:43 ?        00:00:00 Nginx: master process /usr/sbin/Nginx -c /etc/Nginx/Nginx.conf
Nginx      8294   8293  0 23:43 ?        00:00:00 Nginx: worker process
root       8301   2589  0 23:44 pts/0    00:00:00 grep --color=auto Nginx
[root@web-7 ~]#

测试Nginx版本
[root@web-7 ~]#curl -I 127.0.0.1

测试访问Nginx
[root@web-7 ~]#echo '阿飞的Nginx' >> /usr/share/Nginx/html/index.html
[root@web-7 ~]#curl 127.0.0.1

两个版本的Nginx管理

Nginx配置文件的读取

源码编译的
[root@web-7 ~]#/opt/Nginx-1.20.2/sbin/Nginx -t
Nginx: the configuration file /opt/Nginx-1.20.2/conf/Nginx.conf Syntax is ok
Nginx: configuration file /opt/Nginx-1.20.2/conf/Nginx.conf test is successful

指定配置文件检测语法
[root@web-7 ~]#/opt/Nginx-1.20.2/sbin/Nginx -t -c /opt/Nginx-1.20.2/conf/Nginx.conf
Nginx: the configuration file /opt/Nginx-1.20.2/conf/Nginx.conf Syntax is ok
Nginx: configuration file /opt/Nginx-1.20.2/conf/Nginx.conf test is successful

yum安装的
[root@web-7 ~]#Nginx -t
Nginx: the configuration file /etc/Nginx/Nginx.conf Syntax is ok
Nginx: configuration file /etc/Nginx/Nginx.conf test is successful

Nginx命令小结

编译安装的
1.查看帮助信息
/opt/Nginx-1.20.2/sbin/Nginx -h

2.查看语法 
/opt/Nginx-1.20.2/sbin/Nginx -t   # 检测认路径配置文件
/opt/Nginx-1.20.2/sbin/Nginx -t -c /opt/Nginx-1.20.2/conf/Nginx.conf  # 指定配置文件检测语法


3.重载配置文件
/opt/Nginx-1.20.2/sbin/Nginx -s reload  # 重载认路径下的配置文件

4.停止服务(注意Nginx命令的路径)
/opt/Nginx-1.20.2/sbin/Nginx -s stop
yum安装的
/usr/sbin/Nginx -t 

systemctl start/restart/reload/stop  Nginx

Nginx配置文件

重要文件

[root@web-7 /opt]#rpm -ql Nginx
/etc/logrotate.d/Nginx        # Nginx日志切割配置文件
/etc/Nginx/conf.d   # 子配置文件目录
/etc/Nginx/conf.d/default.conf    # 配置文件模板
/etc/Nginx/fastcgi_params         # 翻译Nginx的变量为PHP可识别的变量
/etc/Nginx/mime.types        # 支持的媒体文件类型
/etc/Nginx/Nginx.conf        # Nginx配置文件
/etc/Nginx/uwsgi_params    # 翻译Nginx变量为python可识别的变量
/usr/lib/systemd/system/Nginx.service  # systemctl管理Nginx的脚本
/usr/sbin/Nginx        # Nginx命令
/usr/share/Nginx/html        # 网站根目录
/var/log/Nginx            # Nginx认日志目录

查看已安装Nginx的详细信息

[root@web-7 ~]#Nginx -V
Nginx version: Nginx/1.20.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/Nginx --sbin-path=/usr/sbin/Nginx --modules-path=/usr/lib64/Nginx/modules --conf-path=/etc/Nginx/Nginx.conf --error-log-path=/var/log/Nginx/error.log --http-log-path=/var/log/Nginx/access.log --pid-path=/var/run/Nginx.pid --lock-path=/var/run/Nginx.lock --http-client-body-temp-path=/var/cache/Nginx/client_temp --http-proxy-temp-path=/var/cache/Nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/Nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/Nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/Nginx/scgi_temp --user=Nginx --group=Nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,Now -pie'

配置文件理解

Nginx.conf是纯文本类型的文件
[root@web-7 ~]#file /etc/Nginx/Nginx.conf 
/etc/Nginx/Nginx.conf: ASCII text
Nginx以区块的形式组织各区域的配置参数

Nginx配置文件主要区分三大区域

coremodule
核心模块
EventModul
事件驱动模块
HttpCoreModule
http内核模块

核心模块

事件驱动配置

http区域

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐