Difference between revisions of "Nginx"

From Alpine Linux
Jump to: navigation, search
(Nginx with PHP)
(Nginx with PHP5)
Line 135: Line 135:
vi /etc/nginx/nginx.conf}}
vi /etc/nginx/nginx.conf}}
== Nginx with PHP5 ==
== Nginx with PHP ==
[[Nginx_with_PHP#Nginx_with_PHP5|Setting Up Nginx with PHP5]] <br>
[[Nginx_with_PHP#Nginx_with_PHP5|Setting Up Nginx with PHP5]] <br>

Revision as of 13:07, 25 June 2018

Nginx (engine x) is an HTTP and reverse proxy server, a mail proxy server, and a generic TCP/UDP proxy server


Nginx package is available in the Alpine Linux repositories. To install it run:

apk update apk add nginx

Creating new user and group 'www' for nginx

adduser -D -g 'www' www

Create a directory for html files

mkdir /www chown -R www:www /var/lib/nginx chown -R www:www /www


You may want to make backup of original nginx.conf file before writting your own

mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.orig

Configuring Nginx to listen to port 80 and process .html or .htm files

vi /etc/nginx/nginx.conf

user                            www;
worker_processes                auto; # it will be determinate automatically by the number of core

error_log                       /var/log/nginx/error.log warn;
#pid                             /var/run/nginx.pid; # it permit you to use /etc/init.d/nginx reload|restart|stop|start

events {
    worker_connections          1024;

http {
    include                     /etc/nginx/mime.types;
    default_type                application/octet-stream;
    sendfile                    on;
    access_log                  /var/log/nginx/access.log;
    keepalive_timeout           3000;
    server {
        listen                  80;
        root                    /www;
        index                   index.html index.htm;
        server_name             localhost;
        client_max_body_size    32m;
        error_page              500 502 503 504  /50x.html;
        location = /50x.html {
              root              /var/lib/nginx/html;

Sample page

vi /www/index.html

<!DOCTYPE html>
<html lang="en">
    <meta charset="utf-8" />
    Server is online

Controlling nginx

Start Nginx

After the installation Nginx is not running. To start Nginx, use start.

rc-service nginx start

You will get a feedback about the status.

 * Caching service dependencies ...                     [ ok ]
 * /run/nginx: creating directory
 * /run/nginx: correcting owner                         
 * Starting nginx ...                                   [ ok ]

Test configuration

When you've made any changes to your nginx configuration files, you should check it for errors before restarting/reloading nginx.
This will check for any duplicate configuration, syntax errors etc. To do this, run:

nginx -t

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.

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Reload and Restart Nginx

Changes made in the configuration file will not be applied until the command to reload configuration is sent to nginx or it is restarted.
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. If you want to reload the web server, use reload.

rc-service nginx reload

If you want to restart the web server, use restart.

rc-service nginx restart

Stop Nginx

If you want to stop the web server, use stop.

rc-service nginx stop


Normally you want to start the web server when the system is launching. This is done by adding Nginx to the needed runlevel.

rc-update add nginx default

Now Nginx should start automatically when you boot your machine next time. To test that run:


To make sure that Nginx is started run:

ps aux | grep nginx

You should get something like this:

  263 root       0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
  264 www        0:00 nginx: worker process
  310 root       0:00 grep nginx

Testing Nginx

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. You should get:

Server is online


If Nginx is not started check Nginx log file

less /var/log/nginx/error.log

Make sure that configuration file does not contain errors. Edit the file in case there are any errors.

nginx -t vi /etc/nginx/nginx.conf

Nginx with PHP

Setting Up Nginx with PHP5
Setting Up Nginx with PHP7
Setting Up Nginx as Reverse Proxy with acme (Let's Encrypt)