Nginx教程

nginx的搭建

本文主要是介绍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区域

这篇关于nginx的搭建的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!