Nginx: Difference between revisions

From Alpine Linux
No edit summary
Line 121: Line 121:
== Nginx with PHP ==
== Nginx with PHP ==


[[Nginx_with_PHP|Setting Up Nginx with PHP]]
[[Nginx_with_PHP#Nginx_with_PHP|Setting Up Nginx with PHP]]


[[Category:Server]]
[[Category:Server]]

Revision as of 08:09, 22 November 2016

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

Installation

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

apk add nginx

Creating new user and group 'www' for nginx

adduser -D -u 1000 -g 'www' www

Create a directory for html files

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

Configuration

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                1;

error_log                       /var/log/nginx/error.log warn;
pid                             /var/run/nginx.pid;

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">
<head>
    <meta charset="utf-8" />
    <title>HTML5</title>
</head>
<body>
    Server is online
</body>
</html>

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.

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

Restart Nginx

After changing the configuration file nginx needs to be restarted. 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

Runlevel

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:

reboot

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

Troubleshooting

If Nginx is not started check Nginx log file

less /var/log/nginx/error.log

Make sure that configuration file does not contain errors

vi /etc/nginx/nginx.conf

Nginx with PHP

Setting Up Nginx with PHP