啥是nginx
☆☆☆☆官方网址☆☆☆☆
Nginx的相关概念:
反向代理:
—————————————————————————————————————————————————————
反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个服务器。
负载均衡:
—————————————————————————————————————————————————————
负载均衡,英文名称为Load Balance,是指建立在现有网络结构之上,并提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽、增加吞吐量、加强网络数据处理能力、提高网络的灵活性和可用性。其原理就是数据流量分摊到多个服务器上执行,减轻每台服务器的压力,多台服务器共同完成工作任务,从而提高了数据的吞吐量。
安装nginx
依赖介绍:
gcc gcc-c++
make automake
autoconf
pcre pcre-devel
zlip zlib-devel
openssl openssl-devel
libtool
依赖包安装:
yum -y install gcc gcc-c++ make automake autoconf pcre pcre-devel zlib zlib-devel openssl openssl-devel libtool
nginx下载:
wget https://nginx.org/download/nginx-1.16.1.tar.gz
解压编译安装:
[root@web01 ~]# tar -zxvf nginx-1.16.1.tar.gz [root@web01 ~]# cd nginx-1.16.1/ 编译安装 ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module make && make install
验证是否安装成功:
[root@web02 ~]# /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) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
目录 | 作用 |
conf | 用于存储nginx配置文件 |
html | 用于存放静态网页 |
logs | 存放日志 |
sbin | 用于存放 nginx这种工具 |
1.在系统服务目录里创建nginx.service文件
vi /lib/systemd/system/nginx.service
[Unit] Description=nginx - high performance web server Documentation=http://nginx.org/en/docs/ After=network.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s quit PrivateTmp=true [Install] WantedBy=multi-user.target
2.设置开机启动
systemctl enable nginx.service
service启动nginx服务
1./etc/init.d/目录下编辑nginx默认启动脚本,并添加权限(755):
vim /etc/init.d/nginx
#!/bin/bash #chkconfig: 2345 99 20 #description: nginx-server nginx=/usr/local/nginx/sbin/nginx case $1 in start) netstat -anptu | grep nginx if [ $? -eq 0 ] then echo "nginx-server is already running" else echo "nginx-server begin start" $nginx fi ;; stop) $nginx -s stop if [ $? -eq 0 ] then echo "nginx-server is stoped" else echo "nginx-server stop fail,try again" fi ;; status) netstat -anlpt | grep nginx if [ $? -eq 0 ] then echo "nginx-server is running" else echo "nginx-server is stoped" fi ;; restart) $nginx -s reload if [ $? -eq 0 ] then echo "nginx-server is begin restart" else echo "nginx-server restart fail" fi ;; *) echo "please enter {start restart status stop}" ;; esac exit 0
添加到系统服务:
[root@web01 sbin]# chkconfig --add nginx --将nginx添加到系统服务 [root@web01 sbin]# chkconfig --list nginx --- 查看nginx的运行级别 注意:此输出仅显示SysV服务,不包括本机服务 systemd服务。SysV配置数据可能被本机覆盖 systemd配置。 如果你想列出systemd服务使用'systemctl列表-单元-文件'。 查看在特定目标使用上启用的服务 “systemctl list-dependencies[目标]”。 nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off
service 命令:
[root@web01 /]# service nginx stop
[root@web01 /]# service nginx start