Nginx系列—服务器安装与配置
来源:互联网 发布:苹果换铃声软件 编辑:程序博客网 时间:2024/06/14 09:44
一、安装
1、首先安装缺少的依赖包
[root@bogon /]# yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel
- zlib zlib-devel #为nginx提供gzip模块,需要zlib库支持
- openssl openssl-devel #为nginx提供ssl功能
- pcre pcre-devel #为支持地址重写rewrite功能
2、下载并解压Nginx
[root@bogon src]# wget http://nginx.org/download/nginx-1.13.3.tar.gz[root@bogon src]# tar xvf nginx-1.13.3.tar.gz[root@bogon src]# cd nginx-1.13.3
3、编译安装
为了后续准备我们另外下载2个插件模块:nginx_upstream_check_module-0.3.0.tar.gz —— 检查后端服务器的状态,nginx-goodies-nginx-sticky-module-ng-bd312d586752.tar.gz(建议在/usr/local/src下解压后将目录重命名为nginx-sticky-module-ng-1.2.5)* —— 后端做负载均衡解决session sticky问题*。
# 下载文件并重新命名[root@localhost src]# wget -O "nginx_upstream_check_module-0.3.0.tar.gz" https://codeload.github.com/yaoweibin/nginx_upstream_check_module/tar.gz/v0.3.0[root@localhost nginx-1.13.3]# wget -O "nginx-sticky-module-ng-1.2.5.zip" https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/08a395c66e42.zip# 解压文件并重命名目录[root@localhost src]# tar -xvf nginx_upstream_check_module-0.3.0.tar.gz[root@localhost src]# unzip nginx-sticky-module-ng-1.2.5.tar.gz[root@localhost src]# mv nginx-goodies-nginx-sticky-module-ng-08a395c66e42/ nginx-sticky-module-ng-1.2.5
[root@bogon nginx-1.13.3]# ./configure \> --prefix=/usr/local/nginx \> --with-pcre \> --user=nginx \> --group=nginx \> --with-http_stub_status_module \> --with-http_ssl_module \> --with-http_gzip_static_module \> --with-http_realip_module \> --pid-path=/usr/local/nginx/run/nginx.pid \> --without-mail_pop3_module \> --without-mail_imap_module \> --without-mail_smtp_module \> --add-module=../nginx_upstream_check_module-0.3.0 \> --add-module=../nginx-sticky-module-ng-1.2.5[root@bogon nginx-1.13.3]# make[root@bogon nginx-1.13.3]# make test[root@bogon nginx-1.13.3]# make install
make的地方有一个小技巧,如果服务器是双核,可以通过-j2来指定用双核进行编译,-j4代表4核编译。
常用编译选项说明
- –prefix=PATH : 指定nginx的安装目录。默认 /usr/local/nginx
- –conf-path=PATH : 设置nginx.conf配置文件的路径。nginx允许使用不同的配置文件启动,通过命令行中的-c选项。默认为prefix/conf/nginx.conf
- –user=name: 设置nginx工作进程的用户。安装完成后,可以随时在nginx.conf配置文件更改user指令。默认的用户名是nobody。–group=name类似
- –with-pcre : 设置PCRE库的源码路径,如果已通过yum方式安装,使用–with-pcre自动找到库文件。使用–with-pcre=PATH时,需要从PCRE网站下载pcre库的源码(版本4.4 - 8.30)并解压,剩下的就交给Nginx的./configure和make来完成。perl正则表达式使用在location指令和 ngx_http_rewrite_module模块中。
- –with-zlib=PATH : 指定 zlib(版本1.1.3 - 1.2.5)的源码解压目录。在默认就启用的网络传输压缩模块ngx_http_gzip_module时需要使用zlib 。
- –with-http_ssl_module : 使用https协议模块。默认情况下,该模块没有被构建。前提是openssl与openssl-devel已安装
- –with-http_stub_status_module : 用来监控 Nginx 的当前状态
- –with-http_realip_module : 通过这个模块允许我们改变客户端请求头中客户端IP地址值(例如X-Real-IP 或 X-Forwarded-For),意义在于能够使得后台服务器记录原始客户端的IP地址
- –add-module=PATH : 添加第三方外部模块,如nginx-sticky-module-ng或缓存模块。每次添加新的模块都要重新编译(Tengine可以在新加入module时无需重新编译)
二、配置
1、创建 Nginx 运行使用的用户 www
[root@bogon conf]# groupadd nginx [root@bogon conf]# useradd -g nginx nginx
2、配置nginx.conf
下面的nginx.conf简单的实现nginx在前端做反向代理服务器的例子,处理js、png等静态文件,jsp等动态请求转发到其它服务器
[root@bogon conf]# cat /usr/local/nginx/conf/nginx.confuser nginx nginx;worker_processes 2;error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;pid /usr/local/nginx/run/nginx.pid;events { use epoll; worker_connections 2048;}http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; # tcp_nopush on; keepalive_timeout 65; # gzip压缩功能设置 gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.0; gzip_comp_level 6; gzip_types text/html text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml; gzip_vary on; # http_proxy 设置 client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 75; proxy_send_timeout 75; proxy_read_timeout 75; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; proxy_temp_path /usr/local/nginx/proxy_temp 1 2; # 设定负载均衡后台服务器列表 upstream backend { #ip_hash; server 192.168.10.100:8080 max_fails=2 fail_timeout=30s ; server 192.168.10.101:8080 max_fails=2 fail_timeout=30s ; } # 很重要的虚拟主机配置 server { listen 80; server_name itoatest.example.com; root /apps/oaapp; charset utf-8; access_log logs/host.access.log main; #对 / 所有做负载均衡+反向代理 location / { root /apps/oaapp; index index.jsp index.html index.htm; proxy_pass http://backend; proxy_redirect off; # 后端的Web服务器可以通过X-Forwarded-For获取用户真实IP proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; } #静态文件,nginx自己处理,不去backend请求tomcat location ~* /download/ { root /apps/oa/fs; } location ~ .*\.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ { root /apps/oaapp; expires 7d; } location /nginx_status { stub_status on; access_log off; allow 192.168.10.0/24; deny all; } location ~ ^/(WEB-INF)/ { deny all; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } ## 其它虚拟主机,server 指令开始}
3、检查配置文件ngnix.conf的正确性命令
[root@bogon conf]# /usr/local/nginx/sbin/nginx -t
三、添加为系统服务
Nginx安装完成后默认不会注册为系统服务,所以需要手工添加系统服务脚本。在/etc/init.d目录下新建nginx文件,并更改权限其即可。
1、新建nginx启动脚本
[root@bogon src]# vim /etc/init.d/nginx#!/bin/bash## nginx - this script starts and stops the nginx daemin## chkconfig: - 85 15# description: Nginx is an HTTP(S) server, HTTP(S) reverse \# proxy and IMAP/POP3 proxy server# processname: nginx# config: /usr/local/nginx/conf/nginx.conf# pidfile: /usr/local/nginx/logs/nginx.pidnginxd=/usr/local/nginx/sbin/nginxnginx_config=/usr/local/nginx/conf/nginx.confnginx_pid=/usr/local/nginx/logs/nginx.pidRETVAL=0prog="nginx"# Source function library.. /etc/rc.d/init.d/functions# Source networking configuration.. /etc/sysconfig/network# Check that networking is up.[ ${NETWORKING} = "no" ] && exit 0[ -x $nginxd ] || exit 0# Start nginx daemons functions.start(){ if [ -e $nginx_pid ]; then echo "nginx already running..." exit 1 fi echo -n $"Starting $prog:" daemon $nginxd -c ${nginx_config} RETVAL=$? echo [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx return $RETVAL}# Stop nginx daemons functions.stop(){ echo -n $"Stopping $prog:" killproc $nginxd RETVAL=$? echo [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx $nginx_pid}#reload nginx service functions.reload(){ echo -n $"Reloading $proc:" killproc $nginxd -HUP RETVAL=$? echo}# See how we were called.case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) stop start ;; status) status $prog RETVAL=$? ;; *) echo $"Usage: $prog {start|stop|restart|reload|status|help}" exit 1esacexit $RETVAL
2、修改其权限并开机启动
- 修改权限:chmod 755 /etc/init.d/nginx
- 加为系统服务:chkconfig –add nginx
- 开机启动:chkconfig nginx on
- 查看开机启动的服务:chkconfig –list
3、备注
- 启动服务:service nginx start
- 停止服务:service nginx stop
- 重启服务:service nginx reload
附:另一种添加nginx命令的方法
1、配置环境变量
[root@bogon nginx-1.13.3]# vim /etc/profile.d/nginx.shexport PATH=/usr/local/nginx/sbin:$PATH
2、加载刚设置的变量
[root@bogon nginx-1.13.3]# source /etc/profile
3、下面是几个nginx常用的命令,如果您可以正常使用这些命令,那么说明nginx已经安装成功了。
- nginx:直接在命令行键入nginx,就可以启动nginx。
- nginx -t:检查配置文件是否正确。这个命令可以检查nginx.conf配置文件其格式、语法是否正确。如果配置文件存在错误,则会出现相应提示;如果nginx.conf文件正确,也会出现相应的成功提示。
- nginx -s reload:重加载/重启nginx——以新的nginx.conf配置文件中的定义。
- nginx -s stop:停止nginx。
附、安装过程中的问题
1、nginx启动后,使用“systemctl status nginx”查看启动状态
[root@bogon logs]# systemctl status nginx● nginx.service - SYSV: Nginx is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server Loaded: loaded (/etc/rc.d/init.d/nginx; bad; vendor preset: disabled) Active: active (running) since 四 2017-07-20 17:00:44 CST; 39min ago Docs: man:systemd-sysv-generator(8) Process: 2968 ExecStart=/etc/rc.d/init.d/nginx start (code=exited, status=0/SUCCESS) Main PID: 2971 (nginx) CGroup: /system.slice/nginx.service ├─2971 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf └─2973 nginx: worker process7月 20 17:00:44 bogon systemd[1]: Starting SYSV: Nginx is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server...7月 20 17:00:44 bogon nginx[2968]: /etc/rc.d/init.d/nginx: 第 26 行:[: =: 期待一元表达式7月 20 17:00:44 bogon nginx[2968]: Starting nginx:[ 确定 ]7月 20 17:00:44 bogon systemd[1]: PID file /usr/local/nginx/logs/nginx.pid not readable (yet?) after start.
问题1、/etc/rc.d/init.d/nginx: 第 26 行:[: =: 期待一元表达式
决解方法:[ “$yn” != “” ]变量那加引号。
问题2、PID file /usr/local/nginx/logs/nginx.pid not readable (yet?) after start.
决解方法:
1.建立nginx运行的组和用户,并将相应的目录分配给新建的组和用户。
2.修改目录权限为可读可写:chmod -R 766 logs/
2、启动不了
7月 20 16:17:51 bogon nginx[2671]: Starting nginx:nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)7月 20 16:17:51 bogon nginx[2671]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)7月 20 16:17:52 bogon nginx[2671]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)7月 20 16:17:52 bogon nginx[2671]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)7月 20 16:17:53 bogon nginx[2671]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)7月 20 16:17:53 bogon nginx[2671]: nginx: [emerg] still could not bind()7月 20 16:17:53 bogon nginx[2671]: [失败]7月 20 16:17:53 bogon systemd[1]: nginx.service: control process exited, code=exited status=17月 20 16:17:53 bogon systemd[1]: Failed to start SYSV: Nginx is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server.
查看端口是否被占用
阅读全文
0 0
- Nginx系列—服务器安装与配置
- Apache系列—服务器安装与配置
- MySQL系列—服务器安装与配置
- Tomcat系列—服务器安装与配置
- Keepalived系列—服务器安装与配置
- nginx服务器介绍、安装与配置
- Nginx服务器的安装与配置
- nginx服务器的安装与配置
- Nginx服务器安装配置
- MySQL系列—服务器安装与配置(rpm)
- 配置服务器 —— 安装Nginx
- HTTP服务器 nginx for windows下载 详细安装与配置
- 2 Nginx服务器的安装与配置
- Linux系统Nginx服务器的安装与配置方法
- 【Nginx】服务器安装于配置
- Nginx 图片服务器安装配置
- CentOS安装配置Nginx服务器
- 【服务器】nginx安装和配置
- pyhton爬虫(8)——获取网易新闻内容
- 好记性不如烂键盘,今天开始, 记录点滴,茁壮成长^_^~
- JVM系列六(HotSpot的算法实现)
- hibernate的Query对象的setPatameter()方法与setString()方法的区别
- 网口扫盲三:以太网芯片MAC和PHY的关系
- Nginx系列—服务器安装与配置
- VUE+WebPack游戏设计:用CSS实现扑克牌翻转特效
- [POJ2241]最高巴比伦塔
- centos7、unbutu14安装rabbitmq
- 自己手写的一个关于中英文切换时间的工具类
- 10 SQL optimization
- DTcms(四)
- 手机软键盘搜索按钮实现点击搜索功能
- struts原理(手写原理)