Ubuntu – apache2 can’t stop and nginx can’t start

Apache2firefoxnginxserver

I have installed both apache2 and nginx as web server.
I found that it is a strange thing that apache2 logo will be still displayed in the firefox when service apache2 stop executed and nginx logo can't be displayed in the firefox when service nginx start executed.
enter image description here

Whatever you stop apache2 or start nginx ,the apache2 debian default page always be display in my firefox.

enter image description here

The config file /etc/apache2/sites-available/000-default.conf of apache2 is
as below:

<VirtualHost *:8080>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

The config file /etc/nginx/sites-available/default of nginx
is as below:

server {
    listen       8080;
    server_name  127.0.0.1;
    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;

    location / {
            try_files $uri $uri/ =404;
    }

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;

    location = /50x.html {
            root /var/www/html;
    }

    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
    }
}

In my os ,

ls /var/www/html
index.nginx-debian.html  index.html

The index.html is a apache2 debian logo,everytime 127.0.0.1:8080 executed, index.html was called no matter apache2 or nginx ,apache2 debian logo was displayed.
Problem solved.

Best Answer

Really there's two questions here, so I'll try to answer them both:

1. Why do Apache and Nginx display the same webpage?

Take a close look at your configuration files. You will see that they both load the same content:

Apache is loading (document root): /var/www/html

Nginx is loading (root): /var/www/html;

This means both servers will show the same content, as they are loading the same file(s). You can change your "root" directory to be two different locations by editing your configuration files.

eg apache:

<VirtualHost *:8080>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/apache
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

eg nginx:

server {
    listen       8080;
    server_name  127.0.0.1;
    root /var/www/html/nginx;
    index index.php index.html index.htm index.nginx-debian.html;

    location / {
            try_files $uri $uri/ =404;
    }

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;

    location = /50x.html {
            root /var/www/html/nginx;
    }

    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
    }
}

You'd then need to make each directory and put a file in them:

mkdir /var/www/html/nginx

mkdir /var/www/html/apache

And put content into each of these folders. It's also worth checking the permissions of each folder are appropriate for the web server to access them.

2. How do I check to see if Apache has stopped and Nginx has started

There's a simple way to check the status of each service. Assuming you're logged in as root:

service apache2 stop #Stop apache
service apache2 status #is apache still running?

You can also use the ps command to get the process list:

ps aux | grep -i apache

This effectively gets all running processes, and searches for any with the name apache.

You can then start nginx and check it's running:

service nginx start #start nginx
service nginx status #is nginx running?

(optionally another way to prove the process is running): ps aux | grep -i nginx

It's also possible to see which program is listening on which port:

netstat -ntlp

This will tell you the local address, port, and process name of everything listening for connections on your computer. You should see in this list either nginx or apache running with port 8080 (based on your configuration files) exposed.

Related Question