Alpine3.24 手动部署LNMP环境

安装依赖:

apk update && apk upgrade
apk add --no-cache \
  build-base linux-headers perl autoconf automake libtool \
  pcre-dev zlib-dev git wget curl\
  php83 php83-fpm php83-mysqli php83-pdo_mysql php83-mbstring php83-json \
  mariadb mariadb-client vim bash bash-completion

切换到 Bash 终端

bash

校准时间:

ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && date -R

下载geoip2数据库

git clone https://github.com/leev/ngx_http_geoip2_module.git
wget -nc https://github.com/maxmind/libmaxminddb/releases/download/1.13.3/libmaxminddb-1.13.3.tar.gz && tar -zxvf libmaxminddb-1.13.3.tar.gz && cd libmaxminddb-1.13.3
./configure && make -j$(nproc) && make install && ldconfig

下载/安装/解压 openssl

cd && wget -nc https://github.com/openssl/openssl/releases/download/openssl-4.0.1/openssl-4.0.1.tar.gz && tar -zxvf openssl-4.0.1.tar.gz

下载/解压 nginx:

wget -nc https://nginx.org/download/nginx-1.30.3.tar.gz && tar -zxvf nginx-1.30.3.tar.gz && cd nginx-1.30.3

添加编译插件:

./configure \
  --prefix=/usr/local/nginx \
  --conf-path=/etc/nginx/nginx.conf \
  --pid-path=/var/run/nginx.pid \
  --lock-path=/var/run/nginx.lock \
  --error-log-path=/var/log/nginx/error.log \
  --http-log-path=/var/log/nginx/access.log \
  --with-threads \
  --with-file-aio \
  --with-http_ssl_module \
  --with-http_v2_module \
  --with-http_v3_module \
  --with-http_realip_module \
  --with-http_sub_module \
  --with-http_gzip_static_module \
  --with-http_auth_request_module \
  --with-http_secure_link_module \
  --with-http_stub_status_module \
  --with-stream \
  --with-stream_ssl_module \
  --with-stream_realip_module \
  --with-stream_ssl_preread_module \
  --with-compat \
  --add-dynamic-module=../ngx_http_geoip2_module \
  --with-openssl-opt="no-tests" \
  --with-cc-opt='-march=native -O3' \
  --with-openssl=../openssl-4.0.1

编译/安装 nginx

make -j$(nproc) && make install

安装wordpress

cd && wget -P /usr/local/nginx/html/ https://cn.wordpress.org/latest-zh_CN.zip && unzip /usr/local/nginx/html/latest-zh_CN.zip -d /usr/local/nginx/html/ && mv /usr/local/nginx/html/wordpress/* /usr/local/nginx/html && rm -rf /usr/local/nginx/html/latest-zh_CN.zip /usr/local/nginx/html/wordpress
#删除已使用文件
rm -rf libmaxminddb-1.13.3 nginx-1.30.3 nginx-1.30.3.tar.gz ngx_http_geoip2_module openssl-4.0.1

创建目录和设置权限.

# 创建目录和设置权限.
mkdir -p /etc/nginx/ssl /etc/nginx/GeoLite2 /var/log/nginx /etc/init.d /run/php-fpm83
touch /var/log/nginx/access.log /var/log/nginx/error.log
chown -R nobody:nobody /usr/local/nginx/html /var/log/nginx
sed -i 's|#pid.*logs/nginx.pid;|pid /var/run/nginx.pid;|g' /etc/nginx/nginx.conf
sed -i 's|#user  nobody;|user  nobody;|g' /etc/nginx/nginx.conf
sed -i '/user  nobody;/a \
load_module /usr/local/nginx/modules/ngx_http_geoip2_module.so;\
load_module /usr/local/nginx/modules/ngx_stream_geoip2_module.so;' /etc/nginx/nginx.conf
sed -i 's|worker_processes\s*1;|worker_processes  auto;|g' /etc/nginx/nginx.conf

配置nginx服务:

cat > /etc/init.d/nginx << 'EOF'
#!/sbin/openrc-run

description="Nginx HTTP Server"
cfgfile="${cfgfile:-/etc/nginx/nginx.conf}"
command="/usr/local/nginx/sbin/nginx"
command_args="-c $cfgfile"
pidfile="/var/run/nginx.pid"

depend() {
    need net
    use dns logger
}

extra_started_commands="reload"

reload() {
    ebegin "Reloading $description configuration"
    $command -s reload
    eend $?
}
EOF

添加权限

chmod +x /etc/init.d/nginx

下载GeoLite2-Country数据

wget -O /etc/nginx/GeoLite2/GeoLite2-Country.mmdb \
  https://raw.githubusercontent.com/P3TERX/GeoLite.mmdb/download/GeoLite2-Country.mmdb

安装nginx配置文件

vim /etc/nginx/nginx.conf

http {
添加

include conf.d/default.conf;
	# GeoIP2 国家识别
	map $http_x_forwarded_for $clientRealIp {
		"" $remote_addr;
		~^(?P<firstAddr>[0-9\.]+),?.*$ $firstAddr;
	}
	geoip2 /etc/nginx/GeoLite2/GeoLite2-Country.mmdb {
		auto_reload 5m;
		$geoip2_data_country_code default=XX source=$clientRealIp country iso_code;
	}
	map $geoip2_data_country_code $allowed_country {
		CN      yes;
		JP      yes;
		HK      yes;
		US      yes;
		CA      yes;
		FR      yes;
		default no;
	}

server {下的

location / {添加

	if ($allowed_country = no) {
	    return 404;
	}

添加dhparam

curl https://ssl-config.mozilla.org/ffdhe2048.txt > /etc/nginx/ssl/dhparam

nginx配置生成地址

修改 PHP-FPM 配置文件

sed -i 's|127.0.0.1:9000|/run/php-fpm83/www.sock|' /etc/php83/php-fpm.d/www.conf
sed -i 's|;listen.owner = nobody|listen.owner = nobody|' /etc/php83/php-fpm.d/www.conf
sed -i 's|;listen.group = nobody|listen.group = nobody|' /etc/php83/php-fpm.d/www.conf
sed -i 's|;listen.mode = 0660|listen.mode = 0660|' /etc/php83/php-fpm.d/www.conf

初始化数据库 MariaDB

mysql_install_db --user=mysql --datadir=/var/lib/mysql

启动 PHP-FPM 8.3 服务

rc-service php-fpm83 start

开启状态:

rc-service php-fpm83 status 

重新启动:

rc-service php-fpm83 restart

设置开机自动启动

rc-update add php-fpm83 default

启动 mariadb 服务

rc-service mariadb start

开启状态:

rc-service mariadb status

重新启动:

rc-service mariadb restart

设置开机自动启动

rc-update add mariadb default

mysql或MariaDB安全加固:
会设置密码,界面1直接回车,界面2选N,剩的都选Y

mysql_secure_installation

root登录:

mysql -u root -p

查看当前的数据库:

show databases;

创建数据库:

CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

创建用户

CREATE USER 'wp_user'@'localhost' IDENTIFIED BY '你的安全密码';

授予权限:

GRANT ALL PRIVILEGES ON wordpress.* TO 'wp_user'@'localhost';

修改root登录密码无效

ALTER USER 'root'@'localhost' IDENTIFIED VIA mysql_native_password USING PASSWORD("password");

最后务必刷新权限

FLUSH PRIVILEGES;

退出:

exit

安装新内核后开启BBR:

echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf && echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf && sysctl -p && lsmod | grep bbr

启动 nginx 服务

rc-service nginx start

开启状态:

rc-service nginx status

重新启动:

rc-service nginx restart

设置开机自动启动

rc-update add nginx default

修改

        location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm83/www.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
root /usr/local/nginx/html;
index index.php index.html index.ht
m;

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注