完全卸载apache2
1.
sudo apt update
2.
sudo apt upgrade
3.
sudo apt remove apache2*
4.
sudo apt autoremove
安装Nginx
1.
sudo apt install nginx
2.
sudo service nginx start
测试Nginx,首先使用如下命令获取本机IP地址
ifconfig
使用浏览器访问本地IP。
返回如下页面,则安装Nginx成功。
安装MySQL
sudo apt install mysql-server
安装过程中需要输入两次密码。
安装MySQL安全组件
sudo mysql_secure_installation
安装过程中首先需要输入MYSQL的root密码.
当询问“Change the root password?”时,输入“N”
剩下的问题可以敲击回车键,选择默认选项。
安装PHP
sudo apt install php php-fpm php7.0-mysql
为使Nginx支持PHP,需要修改Nginx的配置文件,首先备份原始配置文件。
sudo mv /etc/nginx/sites-available/default /etc/nginx/sites-available/default.old
编辑/etc/nginx/sites-available文件,作为Nginx的配置文件。
sudo vim /etc/nginx/sites-available/default
向/etc/nginx/sites-available
输入以下内容
server {
listen 80;
server_name your_site_name.com;
root /usr/share/nginx/html;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
修改完Nginx配置,需要重启Nginx
sudo service nginx restart
在/usr/share/nginx/html/目录创建info.php文件,测试PHP
sudo nano /usr/share/nginx/html/info.php
输入以下内容
<?php
phpinfo();
?>
在浏览器打开http://本地IP/info.php
例如http://10.102.7.193/info.php.
返回如下内容,则Nginx能够支持PHP
最后,可以选择删除创建的info.php文件,避免被攻击者利用。
配置自己多域名、多项目
在 /etc/nginx/conf.d/
创建 .conf
文件 在这里配置自己的域名
server {
listen 80;
server_name you_project_name;
root /Data/; #项目的路径
index index.php index.html;
location / {
# try_files $uri $uri/ =404; #将这个注释掉
try_files $uri $uri/ /index.php?$query_string; #添加上这一条
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}