2

I have an Apache server on Ubunu 18.04 with a Drupal 8 site that I want to migrate to Nginx and PHP7.3-FPM.

This is the only site on the server. I do not know Nginx at all.

I deleted Apache with the following command :

sudo apt autoremove --purge apache2*

Here is the configuration I had on Apache :

<VirtualHost *:80>
   ServerAdmin contact@domaine.com
   ServerName domaine.com
   ServerAlias www.domaine.com
   Protocols h2 http/1.1
   DocumentRoot /var/www/www-domaine-com/web/

   <Directory /var/www/www-domaine-com/web>
      Options +Includes -Indexes +FollowSymLinks
      AllowOverride All
      Require all granted
   </Directory>

   <FilesMatch \.php$>
      SetHandler "proxy:unix:/var/run/php/php7.3-fpm.sock|fcgi://localhost/"
   </FilesMatch>

   ErrorLog ${APACHE_LOG_DIR}/error.log
   CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Here's how I installed Nginx :

sudo apt install nginx
sudo ufw allow in "Nginx HTTP"

I am testing the IP address of my server and the Nginx page is displayed.

sudo unlink /etc/nginx/sites-enabled/default

Here is my Nginx configuration :

server {
    listen :80;
    listen [::]:80;
    server_name domaine.com www.domaine.com;

    root   /var/www/www-domaine-com/web;
    index  index.html index.php;

    location / {
        try_files $uri $uri/ =404;
    }
    location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            include fastcgi_params;
            fastcgi_pass unix:/run/php/php7.3-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

I create a symbolic link of my new configuration :

sudo ln -s /etc/nginx/sites-available/www.domaine.com /etc/nginx/sites-enabled/

I test my configuration :

sudo nginx -t

This message is displayed :

nginx: [emerg] no host in ":80" of the "listen" directive in /etc/nginx/sites-enabled/www-domaine-com:2
nginx: configuration file /etc/nginx/nginx.conf test failed
  • What file is that nginx configuration from? Does nginx -t return any errors? Regarding the connection refused error, check the output of ss -tnlp on your server to confirm that nginx is started and listening. – fukawi2 Feb 27 at 4:35
  • @fukawi2 I updated my question – fgtr Feb 27 at 4:46
  • @Dextro67 I followed the tutorial and I have this error "The provided host name is not valid for this server." – fgtr Feb 27 at 5:02
  • @fgtr You missed my first question - which file is your nginx snippet from? /etc/nginx/nginx.conf or a file in /etc/nginx/conf.d/? Also, are your file/directory permissions set to allow nginx user access to /var/www/html/www-s1biose-com/web? – fukawi2 Feb 27 at 5:08
4

The error message shows you have an error in the specified line of your nginx configuration.

    listen :80;

This is a typo; it should appear as:

    listen 80;
  • on the ip address I have the same error as in my question. On the domain name I have the error 404 Not Found nginx/1.14.0 (Ubuntu) – fgtr Feb 27 at 17:04
2

Based on our short dicussion in the comments, my best guess is that there is another server block in /etc/nginx/nginx.conf that is acting as the default server so when you access it by IP, nginx serves that server block instead of the one you've configured in /etc/nginx/sites-available/www-s1biose-com. You can either remove that server block from nginx.conf, or just remove default_server from the server_name directive in it and add default_server to server_name in your configuration.

You don't mention what distribution you're using, but also check that the nginx.conf file is including files from /etc/nginx/sites-available/. CentOS doesn't by default, I'm not sure about Ubuntu/Debian and others.

As to why you get a connection refused when accessing by the domain name (which should work as it matches the server_name directive in your config), check that your DNS resolution is pointing to the right IP address, and check that your browser isn't trying HTTPS instead of HTTP as you haven't configured HTTPS.

  • I'm on Ubuntu 18.04 and I only deleted the default server with the command line sudo unlink /etc/nginx/sites-enabled/default. The domain name points well to the server puique I have a 404 error – fgtr Feb 27 at 5:39
  • I updated my question – fgtr Feb 27 at 16:47

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.