centos

centos php配置安全设置

小樊
34
2025-05-02 18:54:20
栏目: 编程语言

在CentOS系统中配置PHP的安全设置,可以遵循以下步骤:

1. 更新系统和PHP

首先,确保你的系统和PHP都是最新的。

sudo yum update -y
sudo yum install epel-release -y
sudo yum install php php-cli php-fpm php-mysqlnd php-gd php-mbstring php-xml php-pear php-bcmath -y

2. 配置PHP-FPM

编辑PHP-FPM的配置文件 /etc/php-fpm.d/www.conf/etc/php-fpm.conf,根据需要进行调整。

; 设置监听端口
listen = /run/php-fpm/www.sock

; 设置用户和组
user = apache
group = apache

; 设置最大连接数
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35

; 设置请求超时时间
request_terminate_timeout = 60s

; 禁用危险函数
disable_functions = eval, assert, system, shell_exec, passthru, exec, popen, curl_exec, curl_multi_exec, parse_ini_file, show_source

3. 配置PHP安全设置

编辑PHP的主配置文件 /etc/php.ini/etc/php.d/ 目录下的相关文件。

3.1 设置错误报告

display_errors = Off
log_errors = On
error_log = /var/log/php_errors.log

3.2 设置文件上传限制

file_uploads = On
upload_max_filesize = 10M
post_max_size = 10M

3.3 设置会话安全

session.cookie_httponly = On
session.cookie_secure = On
session.cookie_samesite = Strict

3.4 设置密码策略

password_hash_default = bcrypt

3.5 禁用危险函数

disable_functions = eval, assert, system, shell_exec, passthru, exec, popen, curl_exec, curl_multi_exec, parse_ini_file, show_source

4. 配置Web服务器

如果你使用的是Apache或Nginx,还需要配置相应的Web服务器。

4.1 Apache

编辑 /etc/httpd/conf/httpd.conf/etc/httpd/conf.d/ 目录下的相关文件。

<Directory "/var/www/html">
    Options -Indexes +FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

4.2 Nginx

编辑 /etc/nginx/nginx.conf/etc/nginx/conf.d/ 目录下的相关文件。

server {
    listen 80;
    server_name example.com;

    root /var/www/html;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php-fpm/www.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

5. 重启服务

最后,重启PHP-FPM和Web服务器以应用更改。

sudo systemctl restart php-fpm
sudo systemctl restart httpd  # 如果使用Apache
# 或者
sudo systemctl restart nginx  # 如果使用Nginx

通过以上步骤,你可以大大提高CentOS系统中PHP的安全性。记得定期检查和更新你的系统和软件,以确保安全漏洞得到及时修复。

0
看了该问题的人还看了