赞
踩
Nginx 是一个高性能的 Web 和反向代理服务器, 它具有有很多非常优越的特性:
Web服务器:相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,体现更高的效率,这点使 Nginx 尤其受到虚拟主机提供商的欢迎。能够支持高达 50,000 个并发连接数的响应,感谢 Nginx 为我们选择了 epoll and kqueue 作为开发模型.
负载均衡服务器:Nginx 既可以在内部直接支持 Rails 和 PHP,也可以支持作为 HTTP代理服务器 对外进行服务。Nginx 用 C 编写, 不论是系统资源开销还是 CPU 使用效率都比 Perlbal 要好的多。
邮件代理服务器: Nginx 同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器),Last.fm 描述了成功并且美妙的使用经验。
Nginx 安装非常的简单,且Nginx 启动特别容易,并且几乎可以做到24小时不间断运行,即使运行数个月也不需要重新启动。你还能够在不间断服务的情况下进行软件版本的升级。接下来我们要针对于安装和运维操作进行实战和分析。
使用CMD命令start命令启动nginx,再运行nginx,到nginx所在目录,如果启动前已经启动nginx并记录下pid文件,会kill指定进程,按照指定配置去启动nginx
Nginx的默认配置文件为 nginx.conf。nginx -c xxx.conf
以指定的文件作为配置文件,启动 Nginx。不像许多其他软件系统,Nginx仅有几个命令行参数,完全通过配置文件来配置。
Nginx可以使用信号系统来控制主进程。
默认Nginx将其主进程的pid写入到/usr/local/nginx/nginx.pid 文件中。通过传递参数给 ./configure 或使用 pid 指令,来改变该文件的位置。
主进程可以处理以下的信号:
nginx.exe -c conf/nginx.conf
-t:代表测试配置文件语法正确性
nginx.exe -c -t conf\nginx.conf
nginx.exe -s stop
nginx.exe -v
启动成功后,浏览器访问 localhost,即可看到Nginx 欢迎页
Nginx 的使用比较简单,就是几条命令,常用到的命令如下(nginx.exe 可以等价于nginx):
Mac OSX 安装特别简单,首先你需要安装 Brew, 通过 brew 快速安装 nginx。
brew install nginx
# Updating Homebrew...
# ==> Auto-updated Homebrew!
# Updated 2 taps (homebrew/core, homebrew/cask).
# ==> Updated Formulae
# ==> Installing dependencies for nginx: openssl, pcre
# ==> Installing nginx dependency: openssl
# ==> Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2o_1.high_sierra.bottle.tar.gz
# ######################################################################## 100.0%
# ==> Pouring openssl-1.0.2o_1.high_sierra.bottle.tar.gz
# ==> Caveats
# A CA file has been bootstrapped using certificates from the SystemRoots
# keychain. To add additional certificates (e.g. the certificates added in
# the System keychain), place .pem files in
# /usr/local/etc/openssl/certs
#
# and run
# /usr/local/opt/openssl/bin/c_rehash
#
# This formula is keg-only, which means it was not symlinked into /usr/local,
# because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries.
#
# If you need to have this software first in your PATH run:
# echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.zshrc
#
# For compilers to find this software you may need to set:
# LDFLAGS: -L/usr/local/opt/openssl/lib
# CPPFLAGS: -I/usr/local/opt/openssl/include
# For pkg-config to find this software you may need to set:
# PKG_CONFIG_PATH: /usr/local/opt/openssl/lib/pkgconfig
#
# ==> Summary
# ? /usr/local/Cellar/openssl/1.0.2o_1: 1,791 files, 12.3MB
# ==> Installing nginx dependency: pcre
# ==> Downloading https://homebrew.bintray.com/bottles/pcre-8.42.high_sierra.bottle.tar.gz
# ######################################################################## 100.0%
# ==> Pouring pcre-8.42.high_sierra.bottle.tar.gz
# ? /usr/local/Cellar/pcre/8.42: 204 files, 5.3MB
# ==> Installing nginx
# ==> Downloading https://homebrew.bintray.com/bottles/nginx-1.13.12.high_sierra.bottle.tar.gz
# ######################################################################## 100.0%
# ==> Pouring nginx-1.13.12.high_sierra.bottle.tar.gz
# ==> Caveats
# Docroot is: /usr/local/var/www
#
# The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
# nginx can run without sudo.
#
# nginx will load all files in /usr/local/etc/nginx/servers/.
#
# To have launchd start nginx now and restart at login:
# brew services start nginx
# Or, if you don't wacd /usr/local/Cellar/nginx/1.13.12/n just run:
# cd /usr/local/Cellar/nginx/1.13.12/
注意默认端口不是80查看确认端口是否被占用。
brew services start nginx
http://localhost:8080/
Nginx可以使用各平台的默认包来安装,包括具体的编译参数信息。正式开始前,编译环境gcc g++ 开发库之类的需要提前装好,这里默认你已经装好。CentOS6.5左右默认这两个包都没安装全,所以这两个都运行安装即可。
yum -y install gcc make gcc-c++ wget
yum -y install openssl openssl-devel
prce(重定向支持)和openssl(https支持,如果不需要https可以不安装。)
yum install -y pcre-devel
yum -y install gcc automake autoconf libtool make
yum -y install gcc make gcc-c++ openssl-devel
如果不安装会出现安装报错误的话比如:“C compiler cc is not found”,这个就是缺少编译环境,安装一下就可以了。
下载Linux版本的地址根页面:http://nginx.org/download/,如下图所示。
yum install wget
wget http://nginx.org/download/nginx-1.X.X.tar.gz
tar -zxvf nginx-X.X.X.tar.gz
然后进入目录编译安装,configure参数说明
cd nginx-1.xx
./configure --sbin-path=/usr/local/nginx/nginx \
--conf-path=/usr/local/nginx/nginx.conf \
--pid-path=/usr/local/nginx/nginx.pid \
--with-http_ssl_module \
--with-pcre=/usr/local/src/pcre-8.34 \
--with-zlib=/usr/local/src/zlib-1.2.8 \
--with-openssl=/usr/local/src/openssl-1.0.1c
./configure --prefix=/你的安装目录 --add-module=/第三方模块目录
kqueue(sun os),epoll (linux kenel 2.6+), rtsig(- 实时信号)或者/dev/poll(一种类似select的模式,底层实现与SELECT基本相 同,都是采用轮训方法) SELECT模式将是默认安装模式
启动后会进行打印对应的配置信息
....
Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
Requires OpenSSL. On Debian, this is libssl-dev. 开启HTTP SSL模块,使NGINX可以支持HTTPS请求。这个模块需要已经安装了OPENSSL,在DEBIAN上是libssl
配置完成之后,使用 make 和 make install 编译和安装 nginx。
$ wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
nginx rpm 包实际上安装的是 nginx 的 yum 源。
$ rpm -ivh nginx-*.rpm
$ yum install nginx
$ firewall-cmd --zone=public --add-port=80/tcp --permanent
$ firewall-cmd --reload
make
make install
采用编译方式安装 Nginx, 并将其注册为 systemd 服务
安装路径为:/usr/local/nginx
默认下载安装 1.16.0 版本
默认安装 - 执行以下任意命令即可:
curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/soft/nginx-install.sh | bash
wget -qO- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/soft/nginx-install.sh | bash
自定义安装 - 下载脚本到本地,并按照以下格式执行:
sh nginx-install.sh [version]
/usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx -s reload
/usr/local/nginx/sbin/nginx -s stop
/usr/local/nginx/sbin/nginx -s quit
/usr/local/nginx/sbin/nginx -V
systemctl start nginx.service
systemctl disable nginx.service
systemctl status nginx.service
systemctl list-units --type=service
systemctl restart nginx.service
systemctl enable nginx.service
systemctl is-enabled servicename.service # 查询服务是否开机启动
systemctl enable *.service # 开机运行服务
systemctl disable *.service # 取消开机运行
systemctl start *.service # 启动服务
systemctl stop *.service # 停止服务
systemctl restart *.service # 重启服务
systemctl reload *.service # 重新加载服务配置文件
systemctl status *.service # 查询服务运行状态
systemctl --failed # 显示启动失败的服务
如果通过yum安装,使用下面命令安装。
yum remove nginx
编译安装,删除/usr/local/nginx目录即可,如果配置了自启动脚本,也需要删除。
假如采用传统请执行一下命令:
systemctl stop firewalld
systemctl mask firewalld
Iptables 是用来设置、维护和检查 Linux内核 的IP包过滤规则的。
yum install iptables-services
systemctl enable iptables
service iptables restart
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。