https://wiki.alpinelinux.org/w/api.php?action=feedcontributions&user=Nino&feedformat=atomAlpine Linux - User contributions [en]2024-03-29T11:46:46ZUser contributionsMediaWiki 1.40.0https://wiki.alpinelinux.org/w/index.php?title=Nginx&diff=15272Nginx2018-06-17T12:39:09Z<p>Nino: /* Controlling nginx */ Testing configuration before starting nginx will fail with `[emerg] open() "/run/nginx/nginx.pid" failed (2: No such file or directory)`</p>
<hr />
<div>[https://nginx.org/en/ Nginx] (engine x) is an HTTP and reverse proxy server, a mail proxy server, and a generic TCP/UDP proxy server<br />
<br />
== Installation ==<br />
Nginx package is available in the Alpine Linux repositories. To install it run:<br />
<br />
{{Cmd|apk update<br />
apk add nginx}}<br />
<br />
Creating new user and group 'www' for nginx<br />
{{Cmd|adduser -D -g 'www' www}}<br />
<br />
Create a directory for html files<br />
{{Cmd|mkdir /www<br />
chown -R www:www /var/lib/nginx<br />
chown -R www:www /www<br />
}}<br />
<br />
== Configuration ==<br />
You may want to make backup of original nginx.conf file before writting your own<br />
{{Cmd|mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.orig}}<br />
<br />
Configuring Nginx to listen to port 80 and process .html or .htm files<br />
{{Cmd|vi /etc/nginx/nginx.conf}}<br />
<pre><br />
user www;<br />
worker_processes auto; # it will be determinate automatically by the number of core<br />
<br />
error_log /var/log/nginx/error.log warn;<br />
#pid /var/run/nginx.pid; # it permit you to use /etc/init.d/nginx reload|restart|stop|start<br />
<br />
events {<br />
worker_connections 1024;<br />
}<br />
<br />
http {<br />
include /etc/nginx/mime.types;<br />
default_type application/octet-stream;<br />
sendfile on;<br />
access_log /var/log/nginx/access.log;<br />
keepalive_timeout 3000;<br />
server {<br />
listen 80;<br />
root /www;<br />
index index.html index.htm;<br />
server_name localhost;<br />
client_max_body_size 32m;<br />
error_page 500 502 503 504 /50x.html;<br />
location = /50x.html {<br />
root /var/lib/nginx/html;<br />
}<br />
}<br />
}<br />
</pre><br />
<br />
== Sample page ==<br />
{{Cmd|vi /www/index.html}}<br />
<pre><br />
<!DOCTYPE html><br />
<html lang="en"><br />
<head><br />
<meta charset="utf-8" /><br />
<title>HTML5</title><br />
</head><br />
<body><br />
Server is online<br />
</body><br />
</html><br />
</pre><br />
<br />
== Controlling nginx ==<br />
<br />
=== Start Nginx ===<br />
After the installation Nginx is not running. To start Nginx, use ''start''.<br />
{{Cmd|rc-service nginx start}}<br />
<br />
You will get a feedback about the status.<br />
<pre><br />
* Caching service dependencies ... [ ok ]<br />
* /run/nginx: creating directory<br />
* /run/nginx: correcting owner <br />
* Starting nginx ... [ ok ]<br />
</pre><br />
<br />
=== Test configuration ===<br />
When you've made any changes to your nginx configuration files, you should check it for errors before restarting/reloading nginx.<br><br />
This will check for any duplicate configuration, syntax errors etc. To do this, run:<br />
{{Cmd|nginx -t}}<br />
<br />
You will get a feedback if it failed or not. If everything is fine, you'll see the following and can then move ahead to reload the nginx server.<br />
<pre><br />
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok<br />
nginx: configuration file /etc/nginx/nginx.conf test is successful<br />
</pre><br />
<br />
=== Reload and Restart Nginx ===<br />
Changes made in the configuration file will not be applied until the command to reload configuration is sent to nginx or it is restarted.<br><br />
Reloading will do a "hot reload" of the configuration without server downtime. It will start the new worker processes with a new configuration and gracefully shutdown the old worker processes. If you have pending requests, then these will be handled by the old worker processes before it dies, so it's an extremely graceful way to reload configs.<br />
If you want to reload the web server, use ''reload''.<br />
{{Cmd|rc-service nginx reload}}<br />
If you want to restart the web server, use ''restart''.<br />
{{Cmd|rc-service nginx restart}}<br />
<br />
=== Stop Nginx ===<br />
If you want to stop the web server, use ''stop''.<br />
{{Cmd|rc-service nginx stop}}<br />
<br />
=== Runlevel ===<br />
Normally you want to start the web server when the system is launching. This is done by adding Nginx to the needed runlevel.<br />
{{Cmd|rc-update add nginx default}}<br />
<br />
Now Nginx should start automatically when you boot your machine next time. To test that run:<br />
{{cmd|reboot}}<br />
<br />
To make sure that Nginx is started run:<br />
{{cmd|<nowiki>ps aux | grep nginx</nowiki>}}<br />
<br />
You should get something like this:<br />
<pre><br />
263 root 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf<br />
264 www 0:00 nginx: worker process<br />
310 root 0:00 grep nginx<br />
</pre><br />
<br />
== Testing Nginx ==<br />
This section is assuming that nginx is running and sample html page "/www/index.html" is created. Launch a web browser and point it to your web server.<br />
You should get:<br />
<pre>Server is online</pre><br />
<br />
== Troubleshooting ==<br />
If Nginx is not started check Nginx log file<br />
{{cmd|less /var/log/nginx/error.log}}<br />
<br />
Make sure that configuration file does not contain errors. Edit the file in case there are any errors.<br />
{{cmd|nginx -t<br />
vi /etc/nginx/nginx.conf}}<br />
<br />
== Nginx with PHP ==<br />
<br />
[[Nginx_with_PHP#Nginx_with_PHP|Setting Up Nginx with PHP]] <br><br />
[[Nginx_as_reverse_proxy_with_acme_(letsencrypt)|Setting Up Nginx as Reverse Proxy with acme (Let's Encrypt)]]<br />
<br />
[[Category:Server]]<br />
[[Category:Web Server]]</div>Nino