Nginx Basics

From WikiMLT

Ng­inx CLI

sudo nginx -h
nginx version: nginx/1.18.0 (Ubuntu)
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /usr/share/nginx/)
  -c filename   : set configuration file (default: /etc/nginx/nginx.conf)
  -g directives : set global directives out of configuration file

Con­trol via sys­tem­ctl.

sudo systemctl start nginx.service
sudo systemctl stop nginx.service
sudo systemctl reload nginx.service
sudo systemctl restart nginx.service

En­able or Dis­able a Vir­tu­al Host Con­fig­u­ra­tion

There is not avail­able a na­tive com­mand equiv­a­lent of a2ensite or a2dissite. So we need to cre­ate or re­move the VH rel­e­vant sym­bol­ic links man­u­al­ly.

sudo ln -s /etc/nginx/sites-{available,enabled}/vhost-name.conf
sudo rm /etc/nginx/sites-enabled/vhost-name.conf

Af­ter that test the con­fig­u­ra­tion and re­load (or restart) the ser­vice.

sudo nginx -tq && sudo nginx -s reload

Ex­am­ple Con­fig­u­ra­tion

Here are few ex­am­ple con­fig­u­ra­tions:

PHP En­able

Ng­inx us­es PHP-FPM, so as ref­er­ence how to set­up PHP-FPM see the ar­ti­cle: Set­up PHP on Ubun­tu.

Let's En­crypt

Cur­rent­ly I'm us­ing Let's En­crypt *wild­card cer­tifi­cate, which is not com­pat­i­ble with the Ng­inx or Apache2 plu­g­ins, how­ev­er to gen­er­ate the helpers con­fig­u­ra­tion file /etc/letsencrypt/options-ssl-nginx.conf you can use the com­mand pro­vid­ed be­low, but first you must test your con­fig­u­ra­tion for er­rors, be­cause it there is some the conf-file will not be cre­at­ed.

sudo nginx -t
sudo certbot --nginx -n  # run non-interactively: -n

Ref­er­ences