Score:0

How Under local ubuntu 20.04 ro install nginx with Virtual Host Configs?

ms flag

Under local ubuntu 20.04 I have appache2 installed and used and I want install nginx and and to switch them on need.

I switch to nginx with commands :

sudo systemctl disable apache2 && sudo systemctl stop apache2
sudo systemctl enable nginx
sudo systemctl restart nginx

to set 2 laravel apps using Virtual Host Configs. I edited nginx.conf :

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;


    gzip on;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

and 2 files for my sites /etc/nginx/sites-available/Laravel9TestWithNginx.conf :

##
# 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;
    listen [::]:80;

    # 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 /_wwwroot/lar/Laravel9TestWithNginx/public;

    # Add index.php to the list if you are using PHP
    index index.php;

    server_name   local-laravel9-test-with-nginx.com;

    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/php8.1-fpm.sock;
        # With php-cgi (or other tcp sockets):
        # fastcgi_pass 127.0.0.1:9000;
    }

    # 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;
#   }
#}

and /etc/nginx/sites-available/AdsBackend8.conf:

##
# 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;
    listen [::]:80 ;

    # 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 /_wwwroot/lar/AdsBackend8/public;

    # Add index.php to the list if you are using PHP
    index index.php;

    server_name local-ads-backend8.com;

    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/php8.1-fpm.sock;
        # With php-cgi (or other tcp sockets):
        # fastcgi_pass 127.0.0.1:9000;
    }

    # 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;
#   }
#}

Also I Created 2 links with commands:

sudo ln -s /etc/nginx/sites-available/Laravel9TestWithNginx.conf   /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/AdsBackend8.conf   /etc/nginx/sites-enabled/

So I have :

root@hoster-os:/etc/nginx/sites-enabled# ls -la
total 8
drwxr-xr-x 2 root root 4096 Apr 20 16:40 .
drwxr-xr-x 8 root root 4096 Apr 16 09:21 ..
lrwxrwxrwx 1 root root   43 Apr 20 16:40 AdsBackend8.conf -> /etc/nginx/sites-available/AdsBackend8.conf
lrwxrwxrwx 1 root root   53 Apr 20 16:40 Laravel9TestWithNginx.conf -> /etc/nginx/sites-available/Laravel9TestWithNginx.conf

In file /etc/hosts I have added lines:

192.168.55.164   local-laravel9-test-with-nginx.com
192.168.55.164   local-ads-backend8.com

I am not sure actually, is "192.168.55.164" nginx ip which I can use under ubuntu ?

I have empty /etc/nginx/conf.d/ subdirectory, as 2 config files are located in /etc/nginx/sites-available/ ...

But restarting the server :

systemctl restart nginx

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: enabled)
Active: active (running) since Fri 2023-04-21 17:51:41 EEST; 57s ago
Docs: man:nginx(8)
Process: 32914 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; hoster_process on; (code=exited, status=0/SUCCESS)
Process: 32915 ExecStart=/usr/sbin/nginx -g daemon on; hoster_process on; (code=exited, status=0/SUCCESS)
Main PID: 32916 (nginx)
Tasks: 5 (limit: 9331)
Memory: 4.5M
CGroup: /system.slice/nginx.service
├─32916 nginx: hoster process /usr/sbin/nginx -g daemon on; hoster_process on;
├─32917 nginx: worker process
├─32918 nginx: worker process
├─32919 nginx: worker process
└─32920 nginx: worker process

Apr 21 17:51:41 hoster-os systemd[1]: Starting A high performance web server and a reverse proxy server...
Apr 21 17:51:41 hoster-os systemd[1]: Started A high performance web server and a reverse proxy server.

I and running sites in browsers I got error :

This site can’t be reachedlocal-laravel9-test-with-nginx.com took too long to respond.
Try:

Checking the connection
Checking the proxy and the firewall
ERR_CONNECTION_TIMED_OUT

In my system I have :

ls /var/run/php/php8.1-fpm.sock
/var/run/php/php8.1-fpm.sock
root@hoster-os:/# ps aux | grep php
root        1443  0.0  0.3 259144 31468 ?        Ss   15:27   0:00 php-fpm: hoster process (/etc/php/8.1/fpm/php-fpm.conf)
www-data    1610  0.0  0.1 259516 13956 ?        S    15:27   0:00 php-fpm: pool www
www-data    1611  0.0  0.1 259516 13956 ?        S    15:27   0:00 php-fpm: pool www
systemd+    4275  0.0  0.1 203936 12032 ?        Ss   15:27   0:00 php -S [::]:8080 -t /var/www/html
hoster      9066  0.0  0.0   4864  1864 ?        S    15:28   0:00 /bin/sh /opt/PhpStorm-2021.1.4/PhpStorm-211.7628.25/bin/phpstorm.sh
hoster      9083 15.3 23.8 6936644 1918060 ?     SLl  15:28  22:16 /opt/PhpStorm-2021.1.4/PhpStorm-211.7628.25/jbr/bin/java -classpath /opt/PhpStorm-2021.1.4/PhpStorm-211.7628.25/lib/bootstrap.jar:/opt/PhpStorm-2021.1.4/PhpStorm-211.7628.25/lib/util.jar:/opt/PhpStorm-2021.1.4/PhpStorm-211.7628.25/lib/jdom.jar:/opt/PhpStorm-2021.1.4/PhpStorm-211.7628.25/lib/log4j.jar:/opt/PhpStorm-2021.1.4/PhpStorm-211.7628.25/lib/jna.jar -Xms128m -Xmx983m -XX:ReservedCodeCacheSize=512m -XX:+UseG1GC -XX:SoftRefLRUPolicyMSPerMB=50 -XX:CICompilerCount=2 -XX:+HeapDumpOnOutOfMemoryError -XX:-OmitStackTraceInFastThrow -ea -Dsun.io.useCanonCaches=false -Djdk.http.auth.tunneling.disabledSchemes="" -Djdk.attach.allowAttachSelf=true -Djdk.module.illegalAccess.silent=true -Dkotlinx.coroutines.debug=off -Dsun.tools.attach.tmp.only=true -XX:ErrorFile=/home/hoster/java_error_in_phpstorm_%p.log -XX:HeapDumpPath=/home/hoster/java_error_in_phpstorm_.hprof -Didea.vendor.name=JetBrains -Didea.paths.selector=PhpStorm2021.1 -Djb.vmOptionsFile=/home/hoster/.config/JetBrains/PhpStorm2021.1/phpstorm64.vmoptions -Didea.platform.prefix=PhpStorm com.intellij.idea.Main
root       33159  0.0  0.0  11860  2744 pts/3    S+   17:54   0:00 grep --color=auto php
root@hoster-os:/# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
root@hoster-os:/# nginx -V
nginx version: nginx/1.18.0 (Ubuntu)
built with OpenSSL 1.1.1f  31 Mar 2020
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-lUTckl/nginx-1.18.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-compat --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_gun



# sudo ufw status verbose
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), deny (routed)
New profiles: skip

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW IN    Anywhere
22/tcp (v6)                ALLOW IN    Anywhere (v6)

root@hoster-os:/# uname -a
Linux hoster-os 5.15.0-53-generic #59~20.04.1-Ubuntu SMP Thu Oct 20 15:10:22 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

I did not find any errors under /var/log/nginx...

What is wrong in my config and how to run my site ?

Thanks in advance!

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.