[web-server] Nginx 설치

FreeBSD Ports 를 이용하여 Nginx 를 설치 합니다.

Ports 로 설치 할때는 반드시 Ports Tree 를 업데이트 해야 합니다.

Test Machine 의 경우 Apache24-Mariadb102-php71 이 설치 되어 있는 관계로

Mariadb102 설치와 php71 설치 내용은 별도로 작성 하지 않겠습니다.


Ports 트리 업데이트

root@bsd11:~ # 
root@bsd11:~ # portsnap fetch
root@bsd11:~ # portsnap update
root@bsd11:~ # portsnap fetch update


2017년 11월 기준 ports nginx version 은 1.12.2 Version 입니다.

root@bsd11:/usr/ports # make search name=nginx
Port:   nginx-1.12.2_1,2
Path:   /usr/ports/www/nginx
Info:   Robust and small WWW server
Maint:  joneum@FreeBSD.org
B-deps: pcre-8.40_1
R-deps: pcre-8.40_1
WWW:    http://nginx.org/



root@bsd11:~ # 
root@bsd11:~ # cd /usr/ports/www/nginx && make install clean



OK 를 눌러 설치를 진행 합니다.

설치완료후 메세지

Recent version of the NGINX introduces dynamic modules support.  In
FreeBSD ports tree this feature was enabled by default with the DSO
knob.  Several vendor's and third-party modules have been converted
to dynamic modules.  Unset the DSO knob builds an NGINX without
dynamic modules support.

To load a module at runtime, include the new `load_module'
directive in the main context, specifying the path to the shared
object file for the module, enclosed in quotation marks.  When you
reload the configuration or restart NGINX, the module is loaded in.
It is possible to specify a path relative to the source directory,
or a full path, please see
https://www.nginx.com/blog/dynamic-modules-nginx-1-9-11/ and
http://nginx.org/en/docs/ngx_core_module.html#load_module for

Default path for the NGINX dynamic modules is


      This port has installed the following files which may act as network
      servers and may therefore pose a remote security risk to the system.

      This port has installed the following startup scripts which may cause
      these network services to be started at boot time.

      If there are vulnerabilities in these programs there may be a security
      risk to the system. FreeBSD makes no guarantee about the security of
      ports included in the Ports Collection. Please type 'make deinstall'
      to deinstall the port if this is a concern.

      For more information, and contact details about the security
      status of this software, see the following webpage:
===>  Cleaning for GeoIP-1.6.11
===>  Cleaning for libgd-2.2.4_1,1
===>  Cleaning for tiff-4.0.8
===>  Cleaning for jbigkit-2.1_1
===>  Cleaning for webp-0.6.0_4
===>  Cleaning for giflib-5.1.4
===>  Cleaning for nginx-1.12.2_1,2
root@bsd11:/usr/ports/www/nginx # rehash


/etc/rc.conf 파일 수정

root@bsd11:~ # 
root@bsd11:~ # vi /etc/rc.conf



php-fpm 설정 및  Daemon Start

root@bsd11:~ # 
root@bsd11:~ # cd /usr/local/etc/php-fpm.d/
root@bsd11:/usr/local/etc/php-fpm.d #
root@bsd11:/usr/local/etc/php-fpm.d # vi www.conf
listen.owner = www
listen.group = www
listen.mode = 0660

root@bsd11:~ # service php-fpm start
Performing sanity check on php-fpm configuration:
[19-Nov-2017 20:23:54] NOTICE: configuration file /usr/local/etc/php-fpm.conf test is successful

Starting php_fpm.
root@bsd11:~ #


Daemon 구동 확인

root@bsd11:~ # sockstat -4
www      php-fpm    1179  0  tcp4        *:*
www      php-fpm    1178  0  tcp4        *:*
root     php-fpm    1177  8  tcp4        *:*


GeoIP 설정

root@bsd11:~ # 
root@bsd11:~ # wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
--2017-11-19 22:20:02--  http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
Resolving geolite.maxmind.com (geolite.maxmind.com)...,, 2400:cb00:2048:1::6810:262f, ...
Connecting to geolite.maxmind.com (geolite.maxmind.com)||:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 674238 (658K) [application/octet-stream]
Saving to: 'GeoIP.dat.gz'

GeoIP.dat.gz                                                        100%[==================================================================================================================================================================>] 658.44K  --.-KB/s    in 0.06s

2017-11-19 22:20:02 (11.0 MB/s) - 'GeoIP.dat.gz' saved [674238/674238]

root@bsd11:~ #  wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
--2017-11-19 22:20:22--  http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
Resolving geolite.maxmind.com (geolite.maxmind.com)...,, 2400:cb00:2048:1::6810:252f, ...
Connecting to geolite.maxmind.com (geolite.maxmind.com)||:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 11793186 (11M) [application/octet-stream]
Saving to: 'GeoLiteCity.dat.gz'

GeoLiteCity.dat.gz                                                  100%[==================================================================================================================================================================>]  11.25M  13.2MB/s    in 0.9s

2017-11-19 22:20:24 (13.2 MB/s) - 'GeoLiteCity.dat.gz' saved [11793186/11793186]

root@bsd11:~ #
root@bsd11:~ # gunzip ./GeoLiteCity.dat.gz
root@bsd11:~ # gunzip ./GeoIP.dat.gz
root@bsd11:~ # mv Geo* /usr/local/share/GeoIP/


Nginx 설정 (nginx.conf 파일수정)

root@bsd11:~ #
root@bsd11:~ # cd /usr/local/etc/nginx/
root@bsd11:/usr/local/etc/nginx # vi nginx.conf
root@bsd11:/usr/local/etc/nginx # cp nginx.conf nginx.conf.org
root@bsd11:/usr/local/etc/nginx # vi nginx.conf
user  www www;
worker_processes  4;
error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;
### Load Modules###
load_module "/usr/local/libexec/nginx/ngx_http_geoip_module.so";

events {
    worker_connections  1024;
http {
    geoip_country /usr/local/share/GeoIP/GeoIP.dat;
    geoip_city /usr/local/share/GeoIP/GeoLiteCity.dat;
    include /usr/local/etc/nginx/mime.types;
    default_type application/octet-stream;
    access_log off;
    server_tokens off;
    sendfile on;
    client_max_body_size 200m;
    client_body_buffer_size 1m;
    keepalive_timeout 1;
    port_in_redirect off;
    gzip on;
    gzip_http_version 1.1;
    gzip_vary on;
    gzip_comp_level 6;
    gzip_proxied any;
    gzip_types text/plain text/css application/json application/x-javascript application/xml application/xml+rss text/javascript;
    gzip_buffers 16 8k;
    gzip_disable "MSIE [1-6].(?!.*SV1)";
    include /usr/local/etc/nginx/conf.d/*.conf;

Load Modules 의 경우 geoip 사용을 위하여 load_module 을 추가 하였습니다.


fastcgi_params 수정

root@bsd11:/usr/local/etc/nginx # 
root@bsd11:/usr/local/etc/nginx # vi fastcgi_params
#GeoIp Setting
fastcgi_param GEOIP_ADDR $remote_addr;
fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code;
fastcgi_param GEOIP_COUNTRY_NAME $geoip_country_name;
fastcgi_param GEOIP_REGION $geoip_region;
fastcgi_param GEOIP_REGION_NAME $geoip_region_name;
fastcgi_param GEOIP_CITY $geoip_city;
fastcgi_param GEOIP_AREA_CODE $geoip_area_code;
fastcgi_param GEOIP_LATITUDE $geoip_latitude;
fastcgi_param GEOIP_LONGITUDE $geoip_longitude;
fastcgi_param GEOIP_POSTAL_CODE $geoip_postal_code;
fastcgi_param GEOIP_ORGANIZATION $geoip_org;


conf.d 디렉토리 생성 및 Directory 생성

root@bsd11:~ #
root@bsd11:~ # mkdir /usr/local/etc/nginx/conf.d
root@bsd11:~ #
root@bsd11:~ # mkdir -p /var/www/test.com
root@bsd11:~ # chown www:www /var/www/test.com
root@bsd11:~ # chmod 755 /var/www/test.com


phpinfo 페이지 생성

root@bsd11:~ #
root@bsd11:~ # sh -c 'echo "<?php phpinfo(); ?>" > /var/www/test.com/index.php'


test.com.conf 파일 생성 

root@bsd11:~ # 
root@bsd11:~ # cd /usr/local/etc/nginx/conf.d/
root@bsd11:/usr/local/etc/nginx/conf.d # vi test.com.conf

server {
    listen       80;
    server_name  localhost _;
    index        index.php index.html index.htm;
    root         /var/www/test.com;

    location / {
        try_files   $uri $uri/ /index.php?$query_string;
        autoindex on;

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_index index.php;
        fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code;
        fastcgi_param GEOIP_COUNTRY_NAME $geoip_country_name;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        include fastcgi_params;


Nginx 실행

root@bsd11:~ # 
root@bsd11:~ # service nginx restart
Performing sanity check on nginx configuration:
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
Stopping nginx.
Waiting for PIDS: 4815.
Performing sanity check on nginx configuration:
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
Starting nginx.
root@bsd11:~ # sockstat -4
www      nginx      4865  7  tcp4   *:80                  *:*
www      nginx      4864  7  tcp4   *:80                  *:*
www      nginx      4863  7  tcp4   *:80                  *:*
www      nginx      4862  7  tcp4   *:80                  *:*
root     nginx      4861  7  tcp4   *:80                  *:*


웹브라우저 확인

답글 남기기