一. 添加常用软件库
- 
添加 Remi 库
Remi 主打 php 及相关扩展, 所以安装 php 这个库是不二选择.
首先进入 Remi 网站, 在 Maintained Enterprise Linux (RHEL / CentOS / Other clones) 列表中找到 Enterprise Linux 8 项后面的 remi-release-8.rpm , 执行下面的命令安装.
| 1 | dnf install http://rpms.remirepo.net/enterprise/remi-release-8.rpm | 
然后启用库
| 1 | dnf config-manager --set-enabled remi | 
- 
添加 Nginx 库
进入 Nginx 官网 , 点右侧的 download 链接, 拉到最下面点击 Pre-Built Packages 项. 点 RHEL/CentOS 跳到相应位置
新增 nginx.repo 配置
| 1 | nano /etc/yum.repos.d/nginx.repo | 
复制上一步中看到的配置内容到配置文件中
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true [nginx-mainline] name=nginx mainline repo baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/ gpgcheck=1 enabled=0 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true | 
保存退出
默认配置只启用了稳定版本,我们要启用主线版本。
| 1 | dnf config-manager --set-enabled nginx-mainline | 
- 
添加 MariaDB 库
MariaDB 是 MySql 分支出来的项目, 因为 MySql 被 Oracle 收购且闭源, 所以有了 MariaDB, 并逐步添加新的功能. 最新版 10.x 已不再从 MySql 合并代码.
打开链接 https://mariadb.com/kb/en/yum/, 找到安装脚本并在服务器上执行。
| 1 | curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash | 
当有大版本发布时,应当重新执行此脚本以更新存储库。例如 从10.5 版本升级到10.6 版本时,需要重新执行脚本。
二. 安装并配置服务
- 
安装 nginx
| 1 2 3 | dnf install nginx systemctl enable nginx systemctl restart nginx | 
- 
安装 php
| 1 2 3 4 5 6 | #安装php和常用扩展 dnf install php php-fpm php-mysql php-pdo php-gd php-intl php-bcmath php-cli php-mbstring php-mcrypt php-pecl-zip php-opcache #启用 php-fpm systemctl enable php-fpm systemctl restart php-fpm | 
继续安装 ImageMagick 和 php Imagick 扩展, ImageMagick 是一个第三方的图形组件,Imagick 是用于调用 ImageMagick 的 php 扩展。
ImageMagick 位于 EPEL 库中,首先需要启用相应的库。
| 1 2 3 4 5 | #启用 PowerTools 库, 这是 CentOS 8 自带的库 dnf config-manager --set-enabled powertools #安装 ImageMagick  dnf install ImageMagick ImageMagick-devel | 
PHP Imagick 是原生扩展,需能过 pecl 安装。首先安装相应的管理工具
| 1 2 3 4 5 | #安装 pecl dnf install php-devel php-pear #安装 扩展 pecl install imagick | 
回车后会提示 Please provide the prefix of ImageMagick installation [autodetect] : , 直接回车自动检测即可。
将扩展添加到 php.ini 配置中,使用如下命令新增一个配置文件。
| 1 | echo "extension=imagick.so" > /etc/php.d/20-imagick.ini | 
重启 php-fpm 服务
| 1 | systemctl restart php-fpm | 
- 配置 nginx 与 php-fpm
| 1 | nano /etc/nginx/nginx.conf | 
启用 gzip, 将下面一段加入到 http {} 节中。
| 1 2 3 4 5 6 7 8 9 10 11 12 |     #启用 gzip 压缩     gzip on;     gzip_vary on;     gzip_http_version 1.1;     gzip_min_length 1k;     gzip_buffers 4 8k;     gzip_comp_level 2;     gzip_types text/plain text/css                application/x-javascript text/xml                application/xml application/xml+rss                text/javascript application/javascript                image/svg+xml; | 
编辑默认主页配置
| 1 | nano /etc/nginx/conf.d/default.conf | 
修改为如下内容
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | server {     listen 80 default;     server_name localhost;     charset utf-8;     root /usr/share/nginx/html;     location / {         index  index.php index.html index.htm;     }     error_page  500 502 503 504  /50x.html;     location = /50x.html {         root   /usr/share/nginx/html;     }     location ~ \.php$ {         fastcgi_pass   php-fpm;         fastcgi_index  index.php;         fastcgi_param  SCRIPT_FILENAME  $request_filename;         include        fastcgi_params;     }     location ~ /\.ht {         deny  all;     }     location ~ \.(gif|jpg|jpeg|png|bmp|swf)$     {         expires 10d;     }     location ~ \.(js|css)?$     {         expires 1d;     } } | 
修改 php.ini, 增加上传文件大小限制, post请求最大限制。
| 1 | nano /etc/php.ini | 
找到下面两项并修改值。
| 1 2 | post_max_size = 20M upload_max_filesize = 20M | 
修改 php-fpm 配置
| 1 | nano /etc/php-fpm.d/www.conf | 
找到下面的内容,并修改相应值。
| 1 2 3 4 5 6 7 8 9 | #此处修改为 nginx 的用户名和组,这样可以避免 php 应用执行时需要权限的问题。 user = nginx group = nginx pm.max_children = 20 pm.start_servers = 5 pm.min_spare_servers = 5 pm.max_spare_servers = 20 pm.max_requests = 120 | 
设置相关目录权限
| 1 2 3 4 5 | #由于改动了 php-fpm 的默认用户和分组,其用到的相关目录权限也要做改动. chown -R nginx:nginx /var/lib/php #nginx 的站点目录权限也重新设置一遍 chown -R nginx:nginx /usr/share/nginx/html | 
重启 nginx 与 php-fpm 服务
| 1 2 | systemctl restart nginx systemctl restart php-fpm | 
- 
安装 MariaDB 数据库
| 1 2 3 4 | dnf install mariadb mariadb-server systemctl enable mariadb systemctl restart mariadb | 
初始化数据库
| 1 2 3 4 5 6 7 8 9 10 11 12 | #执行客户端,默认没有密码 mysql 或 mariadb #切换当前选择的数据库 use mysql; #查询当前所有用户 select host,user,password from user; #增加新用户 grant <select,insert,update,delete|all> on *.* to user1@'%' identified by "password1"; flush privileges; | 
优化数据库配置
根据内存大小选择示例配置文件(位于 /usr/share/mysql/ 中, 以 my-xxxx.cnf 命名), 复制到 /etc/my.cnf.d/ 中, 然后高速参数配置. 我的服务器内存为 512M, 因此选择 my-large.cnf.
| 1 2 | cp /usr/share/mysql/my-large.cnf /etc/my.cnf.d/myserver.cnf nano /etc/my.cnf.d/myserver.cnf | 
在 mysqld 节输入以下内容
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | [mysqld] default-storage-engine = innodb character-set-server=utf8 port = 3306 socket = /var/lib/mysql/mysql.sock skip-external-locking key_buffer_size = 16M max_allowed_packet = 4M table_open_cache = 512 sort_buffer_size = 2M read_buffer_size = 2M read_rnd_buffer_size = 4M myisam_sort_buffer_size = 32M thread_cache_size = 8 query_cache_size= 16M innodb_data_home_dir = /var/lib/mysql innodb_data_file_path = ibdata1:10M:autoextend innodb_log_group_home_dir = /var/lib/mysql innodb_buffer_pool_size = 32M innodb_log_file_size = 20M innodb_log_buffer_size = 4M innodb_flush_log_at_trx_commit = 2 innodb_lock_wait_timeout = 50 | 
重启数据库
| 1 | systemctl restart mariadb | 
