LXC/Alpine/Nginx/PHP

From Chorke Wiki
Jump to navigation Jump to search

LXC » Alpine

lxc image ls images:alpine/3.21
lxc launch   images:alpine/3.21 nginx

lxc delete   nginx/alpine:3.21 2>/dev/null
lxc snapshot nginx alpine:3.21
lxc restore  nginx alpine:3.21
lxc info     nginx

LXC » Alpine » Nginx

lxc restore  nginx alpine:3.21
lxc delete   nginx/nginx:1.26.3 2>/dev/null

cat <<'EXE'| lxc exec nginx -- sh
apk update
apk upgrade
apk add nginx

cat <<'CFG'| tee /etc/nginx/http.d/default.conf >/dev/null
server {
  listen 80 default_server;
  listen [::]:80 default_server;
  root /var/www/localhost/htdocs;

  # prevent recursive 404
  location = /404.html {
    internal;
  }
}
CFG

cat <<'HTM'| tee /var/www/localhost/htdocs/index.html >/dev/null
<html>
  <body>
    <h1>It works!</h1>
  </body>
</html>
HTM

rc-update add nginx
rc-service nginx start
EXE

INET_PATH='.[].state.network.[].addresses[]|select(.family=="inet" and .scope=="global").address'
INET_ADDR=$(lxc list name=nginx -f=yaml|yq  -r "${INET_PATH}")
xdg-open http://${INET_ADDR} &>/dev/null &
curl -fsSL ${INET_ADDR}

lxc snapshot nginx nginx:1.26.3
lxc restore  nginx nginx:1.26.3
lxc info     nginx

LXC » Alpine » Nginx » PHP

echo 'apk search php84'| lxc exec nginx -- sh
lxc delete   nginx/php-fpm:8.4.5 2>/dev/null
lxc restore  nginx nginx:1.26.3

cat <<'EXE'| lxc exec nginx -- sh
apk update
apk upgrade
apk add php84 php84-curl
apk add php84-fpm php84-opcache
apk add php84-mbstring php84-xml
apk add php84-mysqli php84-pgsql php84-intl

cat <<'PHP'| tee /var/www/localhost/htdocs/info.php >/dev/null
<?php
  phpinfo();
?>
PHP

rc-update add php-fpm84
rc-service php-fpm84 start

cat <<'CFG'| tee /etc/nginx/http.d/default.conf >/dev/null
server {
  listen 80 default_server;
  listen [::]:80 default_server;
  root  /var/www/localhost/htdocs;
  index index.html index.htm index.php;

  location ~ \.php$ {
    fastcgi_pass      127.0.0.1:9000;
    fastcgi_index     index.php;
    include           fastcgi.conf;
  }

  # prevent recursive 404
  location = /404.html {
    internal;
  }
}
CFG
rc-service nginx restart
EXE

INET_PATH='.[].state.network.[].addresses[]|select(.family=="inet" and .scope=="global").address'
INET_ADDR=$(lxc list name=nginx -f=yaml|yq  -r "${INET_PATH}")
xdg-open http://${INET_ADDR}/info.php &>/dev/null &

echo 'php84 -v'| lxc exec nginx -- sh
INFO_PATH='//div/table[1]/tr/td/h1/text()'
curl -fsSL ${INET_ADDR}/info.php|xmllint --html --xpath "${INFO_PATH}" -

lxc snapshot nginx/php-fpm:8.4.5
lxc restore  nginx/php-fpm:8.4.5
lxc info     nginx

Playground

curl -fsSL ${INET_ADDR}/info.php| xmllint --format -
curl -fsSL ${INET_ADDR}/info.php| xmllint --html --xpath "//tr/td/h1/text()" -
curl -fsSL ${INET_ADDR}/info.php| xmllint --html --xpath "/html/body/div/table[1]/tr/td/h1/text()" -
apk cache clean
ls -ahl /var/cache/apk/
rm -vrf /var/cache/apk/*

References