Linux操作系统安装nginx

本文介绍如何在Linux环境下下载、安装Nginx,并配置其为开机自启动服务。包括安装依赖、解压软件包、配置环境、启动及管理Nginx等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下载

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. cd /user/local  
  2. wget http://nginx.org/download/nginx-1.8.0.tar.gz  

安装依赖项

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel  

解压缩

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. tar -zxvf nginx-1.8.0.tar.gz  

环境配置

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. cd nginx-1.8.0  
  2. ./configure  

安装

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. make && make install  

启动

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. cd /usr/local/nginx/sbin  
  2. ./nginx  


查看

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. ps -aux|grep nginx  

关闭

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. ./nginx -s stop  

重启

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. ./nginx -s reload  

查看状态:

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. ./nginx -t  

如何开机自启动

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. cd /etc/init.d  
  2. touch nginx  

新建一个文件后,编辑文件把如下内容粘贴入其中

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. vi /etc/init.d/nginx  

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #!/bin/bash  
  2. # nginx Startup script for the Nginx HTTP Server  
  3. # it is v.0.0.2 version.  
  4. # chkconfig: - 85 15  
  5. # description: Nginx is a high-performance web and proxy server.  
  6. #              It has a lot of features, but it's not for everyone.  
  7. # processname: nginx  
  8. # pidfile: /var/run/nginx.pid  
  9. # config: /usr/local/nginx/conf/nginx.conf  
  10. nginxd=/usr/local/nginx/sbin/nginx  
  11. nginx_config=/usr/local/nginx/conf/nginx.conf  
  12. nginx_pid=/var/run/nginx.pid  
  13. RETVAL=0  
  14. prog="nginx"  
  15. # Source function library.  
  16. . /etc/init.d/functions  
  17. # Source networking configuration.  
  18. . /etc/sysconfig/network  
  19. # Check that networking is up.  
  20. [ ${NETWORKING} = "no" ] && exit 0  
  21. [ -x $nginxd ] || exit 0  
  22. # Start nginx daemons functions.  
  23. start() {  
  24. if [ -e $nginx_pid ];then  
  25.    echo "nginx already running...."  
  26.    exit 1  
  27. fi  
  28.    echo -n $"Starting $prog: "  
  29.    daemon $nginxd -c ${nginx_config}  
  30.    RETVAL=$?  
  31.    echo  
  32.    [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx  
  33.    return $RETVAL  
  34. }  
  35. # Stop nginx daemons functions.  
  36. stop() {  
  37.         echo -n $"Stopping $prog: "  
  38.         killproc $nginxd  
  39.         RETVAL=$?  
  40.         echo  
  41.         [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid  
  42. }  
  43. # reload nginx service functions.  
  44. reload() {  
  45.     echo -n $"Reloading $prog: "  
  46.     #kill -HUP `cat ${nginx_pid}`  
  47.     killproc $nginxd -HUP  
  48.     RETVAL=$?  
  49.     echo  
  50. }  
  51. # See how we were called.  
  52. case "$1" in  
  53. start)  
  54.         start  
  55.         ;;  
  56. stop)  
  57.         stop  
  58.         ;;  
  59. reload)  
  60.         reload  
  61.         ;;  
  62. restart)  
  63.         stop  
  64.         start  
  65.         ;;  
  66. status)  
  67.         status $prog  
  68.         RETVAL=$?  
  69.         ;;  
  70. *)  
  71.         echo $"Usage: $prog {start|stop|restart|reload|status|help}"  
  72.         exit 1  
  73. esac  
  74. exit $RETVAL  

赋予执行权限

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. chmod +x /etc/init.d/nginx  

添加服务

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. chkconfig --add nginx  

设置开机启动

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. chkconfig --level 35 nginx on  

查看是否设置成功

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. chkconfig --list | grep nginx  

此状态下表面开机启动成功

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值