在CentOS上配置LNMP(Linux, Nginx, MySQL, PHP)多站點的方法如下:
首先,確保你的系統已經安裝了Nginx、MySQL和PHP。你可以使用以下命令來安裝這些軟件包:
sudo yum install epel-release
sudo yum install nginx mysql-server php-fpm php-mysqlnd
啟動Nginx和MySQL服務,并設置它們在系統啟動時自動運行:
sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl start mysqld
sudo systemctl enable mysqld
登錄到MySQL并創建數據庫和用戶:
sudo mysql -u root -p
在MySQL shell中執行以下命令:
CREATE DATABASE site1;
CREATE USER 'site1user'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON site1.* TO 'site1user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
重復上述步驟為第二個站點創建數據庫和用戶。
為每個站點創建一個Nginx配置文件。假設你的站點域名分別是site1.example.com
和site2.example.com
。
/etc/nginx/conf.d/site1.conf
)server {
listen 80;
server_name site1.example.com;
root /var/www/site1;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php-fpm/site1.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
/etc/nginx/conf.d/site2.conf
)server {
listen 80;
server_name site2.example.com;
root /var/www/site2;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php-fpm/site2.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
創建每個站點的根目錄并設置適當的權限:
sudo mkdir -p /var/www/site1
sudo mkdir -p /var/www/site2
sudo chown -R nginx:nginx /var/www/site1
sudo chown -R nginx:nginx /var/www/site2
在每個站點的根目錄下創建一個簡單的PHP文件來測試配置:
/var/www/site1/index.php
)<?php
phpinfo();
?>
/var/www/site2/index.php
)<?php
phpinfo();
?>
重啟Nginx以應用新的配置:
sudo systemctl restart nginx
確保你的域名解析正確,指向你的服務器IP地址。你可以編輯DNS記錄或使用hosts文件進行本地測試。
打開瀏覽器并訪問http://site1.example.com
和http://site2.example.com
,你應該能看到各自的PHP信息頁面。
通過以上步驟,你就可以在CentOS上成功配置LNMP多站點。