Nginx: Job for nginx.service failed because the control process exited

前端 未结 19 1440
予麋鹿
予麋鹿 2021-01-29 19:31

I got a problem which I have been trying to fix for a few days now and I don\'t know what to do, have been looking for answers but all of those I found didn\'t help me.

19条回答
  •  遇见更好的自我
    2021-01-29 20:18

    The cause of the issue is this, I already had Apache web server installed and actively listening on port 80 on my local machine.

    Apache and Nginx are the two major open-source high-performance web servers capable of handling diverse workloads to satisfy the needs of modern web demands. However, Apache serves primarily as a HTTP server whereas Nginx is a high-performance asynchronous web server and reverse proxy server.

    The inability of Nginx to start was because Apache was already listening on port 80 as its default port, which is also the default port for Nginx.

    One quick workaround would be to stop Apache server by running the command below

    systemctl stop apache2
    systemctl status apache2
    

    And then starting up Nginx server by running the command below

    systemctl stop nginx
    systemctl status nginx
    

    However, this same issue will arise again when we try to start Apache server again, since they both use port 80 as their default port.

    Here's how I fixed it:

    Run the command below to open the default configuration file of Nginx in Nano editor

    sudo nano /etc/nginx/sites-available/default
    

    When the file opens in Nano editor, scroll down and change the default server port to any port of your choice. For me, I chose to change it to port 85

    # Default server configuration
    #
    server {
           listen 85 default_server;
           listen [::]:85 default_server;
    

    Also, scroll down and change the virtual host port to any port of your choice. For me, I also chose to change it to port 85

    # Virtual Host configuration for example.com
    #
    # You can move that to a different file under sites-available/ and symlink that
    # to sites-enabled/ to enable it.
    #
    # server {
    #        listen 85;
    #        listen [::]:85;
    

    Then save and exit the file by pressing on your keyboard:

    Ctrl + S
    Ctrl + X
    

    You may still be prompted to press Y on your keyboard to save your changes.

    Finally, confirm that your configuration is correct and restart the Nginx server:

    sudo nginx -t
    sudo systemctl restart nginx
    

    You can now navigate to localhost:nginx-port (localhost:85) on your browser to confirm the changes.

    Displaying the default Nginx start page

    If you want the default Nginx start page to show when you navigate to localhost:nginx-port (localhost:85) on your browser, then follow these steps:

    Examine the directory /var/www/html/ which is the default root directory for both Apache and Nginx by listing its contents:

    cd ~
    ls /var/www/html/
    

    You will 2 files listed in the directory:

    index.html                # Apache default start page
    index.nginx-debian.html   # Nginx default start page
    

    Run the command below to open the default configuration file of Nginx in Nano editor:

    cd ~
    sudo nano /etc/nginx/sites-available/default
    

    Change the order of the index files in the root directory from this:

    root /var/www/html;
    
            # Add index.php to the list if you are using PHP
            index index.html index.htm index.nginx-debian.html;
    

    to this (putting the default Nginx start page - index.nginx-debian.html in the 2nd position immediately after index):

    root /var/www/html;
    
            # Add index.php to the list if you are using PHP
            index index.nginx-debian.html index.html index.htm;
    

    Then save and exit the file by pressing on your keyboard:

    Ctrl + S
    Ctrl + X
    

    You may still be prompted to press Y on your keyboard to save your changes.

    Finally, confirm that your configuration is correct and restart the Nginx server:

    sudo nginx -t
    sudo systemctl restart nginx
    

    You can now navigate to localhost:nginx-port (localhost:85) on your browser to confirm the changes.

    That's all.

    I hope this helps

提交回复
热议问题