wj
发布于 2024-02-24 / 55 阅读
0

debian12搭建nginx+MariDB+PHP8.3

真是好久没搭建网站环境了,目前主流的架构也不知道用什么。也曾经想再写些blog但是都没有坚持下来。经过几年的沉淀 希望这次能坚持下去吧!

之前一直用centos直到近期才知道centos也被取而代之了,所以这次用了debian12,还好一直在用Ubuntu 开始搭建:

apt install -y curl gnupg2 ca-certificates lsb-release debian-archive-keyring
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
| sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
gpg --dry-run --quiet --no-keyring --import --import-options import-show /usr/share/keyrings/nginx-archive-keyring.gpg

添加 Stable version仓库

echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/debian `lsb_release -cs` nginx" \
    | sudo tee /etc/apt/sources.list.d/nginx.list
apt update
apt install nginx -y
systemctl start nginx
# 定义 nginx 运行用户
user www-data;
# pid 文件路径
pid /var/run/nginx.pid;
# 运行进程数(建议设置为等于 CPU 线程数)
worker_processes auto;
# 最大打开文件数
worker_rlimit_nofile 65535;

# 加载启用的模块
include /etc/nginx/modules-enabled/*.conf;

events {
    # Nginx 事件处理模型
    use epoll;
    # 同时接收多个新连接
    multi_accept on;
    # 单个进程允许的客户端最大连接数
    worker_connections 65535;
}

http {
    # 默认编码
    charset utf-8;
    # 开启文件的高效传输模式
    sendfile on;
    # 激活 TCP_CORK socket(阻塞住此头部数据,与之后的 sendfile 数据一同发送,优化吞吐性能)
    tcp_nopush on;
    # 小的数据包不等待直接传输
    tcp_nodelay on;
    # 保持链接超时设定(实现服务器与客户端之间的长连接,减少系统对TCP连接的建立和销毁的开销)
    keepalive_timeout 65;
    # 隐藏 Nginx 版本号
    server_tokens off;
    # 设定文件不存在的错误是否写入日志
    log_not_found off;
    # 哈希的最大值,影响散列表的冲突率,值越大消耗内存越多,但散列 key 的冲突率会降低。
    types_hash_max_size 2048;
    # 设置每个散列桶占用的内存大小
    types_hash_bucket_size 64;
    # 限制上传文件的大小
    client_max_body_size 128M;

    # MIME 媒体类型配置
    include mime.types;
    default_type application/octet-stream;

    # 日志配置
    # 全局访问日志文件
    access_log /var/log/nginx/access.log;
    # 全局错误日志文件和记录级别设定
    error_log /var/log/nginx/error.log warn;

    # 加载其他配置文件
    include /etc/nginx/conf.d/*.conf;

    # 加载启用的站点配置文件
    include /etc/nginx/vhosts/*.com;
}

安装 MariaDB

apt install -y mariadb-server

运行安全设置向导

mysql_secure_installation
Enter current password for root (enter for none):
输入 mysql 的 root 密码,默认没有,回车确认。

Switch to unix_socket authentication [Y/n]
切换到 unix 套接字身份验证?选择:否

Change the root password? [Y/n]
设置 root 密码?选择:是(自行设置密码)

Remove anonymous users? [Y/n]
删除匿名(空用户)用户?选择:是

Disallow root login remotely? [Y/n]
不允许远程root登录?选择:否

Remove test database and access to it? [Y/n]
删除 test 数据库?选择:是

Reload privilege tables now? [Y/n]
是否重新加载权限表使之生效?选择:是

安装 PHP 及相关扩展模块

curl -sSL https://packages.sury.org/php/README.txt | sudo bash -x
apt update
apt dist-upgrade -y
apt install -y php8.3-{fpm,cli,common,mysql,curl,dom,exif,fileinfo,imagick,gd,mbstring,xml,zip,memcached,opcache,bcmath,iconv,intl,simplexml,xmlreader}

对php进行一些设置,可以根据自己的实际环境进行设置

sed -i 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/' /etc/php/8.3/fpm/php.ini
sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 128M/' /etc/php/8.3fpm/php.ini
sed -i 's/post_max_size = 8M/post_max_size = 128M/' /etc/php/8.3/fpm/php.ini
systemctl restart php8.3-fpm

MariaDB 数据库服务器性能优化

【512MB 内存方案】

key_buffer_size = 32M

query_cache_size = 4M

tmp_table_size = 4M

innodb_buffer_pool_size = 8M

innodb_log_buffer_size = 4M

sort_buffer_size = 128K

read_buffer_size = 64K

read_rnd_buffer_size = 128K

join_buffer_size = 128K

thread_stack = 128K

thread_cache_size = 16

binlog_cache_size = 128K

table_open_cache = 32

max_connections = 384

【1GB 内存方案】

key_buffer_size = 64M

query_cache_size = 8M

tmp_table_size = 8M

innodb_buffer_pool_size = 16M

innodb_log_buffer_size = 8M

sort_buffer_size = 256K

read_buffer_size = 128K

read_rnd_buffer_size = 256K

join_buffer_size = 256K

thread_stack = 256K

thread_cache_size = 32

binlog_cache_size = 256K

table_open_cache = 64

max_connections = 512

【2GB 内存方案】

key_buffer_size = 128M

query_cache_size = 16M

tmp_table_size = 16M

innodb_buffer_pool_size = 32M

innodb_log_buffer_size = 16M

sort_buffer_size = 512K

read_buffer_size = 256K

read_rnd_buffer_size = 512K

join_buffer_size = 512K

thread_stack = 512K

thread_cache_size = 64

binlog_cache_size = 512K

table_open_cache = 128

max_connections = 512

【4GB 内存方案】

key_buffer_size = 256M

query_cache_size = 32M

tmp_table_size = 32M

innodb_buffer_pool_size = 64M

innodb_log_buffer_size = 32M

sort_buffer_size = 1024K

read_buffer_size = 512K

read_rnd_buffer_size = 1024K

join_buffer_size = 1024K

thread_stack = 1024K

thread_cache_size = 128

binlog_cache_size = 1024K

table_open_cache = 256

max_connections = 512

systemctl restart mariadb.service

设置 PHP-FPM 的进程池为动态模式

vi /etc/php/8.3/fpm/pool.d/www.conf

pm = dynamic

pm.max_children = 32

pm.start_servers = 16

pm.min_spare_servers = 16

systemctl restart php8.3-fpm

安装 Memcached

apt install -y memcached libmemcached-tools
sed -i 's/-m 64/-m 256/' /etc/memcached.conf
可以根据自己的硬件适当调整
vi /etc/php/8.3/fpm/php.ini

session.save_handler = memcached

session.save_path = "127.0.0.1:11211"