Debian 9 安装Linux, NGINX, MySQL/MariaDB, PHP7.0

星期二博客整理

NGINX安装

安装之前先更新服务器,然后通过apt安装

sudo apt update
sudo apt install nginx

启动nginx

sudo systemctl start nginx

设置nginx开机启动

sudo systemctl enable nginx

MariaDB

安装MariaDB server 和 MySQL/MariaDB 相关PHP支持组件.

sudo apt install mariadb-server php7.0-mysql

启动MariaDB,设置MariaDB开机自动启动

sudo systemctl start mariadb
sudo systemctl enable mariadb

MariaDB默认是不设置root账号密码,可通过如下命令设置

sudo mysql_secure_installation

系统会询问是否设置密码,按照提示全部输入Y,然后设置root密码。

Remove anonymous users?
是否删除匿名账户,为保证系统安全,建议删除。
Disallow root login remotely?
禁用root账户远程登录,如果您不使用本地客户端管理mysql服务器,建议禁用。
Remove test database and access to it?
mysql服务器默认会安装一个test数据库,一般来说用处不大,可以删除。
Reload privilege tables now?
刷新权限表,由于前面做了部分权限设置,建议选择Y。

mysql设置密码.jpg

现在MariaDB就安装完成,你可以通过命令行登录mysql,也可以使用PHPMYADMIN或adminer.php管理数据库,不是熟手,不建议使用命令行管理mysql。

PHP安装

v2-1d519f7da8c0df904f710778a93bc366_720w.jpg

debian 9.0默认安装的php7.0,安装命令如下

sudo apt install php7.0-fpm

默认php使用www-data用户组运行,一般情况下,无需调整php运行用户组。

启动php

sudo systemctl restart php7.0-fpm

创建网站

创建网站目录,nginx系统默认在var目录www文件夹运行默认网站。创建网站目录可选择在其他目录如srv。根据需要选择。
在www文件夹创建http://example.com目录存放网站内容

sudo mkdir -p /var/www/http://example.com/

在/etc/nginx/sites-available/目录创建网站运行配置文件http://example.com,可通过修改系统默认配置default.conf来创建。

server {
listen 80 default_server;
listen [::]:80 default_server;
server_name http://example.com www.example.com;
root /var/www/example.com;
index index.html;

location / {
try_files $uri $uri/ =404;
}

location ~* \.php$ {
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}

启用创建的网站配置

sudo ln -s /etc/nginx/sites-available/http://example.com /etc/nginx/sites-enabled

启动nginx

sudo /etc/init.d/nginx restart

现在可以在/var/www/http://example.com文件夹创建test.php,测试php文件是否正常运营。

test.php代码

<?php
phpinfo();
?>