0

I installed nginx on my Ubuntu 18.04. After that I made some changes in the default file. Now I am trying to start it but I am getting following error:

$ sudo service nginx restart

Job for nginx.service failed because the control process exited with
error code. See "systemctl status nginx.service" and "journalctl -xe"
for details.
$ systemctl status nginx.service
nginx.service - A high performance web server and a reverse proxy
server    Loaded: loaded (/lib/systemd/system/nginx.service; enabled;
vendor preset: en    Active: failed (Result: exit-code) since Sat
2019-05-25 13:35:03 CDT; 19min a
     Docs: man:nginx(8)   Process: 3220 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code   Process: 3219
ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process

May 25 13:35:01 lc2530hz nginx[3220]: nginx: [emerg] bind() to
0.0.0.0:80 failed May 25 13:35:01 lc2530hz nginx[3220]: nginx: [emerg] bind() to [::]:80 failed (9 May 25 13:35:02 lc2530hz nginx[3220]:
nginx: [emerg] bind() to 0.0.0.0:80 failed May 25 13:35:02 lc2530hz
nginx[3220]: nginx: [emerg] bind() to [::]:80 failed (9 May 25
13:35:02 lc2530hz nginx[3220]: nginx: [emerg] bind() to 0.0.0.0:80
failed May 25 13:35:02 lc2530hz nginx[3220]: nginx: [emerg] bind() to
[::]:80 failed (9 May 25 13:35:03 lc2530hz nginx[3220]: nginx: [emerg]
still could not bind() May 25 13:35:03 lc2530hz systemd[1]:
nginx.service: Control process exited, code May 25 13:35:03 lc2530hz
systemd[1]: nginx.service: Failed with result 'exit-cod May 25
13:35:03 lc2530hz systemd[1]: Failed to start A high performance web
serv lines 1-17/17 (END)
$ journalctl -xe
Defined-By: systemd
-- Support: http://www.ubuntu.com/support
--
-- Unit phpsessionclean.service has finished starting up.
--
-- The start-up result is RESULT.
May 25 13:42:32 lc2530hz systemd[1]: Starting Message of the Day...
-- Subject: Unit motd-news.service has begun start-up
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
--
-- Unit motd-news.service has begun starting up.
May 25 13:42:32 lc2530hz systemd[1]: Started Message of the Day.
-- Subject: Unit motd-news.service has finished start-up
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
--
-- Unit motd-news.service has finished starting up.
--
-- The start-up result is RESULT.
May 25 13:42:53 lc2530hz org.gnome.Shell.desktop[1989]: (/usr/lib/firefox/firefo
May 25 13:42:58 lc2530hz systemd-resolved[782]: Server returned error NXDOMAIN,
May 25 13:47:19 lc2530hz /usr/lib/gdm3/gdm-x-session[1855]: (II) event5  - PixAr
lines 1576-1598/1598 (END)
$  nginx -t
nginx: [alert] could not open error log file: open()
"/var/log/nginx/error.log" failed (13: Permission denied) 2019/05/25
14:01:03 [warn] 3564#3564: the "user" directive makes sense only if
the master process runs with super-user privileges, ignored in
/etc/nginx/nginx.conf:1 nginx: the configuration file
/etc/nginx/nginx.conf syntax is ok 2019/05/25 14:01:03 [emerg]
3564#3564: open() "/run/nginx.pid" failed (13: Permission denied)
nginx: configuration file /etc/nginx/nginx.conf test failed
zulfi@lc2530hz:/etc/nginx/sites-enabled$
/etc/nginx/sites-enabled$ cat default
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

Default server configuration
server { 
    listen 80 default_server;
    listen [::]:80 default_server;

    # SSL configuration
    #
    # listen 443 ssl default_server; 
    # listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    # 
    # Read up on ssl_ciphers to ensure a secure configuration.  
    # See: https://bugs.debian.org/765782 
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    #
    include snippets/snakeoil.conf;

    root /var/www/html;

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

    #server_name_;

    location / {
        # First attempt to serve request as file, then      
        # as directory, then fall back to displaying a 404.         
        try_files $uri $uri/=404;   
    }

    # pass PHP scripts to FastCGI server
    #   location ~ \.php$ {
    #   include snippets/fastcgi-php.conf;  
    #
    # With php-fpm (or other unix sockets):
    #   fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;     
    # With php-cgi (or other tcp sockets): 
    #   fastcgi_pass 127.0.0.1:9000;
    #            fastcgi_index index.php;
    #            include fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root     
    # concurs with nginx's one  #
    #location ~ /\.ht {     
    #   deny all;
    #} 
}


# 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 80;
#   listen [::]:80;
#
#   server_name example.com;
#
#   root /var/www/example.com;
#   index index.html;
#
#   location / {
#       try_files $uri $uri/ =404;
#   }
#} user@lc2530hz:/etc/nginx/sites-enabled$

Somebody please guide me how to restart the nginx.

1

It seems as if your Port 80 is blocked. (nginx: [emerg] bind() to 0.0.0.0:80 failed) Try:

sudo netstat -lntp | grep ":80"

to find out which process is blocking the port, kill the programm/service and restart nginx.

I'd also advice you to run nginx -t as the user nginx is running as, so the

nginx: [alert] could not open error log file: open() "/var/log/nginx/error.log" failed

gives you a clear indication on wether there actually is a problem writing the logfile. Running this as your standard user may lead to incorrect results if you don't have the same access rights as for example the http user.

1
  • This is useful. For my case I realized that after restarting my server apache2 process started running and nginx was shutdown. So I have stop apache2 and restart nginx – ultrasamad Jun 20 '20 at 9:38
0

First validate that the configuration files are valid:

sudo nginx -t

Apply the changes without restarting the Nginx service:

sudo nginx -s reload

If the changes are not applied immediately, restart the service with the following command (unlike the command used in the previous step, it will abruptly close all web client connections):

sudo systemctl restart nginx

In the extreme case, you can kill the process and start it again with:

sudo pkill -9 nginx && sudo systemctl start nginx

In some cases Nginx may fail to restart because some child processes or another process (Apache or httpd) are using port 80 or 443. In these cases I recommend stopping Nginx (sudo systemctl stop nginx) and validate if any process continues to listen in the port 80:

sudo netstat -ant | grep ": 80"

You can see more details with:

sudo lsof -i -P -n | grep ": 80"

To see the running processes more easily you can use htop.

When nobody is using the TCP/80 port, you can start Nginx again:

sudo systemctl start nginx
0

try this, it's for as root user check the service, disable, enable again and start.

sudo -i
systemctl check nginx
systemctl disable nginx
systemctl enable nginx
systemctl start nginx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.