一般的なwebサーバはApacheを使いますが、おうちサーバではあえて、nginxを使ってみます。
以下のようにportsを使ってインストールします。
root@cafe:/usr/local/etc # cd /usr/ports/www/nginx
root@cafe:/usr/ports/www/nginx # make install clean
===> License BSD2CLAUSE accepted by the user
===> nginx-1.16.1_4,2 depends on file: /usr/local/sbin/pkg - found
=> nginx-1.16.1.tar.gz doesn't seem to exist in /usr/ports/distfiles/.
=> Attempting to fetch https://nginx.org/download/nginx-1.16.1.tar.gz
nginx-1.16.1.tar.gz 25% of 1008 kB 202 kBps
:
省略
:
cd コマンドで、portsのnginxのディレクトリに移動した後、make コマンドを実行します。
すると、ソースプログラムをダウンロードし、コンパイルを実行します。
コンパイルオプションの選択画面では、初期値のまま「OK」を押しておきます。
依存関係のあるパッケージもportsによって自動的にインストールされます。
そのコンパイルオプションの選択画面が表示されるので、適当なものを選びます。
pcreも初期値のままにしておきます。
:
省略
:
===> Creating groups.
Using existing group 'www'.
===> Creating users
Using existing user 'www'.
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
details.
Default path for the NGINX dynamic modules is
/usr/local/libexec/nginx.
===> SECURITY REPORT:
This port has installed the following files which may act as network
servers and may therefore pose a remote security risk to the system.
/usr/local/sbin/nginx
This port has installed the following startup scripts which may cause
these network services to be started at boot time.
/usr/local/etc/rc.d/nginx
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:
https://nginx.org/
===> Cleaning for pcre-8.43_2
===> Cleaning for nginx-1.16.1_4,2
root@cafe:/usr/ports/www/nginx #
これでnginxのインストールができました。
nginxの設定ファイルは/usr/local/etc/nginxにあります。
root@cafe:/usr/ports/www/nginx # cd /usr/local/etc/nginx/
root@cafe:/usr/local/etc/nginx # ls -l
total 60
-rw-r--r-- 1 root wheel 1007 Sep 20 12:57 fastcgi_params
-rw-r--r-- 1 root wheel 1007 Sep 20 12:57 fastcgi_params-dist
-rw-r--r-- 1 root wheel 2837 Sep 20 12:57 koi-utf
-rw-r--r-- 1 root wheel 2223 Sep 20 12:57 koi-win
-rw-r--r-- 1 root wheel 5231 Sep 20 12:57 mime.types
-rw-r--r-- 1 root wheel 5231 Sep 20 12:57 mime.types-dist
-rw-r--r-- 1 root wheel 2989 Sep 20 12:57 nginx.conf
-rw-r--r-- 1 root wheel 2989 Sep 20 12:57 nginx.conf-dist
-rw-r--r-- 1 root wheel 636 Sep 20 12:57 scgi_params
-rw-r--r-- 1 root wheel 636 Sep 20 12:57 scgi_params-dist
-rw-r--r-- 1 root wheel 664 Sep 20 12:57 uwsgi_params
-rw-r--r-- 1 root wheel 664 Sep 20 12:57 uwsgi_params-dist
-rw-r--r-- 1 root wheel 3610 Sep 20 12:57 win-utf
root@cafe:/usr/local/etc/nginx # diff nginx.conf nginx.conf-dist
root@cafe:/usr/local/etc/nginx # grep -v \# nginx.conf | grep -v ^$
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root /usr/local/www/nginx;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/www/nginx-dist;
}
}
}
root@cafe:/usr/local/etc/nginx #
設定ファイルを確認すると、webデータのルートディレクトリは/usr/local/www/nginxのようですね。
root@cafe:/usr/local/etc/nginx # ls -al /usr/local/www/nginx
lrwxr-xr-x 1 root wheel 25 Sep 20 12:57 /usr/local/www/nginx -> /usr/local/www/nginx-dist
root@cafe:/usr/local/etc/nginx # ls -al /usr/local/www/nginx-dist/
total 16
dr-xr-xr-x 2 root wheel 512 Sep 20 12:57 .
drwxr-xr-x 3 root wheel 512 Sep 20 12:57 ..
-rw-r--r-- 1 root wheel 494 Aug 13 2019 50x.html
-rw-r--r-- 1 root wheel 0 Sep 20 12:57 EXAMPLE_DIRECTORY-DONT_ADD_OR_TOUCH_ANYTHING
-rw-r--r-- 1 root wheel 612 Aug 13 2019 index.html
root@cafe:/usr/local/etc/nginx #
index.htmlも用意されているので、起動してみましょう。
bsdconfig
コマンドで起動して「C Startup」を選択します。
「1 Toggle Startup Services」を選択します。
「nginx_enabe」を選択して「OK」を押し、チェックをつけます。
「X Exit」を選択して、メニューから抜けます。
root@cafe:/usr/local/etc/nginx # cat /etc/rc.conf
hostname="cafe.lo.zeke.ne.jp"
keymap="jp.106"
ifconfig_vtnet0="inet 192.168.1.7 netmask 255.255.255.0"
defaultrouter="192.168.1.254"
ifconfig_vtnet0_ipv6="inet6 2001:2c0:cd03:ca00::cafe/64"
ipv6_defaultrouter="2001:2c0:cd03:ca00::254"
local_unbound_enable="YES"
sshd_enable="YES"
ntpdate_enable="YES"
ntpd_enable="YES"
# Set dumpdev to "AUTO" to enable crash dumps, "NO" to disable
dumpdev="NO"
php_fpm_enable="YES"
nginx_enable="YES"
root@cafe:/usr/local/etc/nginx #
/etc/rc.confに「nginx_enable="YES"」が追加されていることを確認します。
root@cafe:/usr/local/etc/nginx # service nginx status
nginx is not running.
root@cafe:/usr/local/etc/nginx # service nginx start
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@cafe:/usr/local/etc/nginx # service nginx status
nginx is running as pid 2727.
root@cafe:/usr/local/etc/nginx #
service
コマンドでnginxを起動しました。
パソコンのブラウザからサーバにつなげてみて、「Welcome to nginx!」の画面が出ることを確認しました。
nginxでPHPプログラムを実行できるように、設定ファイルを変更します。
root@cafe:/usr/local/etc/nginx # vi nginx.conf
root@cafe:/usr/local/etc/nginx # grep -v # nginx.conf | grep -v ^$
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root /usr/local/www/nginx;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/www/nginx-dist;
}
location ~ \.php$ {
root /usr/local/www/nginx;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
root@cafe:/usr/local/etc/nginx #
「location ~ \.php$」節を追加しました。
root@cafe:/usr/local/etc/nginx # rm /usr/local/www/nginx
root@cafe:/usr/local/etc/nginx # cp -pr /usr/local/www/nginx-dist /usr/local/www/nginx
root@cafe:/usr/local/etc/nginx # echo "<?php phpinfo(); ?>" > /usr/local/www/nginx/info.php
root@cafe:/usr/local/etc/nginx # cat /usr/local/www/nginx/info.php
<?php phpinfo(); ?>
root@cafe:/usr/local/etc/nginx # ls -al /usr/local/www/nginx/
total 20
dr-xr-xr-x 2 root wheel 512 Sep 20 21:16 .
drwxr-xr-x 4 root wheel 512 Sep 20 21:15 ..
-rw-r--r-- 1 root wheel 494 Aug 13 2019 50x.html
-rw-r--r-- 1 root wheel 0 Sep 20 12:57 EXAMPLE_DIRECTORY-DONT_ADD_OR_TOUCH_ANYTHING
-rw-r--r-- 1 root wheel 612 Aug 13 2019 index.html
-rw-r--r-- 1 root wheel 20 Sep 20 21:16 info.php
root@cafe:/usr/local/etc/nginx #
ドキュメントルートが/usr/local/www/nginx-distのリンクになっていたので、リンクを実態に置き換えて、phpプログラムを配置します。
root@cafe:/usr/local/etc/nginx # 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.
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@cafe:/usr/local/etc/nginx #
nginxを再起動します。
ブラウザで、info.phpプログラムを指定して、ちゃんと動作していることを確認しました。
以上でnginxの設定は完了です。