ubuntu20(root)+PHP+nginx+mysql
nginx【较新版】
wget http://nginx.org/keys/nginx_signing.key
apt-key add nginx_signing.key
lsb_release -a
vi /etc/apt/sources.list# lsb_release -a 返回的Ubuntu系统版本编码(英文) 添加到sources.list中。【focal为Ubuntu20.04对应版本编码】
deb http://nginx.org/packages/ubuntu/ focal nginx
deb-src http://nginx.org/packages/ubuntu/ focal nginx# 保存sources.list退出
:wq# 安装nginx
apt update
apt -y install nginx
systemctl enable/disable/status/start/stop/restart nginx# 查看Nginx版本
nginx -v1.22.0
PHP7.4【修改数字即可安装不同版本】
# 彻底卸载apt安装的PHP
aptitude purge `dpkg -l | grep php| awk '{print $2}' |tr "\n" " "` apt -y install software-properties-common
add-apt-repository ppa:ondrej/php
apt update
apt -y install php7.4 php7.4-fpm# 安装PHP扩展,php7.4-dev用于后面pecl安装扩展时自动编译(支持命令:phpize)
apt -y install php7.4-mcrypt php7.4-mbstring php7.4-curl php7.4-cli php7.4-mysql php7.4-gd php7.4-intl php7.4-xsl php7.4-zip php7.4-dev php7.4-xml php7.4-soapsystemctl enable/disable/status/start/stop/restart php7.4-fpm# 查询PHP版本
php -v7.4.22
mysql8
apt install -y mysql-server
mysql8设置root密码
第一步:mysql8默认安装完成后,root账户没有密码,直接执行命令“mysql”即可登录。
第二步:还原密码设置。
update mysql.user set authentication_string='' where user='root';
update mysql.user set plugin='mysql_native_password' where user='root';
FLUSH PRIVILEGES;
第三步:通过mysql提供的脚本来设置密码。
MySQL安装随附一个名为的脚本mysql_secure_installation。可以借助它设置我们MYSQL服务器的安全性!
mysql_secure_installation
按提示执行即可。最后出现“All done!”说明脚本执行完成。直接使用新设置密码登录即可。
整合PHP+nginx
find / -name www.confvi /etc/php/7.4/fpm/pool.d/www.conf# 设置php-fpm通过监听端口方式工作
;listen = /run/php/php7.4-fpm.sock
listen = 127.0.0.1:9000
nginx配置文件路径
ps -ef|grep nginx# nginx配置文件地址
root 498847 1 0 00:42 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.confvi /etc/nginx/nginx.conf# 自定义配置文件位置
include /etc/nginx/conf.d/*.conf;cd /etc/nginx/conf.d/vi weiweixiao.conf
:wq
weiweixiao.conf内容
server {
# 修改真实域名端口
# 如果就是使用默认80端口,就将此配置移植到/etc/nginx/sites-enable/default中
listen 80 default_server;
; listen [::]:80 default_server ipv6only=on; # 修改真实项目地址
root XXXXXXXXXXXXXXXXX;
index index.html index.htm index.php; # Make site accessible from http://localhost/
server_name localhost; location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
#try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
index index.html index.htm index.php;
# if (!-e $request_filename) {
# break;
# }
# try_files $uri /index.php$uri;
# 伪静态配置
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php/$1 last;
break;
}
} # php-fpm配置
location ~ .+\.php($|/) {
# 修改真实项目地址
root ##########################;
fastcgi_index index.php;
# fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_split_path_info ^((?U).+.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
# 监听php-fpm端口
fastcgi_pass 127.0.0.1:9000;
fastcgi_connect_timeout 300s;
fastcgi_send_timeout 300s;
fastcgi_read_timeout 300s;
include fastcgi_params;
} location ^~ /Application/Runtime {
deny all;
} location ~* .*\.(svn|git|cvs).* {
deny all;
}}
php连接sqlserver配置
ODBC驱动安装
官网教程:https://learn.microsoft.com/zh-cn/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver16#ubuntu17
# 查询Ubuntu系统版本号
lsb_release -rs 20.04# 安装ODBC驱动
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
curl https://packages.microsoft.com/config/ubuntu/20.04/prod.list > /etc/apt/sources.list.d/mssql-release.listapt-get update
ACCEPT_EULA=Y apt-get install -y msodbcsql18# optional: for bcp and sqlcmd(通过命令行直连sqlserver)
ACCEPT_EULA=Y apt-get install -y mssql-tools18
echo 'export PATH="$PATH:/opt/mssql-tools18/bin"' >> ~/.bashrc
source ~/.bashrc# optional: for unixODBC development headers
apt-get install -y unixodbc-dev
PHP扩展【sqlsrv】安装
官网教程:https://learn.microsoft.com/zh-cn/sql/connect/php/installation-tutorial-linux-mac?view=sql-server-ver16
# 如果,提示不存在pecl,按照提示 安装即可。pecl install sqlsrv
pecl install pdo_sqlsrvprintf "; priority=20\nextension=sqlsrv.so\n" > /etc/php/7.4/mods-available/sqlsrv.ini
printf "; priority=30\nextension=pdo_sqlsrv.so\n" > /etc/php/7.4/mods-available/pdo_sqlsrv.iniphpenmod -v 7.4 sqlsrv pdo_sqlsrvsystemctl resrart php7.4-fpm
连接测试
<?php
namespace Test;/**
* sqlsrv直连
*/
class Test { protected $DB_SQLSERVER = array(
'db_type' => 'sqlsrv',
'db_user' => 'user',
'db_pwd' => "password",
'db_host' => '127.0.0.1',
'db_port' => '1433',
'db_name' => 'dbname',
'db_charset' => 'UTF8',
); public function test(){
$sql='select * from table_name where id =1';
dump($this->querySql($sql));
} protected $sqlsrvConn = null;
protected function sqlsrvConn(){
$dbServerName = $this->DB_SQLSERVER['db_host'] . "," . $this->DB_SQLSERVER['db_port'];
$connInfo = array();
$connInfo['UID'] = $this->DB_SQLSERVER['db_user'];
$connInfo['PWD'] = $this->DB_SQLSERVER['db_pwd'];
$connInfo['Database'] = $this->DB_SQLSERVER['db_name'];
$connInfo['Encrypt'] = 'no'; // 不使用SSL加密
$this->sqlsrvConn = sqlsrv_connect($dbServerName,$connInfo);
if($this->sqlsrvConn === false){
$this->ajaxReturn(genInfoTip(CODE_FAIL, "sqlserver数据库连接失败:".sqlsrv_errors()));
}
}
public function querySql($sql){
if(empty($sql)){
return genInfoTip(CODE_FAIL, "sql不能为空");
}
$this->sqlsrvConn();
$queryRet = sqlsrv_query($this->sqlsrvConn, $sql);
if($queryRet === false){
$this->ajaxReturn(genInfoTip(CODE_FAIL, "sqlserver数据库查询异常:".sqlsrv_errors()));
}
$result = array();
while(($row = sqlsrv_fetch_array($queryRet))){
$result[] = $row;
}
return $result;
}
public function execSql($sql){
if(empty($sql)){
return genInfoTip(CODE_FAIL, "sql不能为空");
}
$this->sqlsrvConn();
$exec = sqlsrv_query($this->sqlsrvConn, $sql);
if($exec === false){
$this->ajaxReturn(genInfoTip(CODE_FAIL, "sqlserver数据库sql操作异常:".sqlsrv_errors()));
}
$num=sqlsrv_rows_affected($exec);//返回修改的行数
return $num;
}
问题
报错:SQLSTATE[08001]: [Microsoft][ODBC Driver 18 for SQL Server]SSL Provider: [error:0A000086:SSL routines::certificate verify failed:self-signed certificate]
看到报错SSL,又是verify failed。怀疑是http传输加密,导致验证失败。
考虑是否可以取消SSL加密。
查询官网(使用连接字符串关键字 - SQL Server Native Client | Microsoft Learn)
发现有个字段和加密相关:Encrypt
然后直接sqlcmd测试(sqlcmd使用方式参考:使用 sqlcmd 进行连接 - ODBC Driver for SQL Server | Microsoft Learn):
测试 -N 参数:
最后,发现ThinkPHP3.2中并不支持此参数。故上文Test.class.php中采用直连方式 配置此参数,最终连接成功!
发表评论 取消回复