在Ubuntu上配置LNMP(Linux, Nginx, MySQL, PHP-FPM)环境时,PHP-FPM(FastCGI Process Manager)用于处理PHP脚本。以下是详细的配置步骤:
首先,更新你的包列表并安装Nginx、MySQL和PHP-FPM:
sudo apt update
sudo apt install nginx mysql-server php-fpm
启动并启用MySQL服务:
sudo systemctl start mysql
sudo systemctl enable mysql
运行MySQL安全脚本以提高安全性:
sudo mysql_secure_installation
按照提示设置root密码,删除匿名用户,禁止root远程登录等。
PHP-FPM的默认配置文件通常位于 /etc/php/7.x/fpm/pool.d/www.conf
(具体版本号可能会有所不同)。你可以根据需要进行调整。
编辑PHP-FPM配置文件:
sudo nano /etc/php/7.x/fpm/pool.d/www.conf
找到以下行并进行修改:
;listen = /run/php/php7.x-fpm.sock ; 默认使用Unix socket
listen = /run/php/php7.x-fpm.sock
;listen.owner = www-data
;listen.group = www-data
确保 listen
指向正确的socket文件,并且 listen.owner
和 listen.group
设置为 www-data
。
编辑Nginx的默认站点配置文件:
sudo nano /etc/nginx/sites-available/default
找到以下部分并进行修改:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.x-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
确保 fastcgi_pass
指向正确的PHP-FPM socket文件。
重启Nginx和PHP-FPM服务以应用更改:
sudo systemctl restart nginx
sudo systemctl restart php7.x-fpm
创建一个简单的PHP文件来测试配置是否成功:
sudo nano /var/www/html/index.php
添加以下内容:
<?php
phpinfo();
?>
保存并退出编辑器,然后在浏览器中访问 http://your_server_ip
,你应该能看到PHP信息页面。
如果你启用了防火墙,确保允许HTTP和HTTPS流量:
sudo ufw allow 'Nginx Full'
通过以上步骤,你已经成功在Ubuntu上配置了LNMP环境,并确保PHP-FPM能够正确处理PHP脚本。如果有任何问题,请检查日志文件以获取更多信息:
/var/log/nginx/error.log
/var/log/php7.x-fpm.log
(具体版本号可能会有所不同)希望这些步骤对你有所帮助!