Nginx实战基础篇一 源码包编译安装部署web服务器

简介:

实验步骤:

一、安装nginx必须的依赖包 



  
  
  1. [root@rhel6u3-7 ~]# yum -y install gcc openssl-devel pcre-devel zlib-devel  //yum创建过程略,安装略 

二、安装编译nginx,目前系统测试环境为rhel6.3  软件版本为nginx-1.27


 
 
  1. [root@rhel6u3-7 ~]# useradd nginx -s /sbin/nologin //给nginx服务器创建后台进程管理用户 
  2. [root@rhel6u3-7 ~]# tar zxvf nginx-1.2.7.tar.gz  //解压缩 
  3. [root@rhel6u3-7 ~]# cd nginx-1.2.7  
  4. [root@rhel6u3-7 nginx-1.2.7]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx/ --with-http_stub_status_module --with-http_ssl_module   
  5. // --user=nginx –group=nginx 设置允许nginx允许的用户和组为nginx 
  6. // --prefix=/usr/local/nginx/  设置nginx安装路径 
  7. // --with-http_stub_status_module 安装允许状态模块 
  8. // --with-http_ssl_module 安装ssl模块 
  9. //更多参数请参看 ./configure --help 
  10. ……  //安装显示略,如果配置正确,最后会显示以下信息。 
  11. Configuration summary 
  12.   + using system PCRE library 
  13.   + using system OpenSSL library 
  14.   + md5: using OpenSSL library 
  15.   + sha1: using OpenSSL library 
  16.   + using system zlib library 
  17.   nginx path prefix: "/usr/local/nginx/" 
  18.   nginx binary file: "/usr/local/nginx//sbin/nginx" 
  19.   nginx configuration prefix: "/usr/local/nginx//conf" 
  20.   nginx configuration file: "/usr/local/nginx//conf/nginx.conf" 
  21.   nginx pid file: "/usr/local/nginx//logs/nginx.pid" 
  22.   nginx error log file: "/usr/local/nginx//logs/error.log" 
  23.   nginx http access log file: "/usr/local/nginx//logs/access.log" 
  24.   nginx http client request body temporary files: "client_body_temp" 
  25.   nginx http proxy temporary files: "proxy_temp" 
  26.   nginx http fastcgi temporary files: "fastcgi_temp" 
  27.   nginx http uwsgi temporary files: "uwsgi_temp" 
  28.   nginx http scgi temporary files: "scgi_temp" 
  29. [root@rhel6u3-7 ~]# /usr/local/nginx/sbin/nginx  –V  //安装完成后查看Nginx的相关环境配置信息是否正确 
  30. nginx version: nginx/1.2.7 
  31. built by gcc 4.4.6 20120305 (Red Hat 4.4.6-4) (GCC)  
  32. TLS SNI support enabled 
  33. configure arguments: --user=nginx --group=nginx --prefix=/usr/local/nginx/ --with-http_stub_status_module --with-http_ssl_module 
  34. [root@rhel6u3-7 ~]# 
  35. [root@rhel6u3-7 nginx-1.2.7]# make & make install   //编译安装过程略 

三、通过nginx自身脚本机器nginx服务器,并通过各种命令查看是否启动成功


 
 
  1. [root@rhel6u3-7 ~]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf   // -c指向nginx主配置文件 
  2. [root@rhel6u3-7 ~]# lsof -i :80  //查看nginx进程号及运行情况 
  3. COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME 
  4. nginx   4080  root    6u  IPv4  21467      0t0  TCP *:http (LISTEN) 
  5. nginx   4081 nginx    6u  IPv4  21467      0t0  TCP *:http (LISTEN) 
  6. [root@rhel6u3-7 ~]# ps -ef | grep nginx  //查看nginx进程号及运行情况 
  7. root      4080     1  0 22:24 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 
  8. nginx     4081  4080  0 22:24 ?        00:00:00 nginx: worker process                                           
  9. root      4088  1433  0 22:27 pts/0    00:00:00 grep nginx 
  10. [root@rhel6u3-7 ~]# netstat -nltp | grep 80  //查看nginx进程监听端口 
  11. tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      4080/nginx  

 

,测试nginx配置的默认web服务器是否能正常运行

通过linux自带命令links 测试



  
  
  1. [root@rhel6u3-7 ~]# links 127.0.0.1  //出现 welcome to nginx!说明nginx服务启动成功

通过windows服务器IE浏览器测试


扩展知识:

1、设置nginx开机自动启动,将nginx启动命令添加到/etc/rc.local 


 
 
  1. [root@rhel6u3-7 ~]# echo "/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf" >> /etc/rc.local  
  2. [root@rhel6u3-7 ~]# cat /etc/rc.local | grep nginx 
  3. /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 

2、通过设置System V 脚本,使用server命令启动nginx服务


 
 
  1. [root@rhel6u3-7 init.d]# vim  nginx  //在/etc/init.d/下创建nginx启动脚本文件 
  2. #!/bin/sh 
  3. # nginx - this script starts and stops the nginx daemon 
  4. # chkconfig: - 85 15 
  5. # description: Nginx is an HTTP(S) server, HTTP(S) reverse \ 
  6. #   proxy and IMAP/POP3 proxy server 
  7. # processname: nginx 
  8. # config: /etc/nginx/nginx.conf 
  9. # config: /etc/sysconfig/nginx 
  10. # pidfile: /var/run/nginx.pid 
  11. # Source function library. 
  12. . /etc/rc.d/init.d/functions 
  13. # Source networking configuration. 
  14. . /etc/sysconfig/network 
  15. # Check that networking is up. 
  16. [ "$NETWORKING" = "no" ] && exit 0 
  17.     nginx="/usr/local/nginx/sbin/nginx" 
  18.     prog=$(basename $nginx) 
  19.     NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" 
  20. [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx 
  21.     lockfile=/var/lock/subsys/nginx 
  22.  
  23. start() { 
  24.     [ -x $nginx ] || exit 5 
  25.     [ -f $NGINX_CONF_FILE ] || exit 6 
  26.     echo -n $"Starting $prog: " 
  27.     daemon $nginx -c $NGINX_CONF_FILE 
  28.     retval=$? 
  29.     echo 
  30. [ $retval -eq 0 ] && touch $lockfile 
  31.     return $retval 
  32.  
  33. stop() { 
  34.     echo -n $"Stopping $prog: " 
  35.     killproc $prog -QUIT 
  36.     retval=$? 
  37.     echo 
  38. [ $retval -eq 0 ] && rm -f $lockfile 
  39.     return $retval 
  40.     killall -9 nginx 
  41.  
  42. restart() { 
  43.     configtest || return $? 
  44.     stop 
  45.     sleep 1 
  46.     start 
  47.  
  48. reload() { 
  49.     configtest || return $? 
  50.     echo -n $"Reloading $prog: " 
  51.     killproc $nginx -HUP 
  52.     RETVAL=$? 
  53.     echo 
  54.  
  55. force_reload() { 
  56.     restart 
  57.  
  58. configtest() { 
  59.     $nginx -t -c $NGINX_CONF_FILE 
  60.  
  61. rh_status() { 
  62.     status $prog 
  63.  
  64. rh_status_q() { 
  65.     rh_status >/dev/null 2>&1 
  66.  
  67. case "$1" in 
  68.     start) 
  69.         rh_status_q && exit 0 
  70.         $1 
  71.     ;; 
  72.     stop) 
  73.         rh_status_q || exit 0 
  74.         $1 
  75.     ;; 
  76.     restart|configtest) 
  77.         $1 
  78.     ;; 
  79.     reload) 
  80.         rh_status_q || exit 7 
  81.         $1 
  82.     ;; 
  83.     force-reload) 
  84.         force_reload 
  85.     ;; 
  86.     status) 
  87.         rh_status 
  88.     ;; 
  89.     condrestart|try-restart) 
  90.         rh_status_q || exit 0 
  91.     ;; 
  92.     *) 
  93.         echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" 
  94.         exit 2 
  95. esac 


 
 
  1. [root@rhel6u3-7 init.d]# chmod 755 nginx   //修改脚本文件nginx的权限 
  2. [root@rhel6u3-7 init.d]# chkconfig --add nginx  //将脚本文件加入chkconfig中 
  3. [root@rhel6u3-7 init.d]# chkconfig --level 35 nginx on  //设置nginx开机在3和5级别自动启动 
  4. [root@rhel6u3-7 init.d]# chkconfig --list | grep nginx 
  5. nginx           0:off   1:off   2:off   3:on    4:off   5:on    6:off 

本文转自凌激冰51CTO博客,原文链接:http://blog.51cto.com/dreamfire/1140965,如需转载请自行联系原作者
相关文章
|
2月前
|
弹性计算 监控 负载均衡
|
2月前
|
负载均衡 监控 应用服务中间件
配置Nginx反向代理时如何指定后端服务器的权重?
配置Nginx反向代理时如何指定后端服务器的权重?
173 61
|
1月前
|
存储 监控 调度
云服务器成本优化深度解析与实战案例
本文深入探讨了云服务器成本优化的策略与实践,涵盖基本原则、具体策略及案例分析。基本原则包括以实际需求为导向、动态调整资源、成本控制为核心。具体策略涉及选择合适计费模式、优化资源配置、存储与网络配置、实施资源监控与审计、应用性能优化、利用优惠政策及考虑多云策略。文章还通过电商、制造企业和初创团队的实际案例,展示了云服务器成本优化的有效性,最后展望了未来的发展趋势,包括智能化优化、多云管理和绿色节能。
|
2月前
|
弹性计算 开发工具 git
2分钟在阿里云ECS控制台部署个人应用(图文示例)
作为一名程序员,我在部署托管于Github/Gitee的代码到阿里云ECS服务器时,经常遇到繁琐的手动配置问题。近期,阿里云ECS控制台推出了一键构建部署功能,简化了这一过程,支持Gitee和GitHub仓库,自动处理git、docker等安装配置,无需手动登录服务器执行命令,大大提升了部署效率。本文将详细介绍该功能的使用方法和适用场景。
2分钟在阿里云ECS控制台部署个人应用(图文示例)
|
1月前
|
存储 编解码 应用服务中间件
使用Nginx搭建流媒体服务器
本文介绍了流媒体服务器的特性及各种流媒体传输协议的适用场景,并详细阐述了使用 nginx-http-flv-module 扩展Nginx作为流媒体服务器的详细步骤,并提供了在VLC,flv.js,hls.js下的流媒体拉流播放示例。
188 1
|
3月前
|
机器学习/深度学习 弹性计算 运维
云计算系列之阿里云ECS服务器管理实战
本文档介绍了阿里云ECS(Elastic Compute Service)的基本概念、实例管理、磁盘操作、快照与镜像功能及其应用场景,最后通过具体案例解析ECS的实际应用。ECS是阿里云提供的高效、可靠的云计算服务,支持多种业务需求,如Web应用、高并发网站、数据库等,帮助企业快速构建稳定安全的应用,提升运维效率,降低IT成本。文档还详细说明了ECS实例的创建方式、连接方法及日常管理操作,帮助用户更好地利用ECS服务。
111 2
云计算系列之阿里云ECS服务器管理实战
|
2月前
|
XML 前端开发 JavaScript
PHP与Ajax在Web开发中的交互技术。PHP作为服务器端脚本语言,处理数据和业务逻辑
本文深入探讨了PHP与Ajax在Web开发中的交互技术。PHP作为服务器端脚本语言,处理数据和业务逻辑;Ajax则通过异步请求实现页面无刷新更新。文中详细介绍了两者的工作原理、数据传输格式选择、具体实现方法及实际应用案例,如实时数据更新、表单验证与提交、动态加载内容等。同时,针对跨域问题、数据安全与性能优化提出了建议。总结指出,PHP与Ajax的结合能显著提升Web应用的效率和用户体验。
69 3
|
2月前
|
NoSQL 容灾 MongoDB
MongoDB主备副本集方案:两台服务器使用非对称部署的方式实现高可用与容灾备份
在资源受限的情况下,为了实现MongoDB的高可用性,本文探讨了两种在两台服务器上部署MongoDB的方案。方案一是通过主备身份轮换,即一台服务器作为主节点,另一台同时部署备节点和仲裁节点;方案二是利用`priority`设置实现自动主备切换。两者相比,方案二自动化程度更高,适合追求快速故障恢复的场景,而方案一则提供了更多的手动控制选项。文章最后对比了这两种方案与标准三节点副本集的优缺点,指出三节点方案在高可用性和数据一致性方面表现更佳。
144 5
|
2月前
|
PHP 数据库 数据安全/隐私保护
布谷直播源码部署服务器关于数据库配置的详细说明
布谷直播系统源码搭建部署时数据库配置明细!
|
3月前
|
关系型数据库 MySQL Linux
基于阿里云服务器Linux系统安装Docker完整图文教程(附部署开源项目)
基于阿里云服务器Linux系统安装Docker完整图文教程(附部署开源项目)
742 3