#私藏项目实操分享# Ngnix--day01

犀渠玉剑良家子,白马金羁侠少年。这篇文章主要讲述#私藏项目实操分享# Ngnix--day01相关的知识,希望能为你提供帮助。
Ngnix   --day01
 

  1. nginx介绍
2004年由俄罗斯人开发,是一个轻量化的Webserver,特点是性能高、功耗低、占空间小,但是目前功能相比apache等有所欠缺;
常用功能是Http服务、反向代理、负载均衡、邮件代理、SSL、流媒体支持等;
国内著名的分支是淘宝开发的Tengine。

  1. yum安装
2.1首先配置一个nginx的扩展源配置文件(文件后缀为repo)
[root@amingLinux ~]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
# 文件内容取自:https://coding.net/u/aminglinux/p/nginx/git/blob/master/2z/nginx.repo
2.2 查看下ngnix的版本
  [root@amingLinux ~]# yum list|grep nginx
nginx.x86_641.16.1-1.el6.ngxnginx
nginx-debug.x86_641.8.0-1.el6.ngxnginx
nginx-debuginfo.x86_641.16.1-1.el6.ngxnginx
nginx-module-geoip.x86_641.16.1-1.el6.ngxnginx
nginx-module-geoip-debuginfo.x86_641.16.1-1.el6.ngxnginx
nginx-module-image-filter.x86_641.16.1-1.el6.ngxnginx
nginx-module-image-filter-debuginfo.x86_64 1.16.1-1.el6.ngxnginx
nginx-module-njs.x86_641.16.1.0.3.7-1.el6.ngxnginx
nginx-module-njs-debuginfo.x86_641.16.1.0.3.7-1.el6.ngxnginx
nginx-module-perl.x86_641.16.1-1.el6.ngxnginx
nginx-module-perl-debuginfo.x86_641.16.1-1.el6.ngxnginx
nginx-module-xslt.x86_641.16.1-1.el6.ngxnginx
nginx-module-xslt-debuginfo.x86_641.16.1-1.el6.ngxnginx
nginx-nr-agent.noarch2.0.0-12.el6.ngxnginx
pcp-pmda-nginx.x86_643.10.9-9.el6base
# 如果之前安装有epel版本的,先禁掉或者卸载掉以免产生冲突;
2.3 安装并启动nginx
[root@amingLiunx ~]# yum install nginx -y
[root@amingLinux yum.repos.d]# service nginx start
正在启动 nginx:[确定]
[root@amingLinux yum.repos.d]# ps aux | grep nginx
root257320.00.1478121084 ?Ss05:400:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx257340.00.1482841796 ?S05:400:00 nginx: worker process
root257361.00.0 103256816 pts/0S+05:400:00 grep nginx
[root@amingLinux yum.repos.d]# netstat -lnp|grep nginx
tcp00 0.0.0.0:800.0.0.0:*LISTEN25732/nginx
[root@amingLinux yum.repos.d]# netstat -lnp|grep ":80"
tcp00 0.0.0.0:800.0.0.0:*LISTEN25732/nginx
[root@amingLinux yum.repos.d]# lsof -i :80
COMMANDPIDUSERFDTYPE DEVICE SIZE/OFF NODE NAME
nginx25732root6uIPv4371570t0TCP *:http (LISTEN)
nginx25734 nginx6uIPv4371570t0TCP *:http (LISTEN)
#contos7以后的版本,启动用systemctl,7以前的版本,还是用service
用IP地址直接访问页面,如果不能访问,可能是防火墙没关
[root@amingLinux yum.repos.d]# service iptables stop
iptables:将链设置为政策 ACCEPT:filter[确定]
iptables:清除防火墙规则:[确定]
iptables:正在卸载模块:[确定]
[root@amingLinux yum.repos.d]# iptables -A INPUT -ptcp --dport 80 -j ACCEPT
firewall-cmd --zone=public --query-port=80/tcp
[root@izftn316gg7zdqz ~]# systemctl stop firewalld
2.4查看nginx版本的参数
[root@amingLinux yum.repos.d]# nginx -v
nginx version: nginx/1.16.1
[root@amingLinux yum.repos.d]# nginx -V
nginx version: nginx/1.16.1
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-23) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
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 --param=ssp-buffer-size=4 -m64 -mtune=generic -fPIC --with-ld-opt=-Wl,-z,relro
-Wl,-z,now -pie

  1. 源码安装
### nginx如果需要制定一些自己定制的模块的话,还是推荐源码安装
### yum安装比较方便,但是仅限于在支持yum安装的操作系统上,如果用ubantu的不支持了
### 操作之前,可以先把原来的进程停掉,包卸载掉:[root@amingLiunx ~]# yum remove nginx或者yum erase nginx
1.解压tar 2.编译 make3.配置 ./configure4.make install 安装
3.1 下载压缩包
[root@amingLinux yum.repos.d]# wget http://nginx.org/download/nginx-1.14.0.tar.gz
[root@amingLinux yum.repos.d]# du -sh nginx-1.14.0.tar.gz
996K nginx-1.14.0.tar.gz
[root@amingLinux yum.repos.d]# tar zxf nginx-1.14.0.tar.gz
[root@amingLinux yum.repos.d]# ls nginx-1.14.0
autoCHANGESCHANGES.ruconfconfigurecontribhtmlLICENSEmanREADMEsrc
nginx核心模块的存放位置
[root@amingLinux ~]# ls /etc/yum.repos.d/nginx-1.14.0/src/core/
nginx.cngx_connection.hngx_file.cngx_md5.cngx_palloc.hngx_radix_tree.hngx_sha1.hngx_thread_pool.c
nginx.hngx_core.hngx_file.hngx_md5.hngx_parse.cngx_rbtree.cngx_shmtx.cngx_thread_pool.h
ngx_array.cngx_cpuinfo.cngx_hash.cngx_module.cngx_parse.hngx_rbtree.hngx_shmtx.hngx_times.c
ngx_array.hngx_crc32.cngx_hash.hngx_module.hngx_parse_time.cngx_regex.cngx_slab.cngx_times.h
ngx_buf.cngx_crc32.hngx_inet.cngx_murmurhash.cngx_parse_time.hngx_regex.hngx_slab.h
ngx_buf.hngx_crc.hngx_inet.hngx_murmurhash.hngx_proxy_protocol.cngx_resolver.cngx_spinlock.c
ngx_conf_file.cngx_crypt.cngx_list.cngx_open_file_cache.cngx_proxy_protocol.hngx_resolver.hngx_string.c
ngx_conf_file.hngx_crypt.hngx_list.hngx_open_file_cache.hngx_queue.cngx_rwlock.cngx_string.h
ngx_config.hngx_cycle.cngx_log.cngx_output_chain.cngx_queue.hngx_rwlock.hngx_syslog.c
ngx_connection.cngx_cycle.hngx_log.hngx_palloc.cngx_radix_tree.cngx_sha1.cngx_syslog.h
3.2编译安装
[root@amingLinux nginx-1.14.0]# ./configure --prefix=/usr/local/nginx
[root@amingLinux nginx-1.14.0]# make
[root@amingLinux nginx-1.14.0]# make install
[root@amingLinux nginx-1.14.0]# ls /usr/local/nginx/
confhtmllogssbin
[root@amingLinux nginx-1.14.0]# ls /usr/local/nginx/sbin/
nginx
[root@amingLinux nginx-1.14.0]# du -sh !$
du -sh /usr/local/nginx/sbin/
3.6M /usr/local/nginx/sbin/
[root@amingLinux nginx-1.14.0]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.14.0
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-23) (GCC)
configure arguments: --prefix=/usr/local/nginx
## 如果安装出现报错,可能是因为缺少安装需要的依赖包:
yum -y install gcc
yum -y install gcc-c++
yum -y install openssl openssl-devel
yum install pcre-devel zlib zlib-devel openssl openssl-devel
这是页面的配置文件
[root@amingLinux nginx-1.14.0]# curl localhost
< !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>
检查配置文件的语法
[root@amingLinux nginx-1.14.0]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@amingLinux nginx-1.14.0]# /usr/local/nginx/sbin/nginx
如果修改了配置文件,想要重新加载,先杀死原来的进程:kilall,如果没有这个命令,
[root@amingLinux nginx-1.14.0]# /usr/local/nginx/sbin/nginx -s reload
[root@amingLiunx src]# yum install -y psmisc
[root@amingLiunx src]# killall nginx
[root@amingLiunx src]# ps aux|grep nginx
root300780.00.0 112728940 pts/0S+18:450:00 grep --color=auto nginx
3.3 启动服务管理脚本
更便捷的方法,启动服务写入脚本
[root@amingLinux nginx-1.14.0]# vi /etc/init.d/nginx
kconfig: - 30 21

description: http service.Source Function Library. /etc/init.d/functions
Nginx Settings【#私藏项目实操分享# Ngnix--day01】NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"
start()

echo -n $"Starting $prog: "
mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN -c $NGINX_CONF
RETVAL=$?
echo
return $RETVAL

stop()

echo -n $"Stopping $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -TERM
rm -rf /dev/shm/nginx_temp
RETVAL=$?
echo
return $RETVAL

reload()

echo -n $"Reloading $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -HUP
RETVAL=$?
RETVAL=$?
echo
return $RETVAL

restart()

stop
start

configtest()

$NGINX_SBIN -c $NGINX_CONF -t
return 0

case "$1" in
start)
start
; ;
stop)
stop
; ;
reload)
reload
; ;
restart)
restart
; ;
configtest)
configtest
; ;
*)
echo $"Usage: $0 start|stop|reload|restart|configtest"
RETVAL=1
esac
exit $RETVAL
##上文件内容:https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/etc_init.d_nginx
使用上面启动脚本文件启动
[root@amingLiunx src]# chmod 755 /etc/init.d/nginx
[root@amingLiunx src]# ll -d /etc/init.d/nginx
-rwxr-xr-x. 1 root root 1141 12月 16 18:53 /etc/init.d/nginx
[root@amingLiunx src]# killall nginx
[root@amingLiunx src]# /etc/init.d/nginx start
Reloading systemd:[确定]
Starting nginx (via systemctl):[确定]
[root@amingLiunx src]# ps aux|grep nginx
root301320.00.020548608 ?Ss18:550:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody301330.00.1209921052 ?S18:550:00 nginx: worker process
root301370.00.0 112728940 pts/0R+18:550:00 grep --color=auto nginx
3.4 加入开机启动
[root@amingLiunx src]# chkconfig --add nginx
[root@amingLiunx src]# chkconfig --list
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 systemctl list-unit-files。
查看在具体 target 启用的服务请执行
systemctl list-dependencies [target]。
netconsole0:关 1:关 2:关 3:关 4:关 5:关 6:关
network0:关 1:关 2:开 3:开 4:开 5:开 6:关
nginx0:关 1:关 2:关 3:关 4:关 5:关 6:关
[root@amingLiunx src]# chkconfig nginx on
[root@amingLiunx src]# chkconfig --list
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 systemctl list-unit-files。
查看在具体 target 启用的服务请执行
systemctl list-dependencies [target]。
netconsole0:关 1:关 2:关 3:关 4:关 5:关 6:关
network0:关 1:关 2:开 3:开 4:开 5:开 6:关
nginx0:关 1:关 2:开 3:开 4:开 5:开 6:关

    推荐阅读