skip to Main Content

I’m using Mac. I used ps -ef | grep nginx which returned the following result.

$ ps -ef | grep nginx
    0 74428     1   0  3:44PM ??         0:00.01 nginx: master process nginx -c /usr/local/etc/nginx/nginx.conf  
   -2 74483 74428   0  3:47PM ??         0:00.00 nginx: worker process  
  501 75545 75489   0  4:31PM ttys003    0:00.00 grep nginx

Does it mean the Nginx is running? Is there any other reliable way to check if Nginx is running?

Additionally, does nginx restart automatically every time the machine restarts?

5

Answers


  1. The output you provided shows that nginx is indeed running on your Mac.

    • The first line with the user "root" (0) and process ID "74428" is the master process for nginx.
    • The second line with the process ID "74483" is a worker process.
    • The last line is the "grep nginx" command you used to find the nginx processes.
    Login or Signup to reply.
  2. If you installed nginx with Homebrew, you can use:

    brew services info nginx
    

    You will get a similar output:

    nginx (homebrew.mxcl.nginx)
    Running: ✔
    Loaded: ✔
    Schedulable: ✘
    User: user
    PID: 78476
    
    Login or Signup to reply.
  3. Question 1: Is Nginx running?
    Yes, the output you get indicates that Nginx is currently running on your mac.
    The first line in the output shows the master process with PID 74428 and the second line shows a worker process with PID 74483.

    Question 2: Other ways to find out if Nginx is running.

    1. Use the command nginx -t which will test the configuration files for syntax errors.
      If the configuration is valid, it will display the message "nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful."
    2. Using the command netstat -an | grep 80 to check if a process is listening on port 80, which is the default port for Nginx.
    3. Using the command lsof -i :80 to check if any process is using port 80.
    4. By using the command sudo nginx -s status that will give you the status of Nginx.
    5. Third party tools like Nagios (open-source).

    Question 3: Does nginx restart automatically every time the machine restarts?

    By default, Nginx does not automatically start at boot time on macOS, so it will not automatically restart after a machine restart. You can use launchd or launchctl to set up Nginx as a service so that it starts automatically on boot.

    If you wish to do so. 2 ways.

    1. Create a launchd plist file and use launchctl command to load it.
      Create a plist file called nginx.plist in the directory /Library/LaunchDaemons/ with the following content:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
      <dict>
        <key>Label</key>
        <string>nginx</string>
        <key>ProgramArguments</key>
        <array>
            <string>/usr/local/bin/nginx</string>
            <string>-g</string>
            <string>daemon off;</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>KeepAlive</key>
        <true/>
        <key>UserName</key>
        <string>_www</string>
        <key>GroupName</key>
        <string>_www</string>
        <key>StandardErrorPath</key>
        <string>/var/log/nginx/error.log</string>
        <key>StandardOutPath</key>
        <string>/var/log/nginx/access.log</string>
      </dict>
    </plist>
    

    Yoiu might have to change the path inside the element.
    Load the plist file using the launchctl command :

    sudo launchctl load /Library/LaunchDaemons/nginx.plist
    

    Use the command "launchctl list | grep nginx" to verify that Nginx is running.

    To remove the plist file and stop Nginx from starting automatically on boot:

    sudo rm /Library/LaunchDaemons/nginx.plist
    
    1. If you have used Homebrew to install Nginx you could you the following commands
    brew services info nginx // status answer to question 2
    brew services start nginx  // start Nginx
    brew services enable nginx // configure Nginx to start automatically on boot
    brew services list // verify that Nginx is set to automatically run on boot
    brew services stop nginx // stop Nginx
    brew services disable nginx // remove the service so not to run Nginx on boot
    
    Login or Signup to reply.
    1. ps aux | grep nginx
    2. nginx -t to test for config files
    3. sudo lsof -i :80. (check for ngnix in the list using grep command)
    4. sudo service nginx status
    Login or Signup to reply.
  4. alias nginx-status="
      test -s /var/run/nginx.pid 
      && echo '▶ Nginx is running' || echo '■ Nginx is stopped'"
    

    -s checks to see if the PID file "exists and has a size greater than zero"

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search