在LAMP架构下部署PHP应用通常涉及以下几个步骤:
安装Apache:
sudo apt update
sudo apt install apache2
启用必要的模块:
sudo a2enmod rewrite
sudo systemctl restart apache2
配置虚拟主机:
编辑/etc/apache2/sites-available/yourdomain.com.conf
文件,添加以下内容:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/yourdomain.com/html
<Directory /var/www/yourdomain.com/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
启用站点:
sudo a2ensite yourdomain.com.conf
sudo systemctl restart apache2
安装Nginx:
sudo apt update
sudo apt install nginx
配置服务器块:
编辑/etc/nginx/sites-available/yourdomain.com
文件,添加以下内容:
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
root /var/www/yourdomain.com/html;
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/php7.4-fpm.sock; # 根据你的PHP版本调整
}
location ~ /\.ht {
deny all;
}
}
启用站点:
sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
安装PHP及其扩展:
sudo apt update
sudo apt install php libapache2-mod-php php-mysql # 对于Apache
sudo apt install php-fpm php-mysql # 对于Nginx
配置PHP:
编辑/etc/php/7.4/apache2/php.ini
(对于Apache)或/etc/php/7.4/fpm/php.ini
(对于Nginx),根据需要进行配置。
安装MySQL或MariaDB:
sudo apt update
sudo apt install mysql-server # 或 mariadb-server
安全配置: 运行安全脚本:
sudo mysql_secure_installation
创建数据库和用户: 登录到MySQL/MariaDB并创建数据库和用户:
CREATE DATABASE yourdatabase;
CREATE USER 'youruser'@'localhost' IDENTIFIED BY 'yourpassword';
GRANT ALL PRIVILEGES ON yourdatabase.* TO 'youruser'@'localhost';
FLUSH PRIVILEGES;
上传应用代码:
使用FTP、SCP或其他文件传输方法将你的PHP应用代码上传到服务器上的/var/www/yourdomain.com/html
目录。
设置文件权限:
确保Web服务器用户(通常是www-data
)有权限访问和执行应用文件:
sudo chown -R www-data:www-data /var/www/yourdomain.com/html
sudo find /var/www/yourdomain.com/html -type f -exec chmod 644 {} \;
sudo find /var/www/yourdomain.com/html -type d -exec chmod 755 {} \;
配置环境变量(如果需要): 根据应用需求,设置必要的环境变量,例如数据库连接信息。
访问应用:
打开浏览器,访问http://yourdomain.com
,确保应用正常运行。
检查日志: 查看Apache或Nginx的错误日志,确保没有错误信息:
sudo tail -f /var/log/apache2/error.log # 对于Apache
sudo tail -f /var/log/nginx/error.log # 对于Nginx
通过以上步骤,你应该能够在LAMP架构下成功部署PHP应用。