CentOS8 搭建 PHP 环境

分类: 系统
日期: 2021-12-22
浏览: 1747

因为个人很喜欢用 oh-my-zsh 所以所有的设置都是在 ~/.zshrc 中,如果不使用 zsh 的话,把 ~/.zshrc 换成 ~/.bashrc 即可!

更换镜像源

更换阿里云镜像
# 备份
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

# 下载新的 CentOS-Base.repo 到 /etc/yum.repos.d/
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo

# 或者
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo

其它镜像:

  1. 中国科技大学 CentOS 镜像
  2. 清华大学 CentOS 镜像
更新缓存
dnf makecache
更新软件
dnf update -y

安装 Nginx

添加 nginx.repo 文件
vim /etc/yum.repos.d/nginx.repo
安装稳定版,添加以下内容
[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
执行安装
dnf install nginx -y
启动 Nginx 并设置开机自启动
systemctl enable nginx --now

安装 PHP

如何安装多版本 PHP 共存的环境请参阅 CentOS8 搭建 PHP 多版本共存的环境

安装epel(阿里云镜像源)
dnf install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm

sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
安装 remi 库(阿里云镜像源)
dnf install -y https://mirrors.aliyun.com/remi/enterprise/remi-release-8.rpm

sed -i 's/https*:\/\/rpms.remirepo.net/https:\/\/mirrors.aliyun.com\/remi/g'  /etc/yum.repos.d/remi*
sed -i 's/#baseurl/baseurl/g' /etc/yum.repos.d/remi*
sed -i 's|^mirrorlist|#mirrorlist|' /etc/yum.repos.d/remi*
安装 utils
dnf install -y dnf-utils
展示可安装的 PHP 版本
dnf module list php
执行安装
# 选择自己要安装的版本
dnf module install -y php:remi-8.1

# 安装PHP扩展
dnf install -y php-{fpm,bcmath,curl,gd,mysqlnd,pdo,opcache,zip,devel,intl,zlib,mbstring,simplexml,json,dom,openssl}
修改 php-fpm 配置文件,将 apache 换成 nginx
vim /etc/php-fpm.d/www.conf
user = nginx
group = nginx
启动 php-fpm 并设置开机自启
systemctl enable php-fpm --now

安装 MySQL

执行安装

dnf install @mysql -y

开启 MySQL 并设置开机启动

systemctl enable mysqld --now

检查 MySQL 运行状态

systemctl status mysqld

设置 MySQL

mysql_secure_installation
  1. 是否配置验证密码组件 - y
  2. 选择密码验证策略等级 - 0
  3. 输入新密码两次 - xxxxxx
  4. 是否继续使用提供的密码 - y
  5. 是否移除匿名用户 - y
  6. 是否不允许root远程登陆 - y
  7. 是否移除test数据库 - y
  8. 是否重新载入权限表 - y
允许远程登录
update user set host='%' where user='root';

flush privileges;
修改配置

utf8mb4 是真正意义上的 utf8,可以存储4字节内容,比如 emoji 表情。虽然在建库建表时可以指定库、表、字段的字符集都是 utf8mb4,但是在导入备份 SQL 文件时还是报如下的错误

Incorrect string value: '\\xF0\\x9F\\x98\\x8A' for column 'xxxxxx'
vim /etc/my.cnf

添加以下内容

[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
[mysqld]
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci

安装 Redis

显示可安装的 Redis 版本
dnf module list redis
执行安装
dnf module install -y redis:remi-6.2
开启 Redis 并设置开机启动
systemctl enable redis --now
安装 phpredis 扩展

从 EPEL 存储库安装 php-pecl-redis 包:

参考文档:https://github.com/phpredis/phpredis/blob/develop/INSTALL.markdown#rhel--centos

dnf install php-pecl-redis -y
安装 swoole 扩展
dnf install php-pecl-swoole -y
查看 PHP 扩展信息
php --ri 扩展名
重启 php-fpm
systemctl restart php-fpm

安装 composer

下载安装文件
php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');"
下载 composer.phar 文件
php composer-setup.php
删除安装文件
php -r "unlink('composer-setup.php');"
全局安装(推荐)
sudo mv composer.phar /usr/local/bin/composer
更换中国镜像
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer

Nginx 站点配置示例

server {
    listen 80;
    server_name localhost;
    root /var/www/html;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass unix:/run/php-fpm/www.sock;
        fastcgi_buffering off;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}
版权声明
作者:不二
来源:不二博客
文章版权归作者所有,未经允许请勿转载。
共有 0 条评论
赶快发表评论吧~
发表评论