Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 1711|回复: 0

[默认分类] zzw原创_非root用户启动apache的问题解决(非root用户启动apache的1024以下端口)

[复制链接]
  • TA的每日心情
    开心
    2021-12-13 21:45
  • 签到天数: 15 天

    [LV.4]偶尔看看III

    发表于 2020-8-8 14:30:49 | 显示全部楼层 |阅读模式
    场景:普通用户编译的apache,要在该用户下启动1024端口以下的apache端口
    1、假设普通用户为sims20,用该用户编译 安装了一个apache,安装路径为/opt/aspire/product/sims20/apache
        ./configure --prefix=/opt/aspire/product/sims20/apache   --enable-so --enable-modules=all   --enable-mods-shared=all   --enable-mods-shared="proxy proxy_ajp proxy_balancer proxy_connect proxy_ftp proxy_http proxy_rewrite"
      make
      make install
    2、编译完成后,设置http.conf的监听端口为80
    3、直接用普通用户sims20启动
      [sims20@bcd-app01 bin]$ ./apachectl  start
    (13)Permission denied: make_sock: could not bind to address [::]:80
    (13)Permission denied: make_sock: could not bind to address 0.0.0.0:80
    no listening sockets available, shutting down
    Unable to open logs
         出错原因:在linux下,普通用户只能用1024以上的端口,而1024以内的端口只能由root用户才可以使用
    4、利用setuid来解决问题,这样使用httpd能以root权限运行
          用root用户登录,进入/opt/aspire/product/sims20/apache/bin,分别用chown root  httpd、chmod u+s httpd 设置httpd的属主为root及特殊权限
    [root@bcd-app01 bin]# ls  -l  httpd
    -rwxr-xr-x 1 sims20 aspire 3517470  3月 15 17:12 httpd
    [root@bcd-app01 bin]# chown root  httpd
    [root@bcd-app01 bin]# ls  -l  httpd
    -rwxr-xr-x 1 root aspire 3517470  3月 15 17:12 httpd
    [root@bcd-app01 bin]# chmod u+s httpd
    [root@bcd-app01 bin]# ls  -l  httpd
    -rwsr-xr-x 1 root aspire 3517470  3月 15 17:12 httpd
    5、重新进入普通用户sims20,启动apache
              [sims20@bcd-app01 bin]$ ./apachectl  start
            可以正常启动,没报错
    6、试着访问一下
    [sims20@bcd-app01 bin]$ curl  http://10.24.12.159:80
    <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
    <html><head>
    <title>403 Forbidden</title>
    </head><body>
    <h1>Forbidden</h1>
    <p>You don"t have permission to access /
    on this server.</p>
    </body></html>
    报403 Forbidden错误
    7、看一下进程
    [sims20@bcd-app01 bin]$ ps  -ef |grep httpd
    root      7841     1  0 17:24 ?        00:00:00 /opt/aspire/product/sims20/apache/bin/httpd -k start
    daemon    7844  7841  0 17:24 ?        00:00:00 /opt/aspire/product/sims20/apache/bin/httpd -k start
    daemon    7845  7841  0 17:24 ?        00:00:00 /opt/aspire/product/sims20/apache/bin/httpd -k start
    daemon    7846  7841  0 17:24 ?        00:00:00 /opt/aspire/product/sims20/apache/bin/httpd -k start
    daemon    7847  7841  0 17:24 ?        00:00:00 /opt/aspire/product/sims20/apache/bin/httpd -k start
    daemon    7848  7841  0 17:24 ?        00:00:00 /opt/aspire/product/sims20/apache/bin/httpd -k start
    sims20    8006  3026  0 17:29 pts/4    00:00:00 grep httpd

    怎么跑出daemon 用户了,  原来httpd主进程仍然以root用户的权限运行,而它的子进程将以一个较低权限的用户运行 ,而这个较低权限用户daemon 在http.conf中配置
      
    7、在http.conf中配置一下,将用户改成root
       User daemon
       Group daemon
        改成
        User root
        Group root
    8、再次用普通用户启动apache

        [sims20@bcd-app01 bin]$ ./apachectl  restart
    Syntax error on line 76 of /opt/aspire/product/sims20/apache/conf/httpd.conf:
    Error:\tApache has not been designed to serve pages while\n\trunning as root.  There are known race conditions that\n\twill allow any local user to read any file on the system.\n\tIf you still desire to serve pages as root then\n\tadd -DBIG_SECURITY_HOLE to the CFLAGS env variable\n\tand then rebuild the server.\n\tIt is strongly suggested that you instead modify the User\n\tdirective in your httpd.conf file to list a non-root\n\tuser.\n

    不行的,要重新加参数编译

    9、再次修改在http.conf中配置一下,将用户改成普通用户吧
        改成
        User sims20
        Group aspire
    10、再次用普通用户sims20启动apache

    [sims20@bcd-app01 bin]$ ./apachectl  start
    [sims20@bcd-app01 bin]$ ps  -ef  |grep  httpd
    root      9720     1  0 18:09 ?        00:00:00 /opt/aspire/product/sims20/apache/bin/httpd -k start
    sims20    9721  9720  0 18:09 ?        00:00:00 /opt/aspire/product/sims20/apache/bin/httpd -k start
    sims20    9722  9720  0 18:09 ?        00:00:00 /opt/aspire/product/sims20/apache/bin/httpd -k start
    sims20    9723  9720  0 18:09 ?        00:00:00 /opt/aspire/product/sims20/apache/bin/httpd -k start
    sims20    9724  9720  0 18:09 ?        00:00:00 /opt/aspire/product/sims20/apache/bin/httpd -k start
    sims20    9725  9720  0 18:09 ?        00:00:00 /opt/aspire/product/sims20/apache/bin/httpd -k start
    sims20    9739  3026  0 18:09 pts/4    00:00:00 grep httpd

    6、试着访问一下

    [sims20@bcd-app01 bin]$ curl  http://10.248.12.159:80
    <html><body><h1>It works!</h1></body></html>

        成功了
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-3-28 21:37 , Processed in 0.368820 second(s), 46 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表