启动和启用数据库服务器
根据我们以前的安装选择,我们可以使用以下命令来启动MariaDB和MySQL数据库:
# service mysql start [ ok ] Starting MariaDB database server: mysqld.
要在重新启动运行后运行数据库,请执行以下操作:
# systemctl enable mysql
PHP 7连接数据库的测试脚本
以下PHP数据库连接代码可用于连接到关系数据库:
<?php $dbh = mysqli_connect('localhost', 'admin', 'pass'); if (!$dbh) { die('Could not connect: ' . mysqli_error()); } echo 'Connected successfully to MySQL database'; mysqli_close($dbh); ?>
将上面的代码保存到新的/var/www/html/db.php
文件中。
接下来,创建数据库用户:
# mysql -u root -e "CREATE USER 'admin'@'%' IDENTIFIED BY 'pass';" # mysql -u root -e "GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' WITH GRANT OPTION;"
最后,执行db.php
脚本:
# php /var/www/html/db.php OR # curl -i http://localhost/db.php Connected successfully to MySQL database
文本将描述Debian 9 Stretch Linux上安装LEMP服务器的步骤。
教程描述的LEMP堆栈将包括:
- Debian 9 Stretch Linux
- nginx web服务器
- MariaDB或者MySQL关系数据库
- PHP 7脚本语言
配置和启动nginx服务器
为了让nginxweb服务器与FastCGI进程管理器连接,我们需要为它提供运行socket的FastCGI进程管理器的完整路径。在上一节中,我们已经通过use-cgi-fcgi-bind命令确定了FastCGI process manager套接字的完整路径。
将现有Nginx的默认站点配置文件/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.0-fpm.sock; } }
接下来,创建一个基本的PHP页面以显示有关已安装PHP的信息:
# echo "<?php phpinfo(); ?>" > /var/www/html/index.php
最后,启动nginx服务器:
# service nginx start [ ok ] Starting nginx: nginx.
设置系统重启后自启动nginx服务:
# systemctl enable nginx
启动FastCGI Process Manager
“PHP7.0-FPM”是预先配置的。
我们需要做的就是启动它:
# service php7.0-fpm start # service php7.0-fpm status [ ok ] php-fpm7.0 is running.
设置在重启后重新启动“php7.0-fpm”:
# systemctl enable php7.0-fpm
(可选)您可以使用“cgi fcgi”测试状态和“php7.0-fpm.sock”套接字位置。“cgi fcgi”是“libfcgi0ldbl”软件包的一部分,您的系统上可能没有该软件包。要安装'libfcgi0ldbl'程序包,请执行以下操作:
# apt-get install libfcgi0ldbl
尝试连接php7.0-fpm.sock
套接字。
请注意,套接字的名称可以根据系统上安装的PHP版本而不同。
# cgi-fcgi -bind -connect /run/php/php7.0-fpm.sock Content-type: text/html; charset=UTF-8
如果“CGI-FCGI”命令未能连接到“PHP7.0-FPM.SOCK”套接字,则会出现以下错误:
# cgi-fcgi -bind -connect /run/php/php7.0-fpm.sock Could not connect to /run/php/php7.0-fpm.sock
准备工作
使用 APT-GET命令 安装所有组件,数据库我们可以选择MariaDB或者MySQL
使用Mariadb的lemp堆栈
# apt-get install nginx mariadb-server php-fpm php-mysql
使用mysql的lemp堆栈
# apt-get install nginx mysql-server php-fpm php-mysql