CentOS7环境下PHP7与LNMP架构的配置与优化实践
引言
在当今互联网时代,Web服务器的性能和稳定性对于网站的正常运行至关重要。LNMP(Linux + Nginx + MySQL + PHP)架构作为一种高效、稳定的Web服务器解决方案,被广泛应用于各类网站和服务中。本文将详细介绍在CentOS7环境下如何配置和优化PHP7与LNMP架构,以提升服务器的性能和稳定性。
环境准备
操作系统选择
:
我们选择CentOS7作为服务器操作系统,因为它稳定、安全且社区支持丰富。
硬件要求
:
CPU:至少2核
内存:至少2GB
硬盘:至少20GB
基础软件安装
:
更新系统包:
sudo yum update -y
安装基础开发工具:
sudo yum groupinstall “Development Tools” -y
安装Nginx
添加EPEL仓库
:
sudo yum install epel-release -y
安装Nginx
:
sudo yum install nginx -y
启动并设置Nginx开机自启
:
sudo systemctl start nginx
sudo systemctl enable nginx
配置Nginx
:
编辑配置文件:
sudo vi /etc/nginx/nginx.conf
优化配置示例:
“`nginx
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘
‘$status $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” “$http_x_forwarded_for”‘;
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
“`
安装MySQL
添加MySQL仓库
:
sudo yum install https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm -y
安装MySQL
:
sudo yum install mysql-community-server -y
启动并设置MySQL开机自启
:
sudo systemctl start mysqld
sudo systemctl enable mysqld
安全配置MySQL
:
sudo mysql_secure_installation
优化MySQL配置
:
编辑配置文件:
sudo vi /etc/my.cnf
优化配置示例:
“`ini
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
innodb_buffer_pool_size = 1G
max_connections = 500
“`
安装PHP7
添加Remi仓库
:
sudo yum install https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm -y
sudo yum install epel-release -y
sudo yum install https://rpms.remirepo.net/enterprise/remi-release-7.rpm -y
安装PHP7
:
sudo yum install php php-fpm php-mysql -y
启动并设置PHP-FPM开机自启
:
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
配置PHP-FPM
:
编辑配置文件:
sudo vi /etc/php-fpm.d/www.conf
优化配置示例:
[www]
user = nginx
group = nginx
listen = /var/run/php-fpm/www.sock
listen.owner = nginx
listen.group = nginx
listen.mode = 0660
配置Nginx与PHP-FPM
创建Nginx配置文件
:
创建文件:
sudo vi /etc/nginx/conf.d/php.conf
配置内容示例:
server {
listen 80;
server_name example.com;
root /var/www/html;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php-fpm/www.sock;
}
}
重启Nginx和PHP-FPM
:
sudo systemctl restart nginx
sudo systemctl restart php-fpm
性能优化
Nginx优化
:
开启Gzip压缩:
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
PHP优化
:
开启OPcache:
zend_extension=opcache.so
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
MySQL优化
:
调整缓冲池大小:
innodb_buffer_pool_size = 1G
安全性优化
Nginx安全配置
:
防止点击劫持:
add_header X-Frame-Options “SAMEORIGIN”;
防止跨站脚本攻击:
add_header X-XSS-Protection “1; mode=block”;
PHP安全配置
:
关闭错误显示:
display_errors = Off
MySQL安全配置
:
修改默认端口:
port = 3307
测试与验证
创建测试文件
:
在
/var/www/html
目录下创建
info.php
:
访问测试
:
打开浏览器访问
http://example.com/info.php
,如果看到PHP信息页面,说明配置成功。
总结
通过本文的详细步骤,我们成功在CentOS7环境下配置和优化了PHP7与LNMP架构。这不仅提升了服务器的性能和稳定性,还增强了安全性。希望这篇文章能为你在实际项目中搭建高效稳定的Web服务器提供参考和帮助。
参考文献
Nginx官方文档:nginx.org
MySQL官方文档:dev.mysql.com
PHP官方文档:php.net
通过不断的学习和实践,我们可以在Web服务器配置和优化方面取得更大的进步。祝你在LNMP架构的应用中取得成功!