How to use sudo or sudoers to start, stop & restart Apache on Linux Cloud Servers

2020-06-04 By Ryan 12350 Views linux sudo apache
2 reviews

sudo permits an allowed user to execute a command as the superuser or another user, as determined in the sudoers document. Setting the original and effective UID and GID to match the target user specified in the passwd file and initializing the group vector based on the group file.


This is a great way to avoid security issues and it requires users to authenticate with passwords by default.


This article will guide you to the basic commands about using sudo or sudoers to start, stop & restart Apache on a common Linux OS.


CentOS / Fedora


  1. Below are the normal commands to start, stop and restart the Apache with sudo.

    # sudo /etc/init.d/httpd start
    
    # sudo /etc/init.d/httpd stop
    
    # sudo /etc/init.d/httpd restart
    
  2. The below error will get while trying to run the above-mentioned commands without editing the sudoers file.

    **layerstack** is not in the sudoers file.  This incident will be reported.
    

    sudoapache1

    Here layerstack is a standard user that using for giving permission to start/stop/restart Apache Web Server.

  3. Now open /etc/sudoers file with any text editor and add the below line.

    # vi /etc/sudoers
    

    sudoapache2

    layerstack  ALL = /etc/init.d/httpd
    

    sudoapache3

    Note: Replace the username layerstack with your original username.

  4. Save the file by pressing,

    :w! and then Enter
    
  5. Now can start, stop and restart the Apache with sudo by using the commands mentioned earlier and using the password of the user while proceeding with these.

    sudoapache4


Ubuntu / Debian


  1. Below are the commands to start, stop and restart the Apache with sudo.

    # sudo /etc/init.d/apache2 start
    
    # sudo /etc/init.d/apache2 stop
    
    # sudo /etc/init.d/apache2 restart
    
  2. The below error will get while trying to run the above-mentioned commands without editing the sudoers file.

    **layerstack** is not in the sudoers file.  This incident will be reported.
    

    sudoapache5

    Here layerstack is a standard user that using for giving permission to start/stop/restart Apache Web Server.

  3. Now open /etc/sudoers file with any text editor and add the below line.

    # vi /etc/sudoers
    

    sudoapache6

    layerstack ALL = /etc/init.d/apache2
    

    sudoapache7

    Note: Replace the username layerstack with your original username.

  4. Save the file by pressing,

    :w! and then Enter
    
  5. Now can start, stop and restart the Apache from the user with sudo by using the commands mentioned earlier and using the password of the user while proceeding with these.

    sudoapache8


Related Tutorials

What do you think about this article?

Rate this article
LayerStack Promotion
Need assistance?

Try this guide to receive free bundled services at signup on a new free account.

Sign Up

Your Feedback Is Important

We hope you’ll give the new products and updates a try. If you have an idea for improving our products or want to vote on other user ideas so they get prioritized, please submit your feedback on our Community platform. And if you have any questions, please feel free to ask in the Community or contact our Technical Support team.