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. # 
  4. # nginx - this script starts and stops the nginx daemon 
  5. # 
  6. # chkconfig: - 85 15 
  7. # description: Nginx is an HTTP(S) server, HTTP(S) reverse \ 
  8. #   proxy and IMAP/POP3 proxy server 
  9. # processname: nginx 
  10. # config: /etc/nginx/nginx.conf 
  11. # config: /etc/sysconfig/nginx 
  12. # pidfile: /var/run/nginx.pid 
  13. # Source function library. 
  14. . /etc/rc.d/init.d/functions 
  15. # Source networking configuration. 
  16. . /etc/sysconfig/network 
  17. # Check that networking is up. 
  18. [ "$NETWORKING" = "no" ] && exit 0 
  19.     nginx="/usr/local/nginx/sbin/nginx" 
  20.     prog=$(basename $nginx) 
  21.     NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" 
  22. [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx 
  23.     lockfile=/var/lock/subsys/nginx 
  24.  
  25. start() { 
  26.     [ -x $nginx ] || exit 5 
  27.     [ -f $NGINX_CONF_FILE ] || exit 6 
  28.     echo -n $"Starting $prog: " 
  29.     daemon $nginx -c $NGINX_CONF_FILE 
  30.     retval=$? 
  31.     echo 
  32. [ $retval -eq 0 ] && touch $lockfile 
  33.     return $retval 
  34. } 
  35.  
  36. stop() { 
  37.     echo -n $"Stopping $prog: " 
  38.     killproc $prog -QUIT 
  39.     retval=$? 
  40.     echo 
  41. [ $retval -eq 0 ] && rm -f $lockfile 
  42.     return $retval 
  43.     killall -9 nginx 
  44. } 
  45.  
  46. restart() { 
  47.     configtest || return $? 
  48.     stop 
  49.     sleep 1 
  50.     start 
  51. } 
  52.  
  53. reload() { 
  54.     configtest || return $? 
  55.     echo -n $"Reloading $prog: " 
  56.     killproc $nginx -HUP 
  57.     RETVAL=$? 
  58.     echo 
  59. } 
  60.  
  61. force_reload() { 
  62.     restart 
  63. } 
  64.  
  65. configtest() { 
  66.     $nginx -t -c $NGINX_CONF_FILE 
  67. } 
  68.  
  69. rh_status() { 
  70.     status $prog 
  71. } 
  72.  
  73. rh_status_q() { 
  74.     rh_status >/dev/null 2>&1 
  75. } 
  76.  
  77. case "$1" in 
  78.     start) 
  79.         rh_status_q && exit 0 
  80.         $1 
  81.     ;; 
  82.     stop) 
  83.         rh_status_q || exit 0 
  84.         $1 
  85.     ;; 
  86.     restart|configtest) 
  87.         $1 
  88.     ;; 
  89.     reload) 
  90.         rh_status_q || exit 7 
  91.         $1 
  92.     ;; 
  93.     force-reload) 
  94.         force_reload 
  95.     ;; 
  96.     status) 
  97.         rh_status 
  98.     ;; 
  99.     condrestart|try-restart) 
  100.         rh_status_q || exit 0 
  101.     ;; 
  102.     *) 
  103.         echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" 
  104.         exit 2 
  105. 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,如需转载请自行联系原作者
相关文章
|
缓存 负载均衡 JavaScript
Nginx:高性能Web服务器与反向代理利器
Nginx:高性能Web服务器与反向代理利器
682 110
|
负载均衡 Cloud Native 前端开发
Nginx:高性能的Web服务器与反向代理利器
Nginx:高性能的Web服务器与反向代理利器
520 100
|
缓存 负载均衡 前端开发
Nginx:高性能Web服务器的核心力量
Nginx:高性能Web服务器的核心力量
473 100
|
缓存 负载均衡 前端开发
Nginx:高性能的Web服务器与反向代理利器
Nginx:高性能的Web服务器与反向代理利器
862 99
|
负载均衡 前端开发 安全
Nginx:高性能的Web服务器与反向代理利器
Nginx:高性能的Web服务器与反向代理利器
539 98
|
缓存 负载均衡 前端开发
Nginx:高性能Web服务器的核心引擎
Nginx:高性能Web服务器的核心引擎
623 99
|
缓存 负载均衡 前端开发
Nginx:高性能Web服务器的核心引擎
Nginx:高性能Web服务器的核心引擎
465 47
|
Ubuntu 安全 应用服务中间件
详细指南:配置Nginx服务器在Ubuntu平台上
以上步骤涵盖了基本流程:从软件包管理器获取 Ngnix, 设置系统服务, 调整UFW规则, 创建并激活服务器块(也称作虚拟主机), 并进行了初步优化与加固措施。这些操作都是建立在命令行界面上,并假设用户具有必要权限(通常是root用户)来执行这些命令。每个操作都有其特定原因:例如,设置开机启动确保了即使重启后也能自动运行 Ngnix;而编辑server block则定义了如何处理进入特定域名请求等等。
783 18
|
Ubuntu 安全 应用服务中间件
详细指南:配置Nginx服务器在Ubuntu平台上
以上步骤涵盖了基本流程:从软件包管理器获取 Ngnix, 设置系统服务, 调整UFW规则, 创建并激活服务器块(也称作虚拟主机), 并进行了初步优化与加固措施。这些操作都是建立在命令行界面上,并假设用户具有必要权限(通常是root用户)来执行这些命令。每个操作都有其特定原因:例如,设置开机启动确保了即使重启后也能自动运行 Ngnix;而编辑server block则定义了如何处理进入特定域名请求等等。
1238 17
|
缓存 监控 前端开发
详述uniapp项目部署于Nginx服务器的配置优化方法。
综上所述,uniapp项目部署于Nginx的优化方法多种多样,应根据实际情况灵活地采取合适的策略。配置后持续监控和调试,适时调整配置以保持最佳性能,并确保随着应用需求和访问模式的变化,服务器配置得到适当的更新和优化。
579 0