Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@pranaysonisoft
Forked from kennwhite/nginx.overrides
Created May 4, 2018 18:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pranaysonisoft/c2473fe01963dd2052ac1a22fdb43196 to your computer and use it in GitHub Desktop.
Save pranaysonisoft/c2473fe01963dd2052ac1a22fdb43196 to your computer and use it in GitHub Desktop.
Restart / Reload Nginx without Entering Sudo Password
# Enter this command to create a sudoers override/include file:
# sudo visudo -f /etc/sudoers.d/nginx.overrides
# (Make sure you actually have this in your /etc/sudoers - Run `sudo visudo` to check)
# #includedir /etc/sudoers.d
# This file assumes your deployment user is `deploy`
# Nginx Commands
Cmnd_Alias NGINX_RESTART = /usr/sbin/service nginx restart
Cmnd_Alias NGINX_RELOAD = /usr/sbin/service nginx reload
# No-Password Commands
deploy ALL=NOPASSWD: NGINX_RESTART, NGINX_RELOAD
@pranaysonisoft
Copy link
Author

sudo service nginx restart

sudo chmod a+rwx -R /etc/nginx/sites-enabled/

sudo chmod a+rwx -R /etc/nginx/sites-available/

@pranaysonisoft
Copy link
Author

sudo visudo

@pranaysonisoft
Copy link
Author

Add a Host Record

sudo nano /etc/hosts

127.0.0.1 dev.example1.com

@pranaysonisoft
Copy link
Author

First Login with root
sudo su

chmod a+w /etc/hosts

echo "192.241.xx.xx test.betaproduct.com" >> /etc/hosts

@pranaysonisoft
Copy link
Author

pranaysonisoft commented Jun 7, 2019

Main Stack link https://stackoverflow.com/questions/3011067/restart-nginx-without-sudo

There is a better answer on Stack Overflow that does not involve writing a custom script:

The best practice is to use /etc/sudoers.d/myuser

The /etc/sudoers.d/ folder can contain multiple files that allow users to call stuff using sudo without being root.

The file usually contains a user and a list of commands that the user can run without having to specify a password.

Instructions:

In all commands, replace myuser with the name of your user that you want to use to restart nginx without sudo.

Open sudoers file for your user:

$ sudo visudo -f /etc/sudoers.d/myuser
Editor will open. There you paste the following line. This will allow that user to run nginx start, restart, and stop:

myusername ALL=(ALL) NOPASSWD: /usr/sbin/service nginx start,/usr/sbin/service nginx stop,/usr/sbin/service nginx restart,/usr/sbin/service nginx reload
Save by hitting ctrl+o. It will ask where you want to save, simply press enter to confirm the default. Then exit out of the editor with ctrl+x.

Now you can restart (and start and stop) nginx without password. Let's try it.

Open new session (otherwise, you might simply not be asked for your sudo password because it has not timed out):

$ ssh myusername@myserver
Stop nginx

$ sudo /usr/sbin/service nginx stop
Confirm that nginx has stopped by checking your website or running ps aux | grep nginx

Start nginx

$ sudo /usr/sbin/service nginx start
Confirm that nginx has started by checking your website or running ps aux | grep nginx

PS: Make sure to use sudo /usr/sbin/service nginx start|restart|stop, and not sudo service nginx start|restart|stop.

@pranaysonisoft
Copy link
Author

if error like this , if you set a wrong on sudoers file then

$ sudo visudo

/etc/sudoers: syntax error near line 28 <<<
sudo: parse error in /etc/sudoers near line 28
sudo: no valid sudoers sources found, quitting

https://askubuntu.com/questions/73864/how-to-modify-an-invalid-etc-sudoers-file


pkexec visudo


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment