Run Apache httpd server from a non-root user privilege

Apache

Generally Apache2 or HTTPD service is started by the superuser root account on any Linux distribution. However on a specific instance or requirement you may need to start the Apache Web Server by a non-root or normal user account. Refer to the solution section for more information.

Topic

  • How to start Apache Web server by a non-root user account?
  • Start Apache service by a non-root user account.
  • How to start Apache HTTPD with non-root account privileges?

apt

  • Linux
  • Apache2
  • HTTPD
  • Centos 7
  • Ubuntu
  • Debian

Solution


There are many ways to start/stop Apache Web Server by a non-root user. In this article, we’ll fulfill this requirement with 5 methods. test is the user account has been mentioned at several places in this article to start Apace service.

  1. Sudo privileges
  2. Polkit privilege
  3. Systemd custom Unit
  4. Linux capabilities
  5. SetUID bit

Method 1: Sudo privileges

  • Provide the non-root account sudo privileges to start the service. For example test user wants to start Apache service.
  • Add the following configuration to /etc/sudoers file. In case your user is different, replace the test user with the user account name of your choice.
test        ALL=(ALL)       NOPASSWD: /usr/bin/systemctl start httpd, /usr/bin/systemctl stop httpd, /usr/bin/systemctl reload httpd, /usr/bin/systemctl status httpd

  • Now login to test user account and execute the following commands with sudo prefix to manage service.
Start service
$ sudo /usr/bin/systemctl start httpd
Stop Service
$ sudo /usr/bin/systemctl stop httpd
Reload Service
$ sudo /usr/bin/systemctl reload httpd
Check service status
$ sudo  /usr/bin/systemctl status httpd


Method 2: Polkit privilege

  • Add the test user account to wheel group.
  • Polkit will authorize the test user account to start any system service or perform any system activity with pkexec prefix.
  • Execute the following commands to start or manage service after adding the test user to wheel group
$ pkexec systemctl start httpd
$ pkexec systemctl stop httpd


Method 3: Systemd custom Unit

  • In this method, we’ll start the service during system startup by test user account with Systemd.
  • Create a systemd Unit file /etc/systemd/system/httpd.service as per following example.
Replace the test user and group with the user of your choice 

# cat /etc/systemd/system/httpd.service

[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)

[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/httpd
ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful
ExecStop=/bin/kill -WINCH ${MAINPID}
KillSignal=SIGCONT
PrivateTmp=true
USER=test
GROUP=test

  • Reload systemd
# systemctl daemon-reload

  • Enable the service Unit to start during reboot

    # systemctl enable httpd

  • You can now reboot the system and check service status.


Method 4: Linux capabilities

Refer to the following two articles to get detailed understanding of Linux capabilities.

User test
Group test

  • Modify /usr/lib/tmpfiles.d/httpd.conf configuration file and replace apache and root user with test user account.
d /run/httpd   710 test test
d /run/httpd/htcacheclean   700 test test

  • Change user ownership of /var/log/httpd to test user account.
# chown -R test /var/log/httpd

  • Set cap_net_bind_service capability to /usr/sbin/httpd apache binary from root login.
# setcap cap_net_bind_service=+epi /usr/sbin/httpd

Then execute below command for validation. 

# getcap /usr/sbin/httpd
/usr/sbin/httpd = cap_net_bind_service+eip

  • Now start HTTPD service from test user login
Start Service
[test@centos-testsrv ~]$ httpd -k start
[test@centos-testsrv ~]$ ps -ef | grep httpd
test      3691     1  0 00:27 ?        00:00:00 httpd -k start
test      3692  3691  0 00:27 ?        00:00:00 httpd -k start
test      3693  3691  0 00:27 ?        00:00:00 httpd -k start
test      3694  3691  0 00:27 ?        00:00:00 httpd -k start
test      3695  3691  0 00:27 ?        00:00:00 httpd -k start
test      3696  3691  0 00:27 ?        00:00:00 httpd -k start

Stop Service
# httpd -k stop


Method 5: SetUID bit

Refer to the following two articles to get detailed understanding of Linux capabilities and SetUID bit.

User test
Group test

  • Modify /usr/lib/tmpfiles.d/httpd.conf configuration file and replace apache and root user with test user account.
d /run/httpd   710 test test
d /run/httpd/htcacheclean   700 test test

  • Change user ownership of /var/log/httpd to test user account.
# chown -R test /var/log/httpd

  • Apply SetUID bit permission to /usr/sbin/httpd binary.
# chmod u+s /usr/sbin/httpd
# ls -l /usr/sbin/httpd
-rwsr-xr-x. 1 root root 523680 Aug  8 17:12 /usr/sbin/httpd

  • Now start HTTPD service from test user login
Start Service
[test@centos-testsrv ~]$ httpd -k start
[test@centos-testsrv ~]$ ps -ef | grep httpd
root      3745     1  0 00:37 ?        00:00:00 httpd -k start
test      3746  3745  0 00:37 ?        00:00:00 httpd -k start
test      3747  3745  0 00:37 ?        00:00:00 httpd -k start
test      3748  3745  0 00:37 ?        00:00:00 httpd -k start
test      3749  3745  0 00:37 ?        00:00:00 httpd -k start
test      3750  3745  0 00:37 ?        00:00:00 httpd -k start

Stop Service
# httpd -k stop
avatar

About the Author: TekFik

TekFik is a technical blogging site helps techies and engineers to solve their day to day issues and also allows everyone to share knowledge and feedback. Please feel free to contact us at tekfik.rd@gmail.com if there is anything.

0 Comments

  1. avatar Solicitar Prestamo Personal says:
    Your comment is awaiting moderation. This is a preview, your comment will be visible after it has been approved.

    Prestamos Rapidos Y Faciles Online Casa De Prestamos De Dinero Dinero En Linea Rapido
    https://elprestamista.space/santa-clarita-ca/
    Pedir Credito Online Rapido Mas Prestamos Intereses De Creditos Personales
    https://elprestamista.space/lubbock-tx/
    Creditos Online Urgentes Creditos Personales Inmediatos Companias De Prestamos De Dinero Creditos Para Devolver A Plazos Minicreditos Rapidos
    https://elprestamista.space/west-orange-nj/
    Prestamos Online Mini Creditos Para Pagar A Plazos Creditos De 300 Euros Rapidos Financieras Prestamos De Dinero Interes Para Prestamos Personales

  2. avatar Calculadora Para Prestamos Personales says:
    Your comment is awaiting moderation. This is a preview, your comment will be visible after it has been approved.

    Pedir Prestamo Personal Prestamos Por Linea Interes Para Prestamos Personales Simulador De Credito
    https://prestamosurgente.space/pembroke-pines-fl/
    Prestamos Online Ya Donde Prestan Dinero Rapido Requisitos Para Un Credito Personal Como Hacer Prestamo En Personal Prestamo Simple
    https://prestamosurgente.space/oro-valley-az/
    Financiamiento Prestamos Personales Sin Bancos Tipo De Interes Actual Para Prestamos Personales Prestamos Personales Inmediatos En Linea
    https://prestamosurgente.space/sunnyvale-ca/
    Dinero Prestamos Personales Donde Me Pueden Prestar Dinero Facil Y Rapido Prestamos Personales Con Veraz

  3. avatar Financieras De Creditos Rapidos says:
    Your comment is awaiting moderation. This is a preview, your comment will be visible after it has been approved.

    Como Hacer Credito Rapido Pago De Prestamo Prestamos En Dolares Como Conseguir Un Prestamo Rapido Y Facil Casas De Creditos Personales
    https://prestamosurgente.space/west-palm-beach-fl/
    Credito De Prestamos Individuales Prestamos Personales De Dinero Dinero Urgente Online Creditos Baratos
    https://prestamosurgente.space/alamogordo-nm/
    Facil Prestamos Consulta De Credito Personal Prestamos Online Rapidos Y Seguros
    https://prestamosurgente.space/toms-river-nj/
    Donde Conseguir Un Prestamo Prestamos Rapidos Hoy Prestamos Rapidos Y Sencillos Personal Prestamo Consulta De Credito Personal

  4. avatar Paginas De Creditos Rapidos says:
    Your comment is awaiting moderation. This is a preview, your comment will be visible after it has been approved.

    Mini Prestamos Personales Necesitas Dinero Nosotros Te Prestamos Cuenta Prestamos Prestar Dinero Online El Credito Financiero
    https://prestamosenestadosunidos.space/coral-springs-fl/
    Credito Particular A Credito Quien Me Presta Dinero Rapido Financieras De Creditos Rapidos Pedir Dinero
    https://prestamosenestadosunidos.space/mesquite-nv/
    Financieras De Creditos Rapidos Creditos Super Rapidos Prestamos Online Rapidos Y Seguros Buscar Creditos Rapidos
    https://prestamosenestadosunidos.space/fort-worth-tx/
    Dinero Rapido Y Urgente Prestamos Todo Facil Prestadores De Dinero

  5. avatar Prestamos Por Internet says:
    Your comment is awaiting moderation. This is a preview, your comment will be visible after it has been approved.

    Donde Solicitar Un Prestamo Rapido Credito 24h Prestamo Facil Online Pedir Credito Online
    https://creditosrapidosenusa.space/wayne-nj/
    Solicitar Prestamo En Linea Simulador Credito Personal Creditos Financieros Creditos Personales Inmediatos
    https://creditosrapidosenusa.space/east-orange-nj/
    Solicitud De Credito Personal Prestamos En Linea Necesito Un Credito Rapido Y Facil Prestamo De Dinero A Interes Como Conseguir Un Prestamo Personal Rapido Y Seguro
    https://creditosrapidosenusa.space/sandy-springs-ga/
    Donde Pedir Un Prestamo Personal Rapido Personas Que Prestan Dinero A Credito Bancos Para Prestamos Personales Busco Personas Que Prestan Dinero Rapido

  6. avatar Creditos Personales Por Internet says:
    Your comment is awaiting moderation. This is a preview, your comment will be visible after it has been approved.

    Prestamos Personales De Dinero Paginas De Creditos Rapidos Requisitos Para Sacar Un Credito Personal
    https://elprestamista.space/smyrna-ga/
    Prestamos Economicos Personales Buscar Creditos Rapidos Minicreditos Al Instante
    https://elprestamista.space/spring-tx/
    Microcreditos Quiero Pedir Un Prestamo Credito Directo
    https://elprestamista.space/macon-ga/
    Lugares De Prestamos De Dinero Prestamos Todo Facil Mejor Interes Prestamo Personal Empresa De Prestamos De Dinero

  7. avatar Prestamos Rapidos Hoy Es Confiable says:
    Your comment is awaiting moderation. This is a preview, your comment will be visible after it has been approved.

    Credito Particular Creditos Personales Online Lugares Donde Prestan Dinero Donde Hacen Prestamos De Dinero
    https://prestamosurgente.space/santa-fe-nm/
    Empresas De Credito Comparador Creditos Prestamos Pequenos
    https://prestamosurgente.space/gilbert-az/
    Prestamos De 300 Euros Rapidos Calcular Credito Simulador De Cuotas De Prestamos Dinero A Credito
    https://prestamosurgente.space/jacksonville-fl/
    Prestamos En Cuotas Online Busco Prestamista Gente Que Haga Prestamos Necesito Dinero Lugares Donde Prestan Dinero Rapido

  8. avatar Te Presto Dinero Para Lo Que Quieras says:
    Your comment is awaiting moderation. This is a preview, your comment will be visible after it has been approved.

    Credito De 300 Euros Prestamos Express En Linea Tasa Prestamos Personales Prestamos Personales Prestamistas
    https://elprestamista.space/wichita-falls-tx/
    Creditos Personales Rapidos Online Donde Es Mejor Pedir Un Prestamo Personal Empresas De Prestamos Te Presto Dinero Para Lo Que Quieras Prestamos Gratis
    https://elprestamista.space/palm-bay-fl/
    Tipos De Interes Para Prestamos Personales Donde Pedir Un Credito Rapido Credito Mas Rapido Creditos Online Prestamos Rapidos Y Seguros
    https://elprestamista.space/fort-lauderdale-fl/
    Simulador Credito Nomina Creditos Rapidos A Plazos Busco Personas Que Prestan Dinero Rapido Prestamos Personales De Bancos

  9. avatar Prestamos De Dinero Rapido Y Facil says:
    Your comment is awaiting moderation. This is a preview, your comment will be visible after it has been approved.

    Creditos A Plazos Online Prestamos Rapidos Para Pagar A Plazos Prestamos Credito Etc Donde Pedir Un Prestamo Personal Como Solicitar Un Credito Personal
    https://prestamosurgente.space/irving-tx/
    El Credito Financiero Prestamos A Domicilio De Dinero Creditos Y Prestamos Personales On Line Prestamos Personales En Estados Unidos Lugares De Prestamos
    https://prestamosurgente.space/johns-creek-ga/
    Credito Con Dinero Prestamos Personales Online Urgentes Minicreditos Online
    https://prestamosurgente.space/thousand-oaks-ca/
    Todos Los Creditos Quien Presta Dinero A Redito Prestamos Avance Donde Puedo Pedir Un Prestamo Personal Donde Puedo Sacar Un Prestamo Rapido

  10. avatar Informe De Credito says:
    Your comment is awaiting moderation. This is a preview, your comment will be visible after it has been approved.

    Prestamos Personales Urgentes En Linea Cooperativa De Credito Como Pedir Prestamo A Personal Prestamos En Linea En 24 Horas
    https://elprestamista.space/north-brunswick-nj/
    Interes Prestamo Personal Ofertas Prestamos Personales Busco Prestamos Dinero Calculo Interes Prestamo Personal
    https://elprestamista.space/cedar-park-tx/
    Prestamo Seguro Credito Privado Prestamos Personales De Bancos Calcular Credito Personal Prestamos Personales Urgentes
    https://elprestamista.space/north-valley-nm/
    Donde Prestan Dinero Facil Y Rapido Creditos Y Prestamos Online Como Aplicar Para Un Prestamo Personal Personas Que Prestan Dinero Quien Presta Dinero Facil

  11. avatar Creditos Rapidisimos says:
    Your comment is awaiting moderation. This is a preview, your comment will be visible after it has been approved.

    Mi Prestamo Personal Creditos Personales Por Internet Simulador De Credito Personal Su Credito Clientes
    https://prestamosurgente.space/newnan-ga/
    Cotizador De Creditos Personales Iaf Prestamos Personales Solo Credito Mini Creditos
    https://prestamosurgente.space/lake-havasu-city-az/
    Necesito Un Prestamista De Dinero Prestamo De Saldo Personal Lugares Donde Hacen Prestamos Prestamos Faciles Y Seguros Como Consigo Un Prestamo Rapido
    https://prestamosurgente.space/downey-ca/
    Mini Prestamos Urgentes Online Pedir Credito Prestamos En Linea En 24 Horas

Leave a Reply

Your email address will not be published. Required fields are marked *