【Day1】Nginx实战训练营
1、Nginx介绍
-
常见WebServer (排行 https://news.netcraft.com/archives/2019)
老牌:httpd(apache)开源、市场份额高
微软:IIS
轻量:Lighttpd,性能高,低能耗,功能欠缺 -
Nginx 诞生
2004年10月发布,俄罗斯人Igor Sysoev开发,rambler.ru -
Nginx 官网、版本
nginx.org
国内废纸Tengine(http://tengine.taobao.org/) -
Nginx 功能介绍
Http服务、反向代理、负载均衡、邮件代理、缓存加速、SSL、flv/mp4流媒体
2、Nginx 安装(yum)
1、新建 nginx 安装源
[root@localhost ~]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
2、可安装的 nginx 版本
[root@localhost ~]# yum list|grep nginx
nginx.x86_64 1:1.16.1-1.el7.ngx nginx
nginx-debug.x86_64 1:1.8.0-1.el7.ngx nginx
nginx-debuginfo.x86_64 1:1.16.1-1.el7.ngx nginx
nginx-module-geoip.x86_64 1:1.16.1-1.el7.ngx nginx
nginx-module-geoip-debuginfo.x86_64 1:1.16.1-1.el7.ngx nginx
nginx-module-image-filter.x86_64 1:1.16.1-1.el7.ngx nginx
nginx-module-image-filter-debuginfo.x86_64 1:1.16.1-1.el7.ngx nginx
nginx-module-njs.x86_64 1:1.16.1.0.3.8-1.el7.ngx nginx
nginx-module-njs-debuginfo.x86_64 1:1.16.1.0.3.8-1.el7.ngx nginx
nginx-module-perl.x86_64 1:1.16.1-1.el7.ngx nginx
nginx-module-perl-debuginfo.x86_64 1:1.16.1-1.el7.ngx nginx
nginx-module-xslt.x86_64 1:1.16.1-1.el7.ngx nginx
nginx-module-xslt-debuginfo.x86_64 1:1.16.1-1.el7.ngx nginx
nginx-nr-agent.noarch 2.0.0-12.el7.ngx nginx
pcp-pmda-nginx.x86_64 4.3.2-2.el7 base
3、yum 安装
[root@localhost ~]# yum install -y nginx
4、启动 nginx
[root@localhost ~]# systemctl start nginx
[root@localhost ~]# ps aux|grep nginx
root 46437 0.0 0.0 46448 968 ? Ss 07:28 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 46438 0.0 0.1 46856 1920 ? S 07:28 0:00 nginx: worker process
root 46440 0.0 0.0 112728 972 pts/1 S+ 07:28 0:00 grep --color=auto nginx
[root@localhost ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 42723/php-fpm: mast
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 46437/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 6916/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 7181/master
tcp6 0 0 :::3306 :::* LISTEN 7067/mysqld
tcp6 0 0 :::22 :::* LISTEN 6916/sshd
tcp6 0 0 ::1:25 :::* LISTEN 7181/master
如果访问不了,检查是否有防火墙,可能需要添加规则 iptables -I INPUT -p tcp --dport 80 -j ACCEPT
或者直接关闭防火墙
5、nginx -V查看版本以及各目录、参数
[root@localhost ~]# nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (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'
3、Nginx 安装(源码)
1、官网下载最新版本源码包
[root@localhost ~]# cd /usr/local/src/
[root@localhost ~]# wget http://nginx.org/download/nginx-1.16.1.tar.gz
2、解压缩,进解压目录
[root@localhost src]# tar xvf nginx-1.16.1.tar.gz
3、编译安装 nginx
[root@localhost src]# cd nginx-1.16.1/
[root@localhost nginx-1.16.1]# ./configure --prefix=/usr/local/nginx
[root@localhost nginx-1.16.1]# make && make install
[root@localhost nginx-1.16.1]# cd /usr/local/nginx/
[root@localhost nginx]# ls
conf html logs sbin
conf 下是配置文件
html 下是默认页
logs 下是日志存放
sbin 下是可执行文件
4、nginx 查看版本
[root@localhost nginx]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
configure arguments: --prefix=/usr/local/nginx
/usr/local/nginx/sbin/nginx //启动
pkill nginx // 杀死进程,停止服务
/usr/local/nginx/sbin/nginx -t //检查配置文件语法错误
/usr/local/nginx/sbin/nginx -s reload //重载配置
5、启动
[root@localhost nginx-1.16.1]# /usr/local/nginx/sbin/nginx
[root@localhost nginx-1.16.1]# ps aux|grep nginx
root 48924 0.0 0.0 20556 608 ? Ss 07:06 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 48925 0.0 0.1 21008 1328 ? S 07:06 0:00 nginx: worker process
root 48927 0.0 0.0 112728 972 pts/1 R+ 07:06 0:00 grep --color=auto nginx
[root@localhost nginx-1.16.1]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 42723/php-fpm: mast
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 48924/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 6916/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 7181/master
tcp6 0 0 :::3306 :::* LISTEN 7067/mysqld
tcp6 0 0 :::22 :::* LISTEN 6916/sshd
tcp6 0 0 ::1:25 :::* LISTEN 7181/master
6、默认页在 html/index.html
7、添加启动脚本
[root@localhost nginx-1.16.1]# vim /etc/init.d/nginx
[root@localhost nginx-1.16.1]# ps aux|grep nginx
root 48924 0.0 0.0 20556 608 ? Ss 07:06 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 48925 0.0 0.1 21008 1328 ? S 07:06 0:00 nginx: worker process
root 48937 0.0 0.0 112728 968 pts/1 R+ 07:13 0:00 grep --color=auto nginx
[root@localhost nginx-1.16.1]# kill 48924
[root@localhost nginx-1.16.1]# ps aux|grep nginx
root 48939 0.0 0.0 112728 972 pts/1 R+ 07:14 0:00 grep --color=auto nginx
[root@localhost nginx-1.16.1]# chmod 755 /etc/init.d/nginx
内容:http://note.youdao.com/noteshare?id=eaa7747686f5cbb4e09c30d4f79ed8c3&sub=0D9C0259066B4FCCA35A0B937E210C82
8、启动和停止(脚本)
[root@localhost nginx-1.16.1]# /etc/init.d/nginx start
Reloading systemd: [ 确定 ]
Starting nginx (via systemctl): [ 确定 ]
[root@localhost nginx-1.16.1]# ps aux|grep nginx
root 48978 0.0 0.0 20556 620 ? Ss 07:15 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 48979 0.0 0.1 21008 1336 ? S 07:15 0:00 nginx: worker process
root 48981 0.0 0.0 112728 972 pts/1 S+ 07:15 0:00 grep --color=auto nginx
[root@localhost nginx-1.16.1]# /etc/init.d/nginx stop
Stopping nginx (via systemctl): [ 确定 ]
[root@localhost nginx-1.16.1]# ps aux|grep nginx
root 49001 0.0 0.0 112728 968 pts/1 R+ 07:15 0:00 grep --color=auto nginx
9、加入启动项
[root@localhost nginx-1.16.1]# chkconfig --add nginx
[root@localhost nginx-1.16.1]# chkconfig nginx on
[root@localhost nginx-1.16.1]# chkconfig --list
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 'systemctl list-unit-files'。
查看在具体 target 启用的服务请执行
'systemctl list-dependencies [target]'。
mysqld 0:关 1:关 2:开 3:开 4:开 5:开 6:关
netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关
network 0:关 1:关 2:开 3:开 4:开 5:开 6:关
nginx 0:关 1:关 2:开 3:开 4:开 5:开 6:关
php-fpm 0:关 1:关 2:开 3:开 4:开 5:开 6:关