配置CentOS下的LNMP(Linux, Nginx, MySQL, PHP)服务涉及多个步骤。以下是一个详细的指南,帮助你完成这些配置。
首先,确保你的系统是最新的,并安装必要的软件包:
sudo yum update -y
sudo yum install -y epel-release
sudo yum install -y nginx mysql-server php php-mysqlnd
编辑Nginx的默认配置文件:
sudo vim /etc/nginx/conf.d/default.conf
找到以下部分并进行修改:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name localhost;
root /usr/share/nginx/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
location = /404.html {
internal;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
internal;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
保存并退出编辑器。
启动Nginx并设置为开机自启动:
sudo systemctl start nginx
sudo systemctl enable nginx
安全地配置MySQL:
sudo mysql_secure_installation
按照提示完成设置,包括删除匿名用户、禁止远程root登录、设置密码等。
编辑PHP的默认配置文件:
sudo vim /etc/php.ini
找到以下部分并进行修改:
[mysqli]
mysqli.default_host = localhost
mysqli.default_user = your_mysql_username
mysqli.default_pw = your_mysql_password
mysqli.default_port = 3306
[pdo_mysql]
pdo_mysql.default_host = localhost
pdo_mysql.default_user = your_mysql_username
pdo_mysql.default_pw = your_mysql_password
pdo_mysql.default_port = 3306
保存并退出编辑器。
启动PHP-FPM并设置为开机自启动:
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
编辑Nginx的默认配置文件:
sudo vim /etc/nginx/conf.d/default.conf
找到以下部分并进行修改:
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
保存并退出编辑器。
重启Nginx和PHP-FPM服务以应用更改:
sudo systemctl restart nginx
sudo systemctl restart php-fpm
创建一个简单的PHP文件来测试配置是否正确:
sudo vim /usr/share/nginx/html/info.php
添加以下内容:
<?php
phpinfo();
?>
保存并退出编辑器。
然后在浏览器中访问 http://your_server_ip/info.php
,你应该能看到PHP信息页面,这表明配置成功。
通过以上步骤,你已经成功配置了CentOS下的LNMP服务。你可以根据需要进一步调整和优化这些配置。