在CentOS系统中配置LNMP(Linux, Nginx, MySQL, PHP-FPM)环境,可以按照以下步骤进行:
首先,确保你已经安装了Nginx。如果没有安装,可以使用以下命令进行安装:
sudo yum install epel-release
sudo yum install nginx
安装完成后,启动Nginx并设置开机自启动:
sudo systemctl start nginx
sudo systemctl enable nginx
接下来,安装MySQL数据库:
sudo yum install mysql-server
启动MySQL并设置开机自启动:
sudo systemctl start mysqld
sudo systemctl enable mysqld
运行安全脚本以设置MySQL的安全选项:
sudo mysql_secure_installation
安装PHP及其相关模块,包括PHP-FPM:
sudo yum install php php-fpm php-mysqlnd
安装完成后,启动PHP-FPM并设置开机自启动:
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
编辑Nginx配置文件,通常位于/etc/nginx/nginx.conf或/etc/nginx/conf.d/default.conf。以下是一个示例配置:
server {
listen 80;
server_name your_domain.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/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
在这个配置中,fastcgi_pass指令指定了PHP-FPM的Unix套接字路径。确保路径正确。
保存配置文件后,重启Nginx以应用更改:
sudo systemctl restart nginx
创建一个简单的PHP文件来测试PHP-FPM是否正常工作。在/var/www/html目录下创建一个名为info.php的文件,并添加以下内容:
<?php
phpinfo();
?>
然后在浏览器中访问http://your_domain.com/info.php,如果看到PHP信息页面,说明PHP-FPM配置成功。
确保防火墙允许HTTP和HTTPS流量:
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
通过以上步骤,你应该能够在CentOS系统上成功配置LNMP环境。如果有任何问题,请检查相关服务的日志文件以获取更多信息。