在Linux上部署PHP应用通常涉及以下几个步骤:
首先,确保你的Linux系统已经安装了以下软件:
sudo apt update
sudo apt install apache2 php libapache2-mod-php php-mysql
sudo apt update
sudo apt install nginx php-fpm php-mysql
编辑Apache的默认站点配置文件:
sudo nano /etc/apache2/sites-available/000-default.conf
添加以下内容:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
启用站点并重启Apache:
sudo a2ensite 000-default.conf
sudo systemctl restart apache2
编辑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:/var/run/php/php7.4-fpm.sock; # 根据你的PHP版本调整
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
重启Nginx:
sudo systemctl restart nginx
将你的PHP应用代码上传到服务器的/var/www/html
目录下。你可以使用scp
、rsync
或FTP等方式进行上传。
如果你使用的是MySQL或PostgreSQL,确保数据库服务器已经安装并运行。然后创建一个新的数据库和用户,并授予适当的权限。
CREATE DATABASE mydatabase;
CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword';
GRANT ALL PRIVILEGES ON mydatabase.* TO 'myuser'@'localhost';
FLUSH PRIVILEGES;
CREATE DATABASE mydatabase;
CREATE USER myuser WITH ENCRYPTED PASSWORD 'mypassword';
GRANT ALL PRIVILEGES ON DATABASE mydatabase TO myuser;
根据你的应用需求,配置PHP应用的.env
文件或其他配置文件,确保它们指向正确的数据库和其他服务。
打开浏览器,访问你的服务器IP地址或域名,检查应用是否正常运行。
通过以上步骤,你应该能够在Linux上成功部署一个PHP应用。