centos7实现mysql+PHP+nginx互相连接
时间: 2023-10-07 13:07:02 浏览: 133
要实现 MySQL + PHP + Nginx 的互相连接,您需要按照以下步骤进行配置:
1. 安装 MySQL
```bash
sudo yum install mysql-server
sudo systemctl start mysqld
sudo systemctl enable mysqld
```
2. 配置 MySQL
```bash
sudo mysql_secure_installation
```
3. 安装 PHP
```bash
sudo yum install php php-mysql
```
4. 安装 Nginx
```bash
sudo yum install nginx
sudo systemctl start nginx
sudo systemctl enable nginx
```
5. 配置 Nginx
在 Nginx 的配置文件 `/etc/nginx/nginx.conf` 中添加以下内容:
```conf
server {
listen 80;
server_name yourdomain.com;
root /var/www/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
```
6. 配置 PHP
编辑 `/etc/php.ini` 文件,修改以下参数:
```ini
cgi.fix_pathinfo=0
```
7. 重启 Nginx 和 PHP-FPM
```bash
sudo systemctl restart nginx
sudo systemctl restart php-fpm
```
现在,您可以在 `/var/www/html` 目录下创建一个 PHP 文件来测试是否可以连接 MySQL 数据库了。
阅读全文