#私藏项目实操分享# Ngnix --day01
Posted qq5e3a878504161
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了#私藏项目实操分享# Ngnix --day01相关的知识,希望对你有一定的参考价值。
Ngnix --day01
- nginx介绍
2004年由俄罗斯人开发,是一个轻量化的Webserver,特点是性能高、功耗低、占空间小,但是目前功能相比apache等有所欠缺;
常用功能是Http服务、反向代理、负载均衡、邮件代理、SSL、流媒体支持等;
国内著名的分支是淘宝开发的Tengine。
- 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_64 1.16.1-1.el6.ngx nginx
nginx-debug.x86_64 1.8.0-1.el6.ngx nginx
nginx-debuginfo.x86_64 1.16.1-1.el6.ngx nginx
nginx-module-geoip.x86_64 1.16.1-1.el6.ngx nginx
nginx-module-geoip-debuginfo.x86_64 1.16.1-1.el6.ngx nginx
nginx-module-image-filter.x86_64 1.16.1-1.el6.ngx nginx
nginx-module-image-filter-debuginfo.x86_64 1.16.1-1.el6.ngx nginx
nginx-module-njs.x86_64 1.16.1.0.3.7-1.el6.ngx nginx
nginx-module-njs-debuginfo.x86_64 1.16.1.0.3.7-1.el6.ngx nginx
nginx-module-perl.x86_64 1.16.1-1.el6.ngx nginx
nginx-module-perl-debuginfo.x86_64 1.16.1-1.el6.ngx nginx
nginx-module-xslt.x86_64 1.16.1-1.el6.ngx nginx
nginx-module-xslt-debuginfo.x86_64 1.16.1-1.el6.ngx nginx
nginx-nr-agent.noarch 2.0.0-12.el6.ngx nginx
pcp-pmda-nginx.x86_64 3.10.9-9.el6 base
# 如果之前安装有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
root 25732 0.0 0.1 47812 1084 ? Ss 05:40 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 25734 0.0 0.1 48284 1796 ? S 05:40 0:00 nginx: worker process
root 25736 1.0 0.0 103256 816 pts/0 S+ 05:40 0:00 grep nginx
[root@amingLinux yum.repos.d]# netstat -lnp|grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 25732/nginx
[root@amingLinux yum.repos.d]# netstat -lnp|grep ":80"
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 25732/nginx
[root@amingLinux yum.repos.d]# lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 25732 root 6u IPv4 37157 0t0 TCP *:http (LISTEN)
nginx 25734 nginx 6u IPv4 37157 0t0 TCP *: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
- 源码安装
### nginx如果需要制定一些自己定制的模块的话,还是推荐源码安装
### yum安装比较方便,但是仅限于在支持yum安装的操作系统上,如果用ubantu的不支持了
### 操作之前,可以先把原来的进程停掉,包卸载掉:[root@amingLiunx ~]# yum remove nginx或者yum erase nginx
1.解压tar 2.编译 make 3.配置 ./configure 4.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
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
nginx核心模块的存放位置
[root@amingLinux ~]# ls /etc/yum.repos.d/nginx-1.14.0/src/core/
nginx.c ngx_connection.h ngx_file.c ngx_md5.c ngx_palloc.h ngx_radix_tree.h ngx_sha1.h ngx_thread_pool.c
nginx.h ngx_core.h ngx_file.h ngx_md5.h ngx_parse.c ngx_rbtree.c ngx_shmtx.c ngx_thread_pool.h
ngx_array.c ngx_cpuinfo.c ngx_hash.c ngx_module.c ngx_parse.h ngx_rbtree.h ngx_shmtx.h ngx_times.c
ngx_array.h ngx_crc32.c ngx_hash.h ngx_module.h ngx_parse_time.c ngx_regex.c ngx_slab.c ngx_times.h
ngx_buf.c ngx_crc32.h ngx_inet.c ngx_murmurhash.c ngx_parse_time.h ngx_regex.h ngx_slab.h
ngx_buf.h ngx_crc.h ngx_inet.h ngx_murmurhash.h ngx_proxy_protocol.c ngx_resolver.c ngx_spinlock.c
ngx_conf_file.c ngx_crypt.c ngx_list.c ngx_open_file_cache.c ngx_proxy_protocol.h ngx_resolver.h ngx_string.c
ngx_conf_file.h ngx_crypt.h ngx_list.h ngx_open_file_cache.h ngx_queue.c ngx_rwlock.c ngx_string.h
ngx_config.h ngx_cycle.c ngx_log.c ngx_output_chain.c ngx_queue.h ngx_rwlock.h ngx_syslog.c
ngx_connection.c ngx_cycle.h ngx_log.h ngx_palloc.c ngx_radix_tree.c ngx_sha1.c ngx_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/
conf html logs sbin
[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
root 30078 0.0 0.0 112728 940 pts/0 S+ 18:45 0: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
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
root 30132 0.0 0.0 20548 608 ? Ss 18:55 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 30133 0.0 0.1 20992 1052 ? S 18:55 0:00 nginx: worker process
root 30137 0.0 0.0 112728 940 pts/0 R+ 18:55 0: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]。
netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关
network 0:关 1:关 2:开 3:开 4:开 5:开 6:关
nginx 0:关 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]。
netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关
network 0:关 1:关 2:开 3:开 4:开 5:开 6:关
nginx 0:关 1:关 2:开 3:开 4:开 5:开 6:关
以上是关于#私藏项目实操分享# Ngnix --day01的主要内容,如果未能解决你的问题,请参考以下文章