CentOS7安装apache2和php72及MSSQL驱动
CentOS7版本:
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
安装httpd
[root@localhost ~]# yum install httpd
启动httpd
[root@localhost ~]# service httpd start
此时可以访问默认网站,默认路径为:
/var/www/html
设置为开机启动
[root@localhost ~]# systemctl enable httpd
安装PHP7.2
[root@localhost ~]# yum install epel-release –y
[root@localhost ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
清除历史版本
[root@localhost ~]# yum -y remove php*
安装扩展包
[root@localhost ~]# yum -y install php72w php72w-cli php72w-fpm php72w-common php72w-devel
安装豪华扩展包
[root@localhost ~]# yum -y install php72w php72w-cli php72w-fpm php72w-common php72w-devel php72w-embedded php72w-gd php72w-mbstring php72w-mysqlnd php72w-opcache php72w-pdo php72w-xml
启动PHP
[root@localhost ~]# systemctl enable php-fpm.service
[root@localhost ~]# systemctl start php-fpm.service
测试
[root@localhost ~]# cd /var/www/html
[root@localhost html]# ls
[root@localhost html]# vi index.php
[root@localhost html]# service httpd restart
Index.php内容:
<?php
Phpinfo();
可以正常访问PHP页面了。
安装编译环境
[root@localhost freetds-1.1.15]# yum install gcc g++
安装软件的时候,提示make: command not found
Centos中无法使用make,make install,命令 make: command not found
make是gcc的编译器,VPS买来必定要安装
安装:
yum -y install gcc automake autoconf libtool make
安装g++:
yum install gcc gcc-c++
安装文件工具
[root@localhost ~]# yum install lrzsz
安装freetds
[root@localhost ~]# yum install wget
[root@localhost ~]# wget ftp://ftp.freetds.org/pub/freetds/stable/freetds-patched.tar.gz
[root@localhost ~]# tar -zxvf freetds-patched.tar.gz
[root@localhost ~]# cd freetds-*
[root@localhost freetds-1.1.15]# ./configure --prefix=/usr/local/freetds --with-tdsver=auto --enable-msdblib --with-gnu-ld --enable-shared --enable-static
[root@localhost ~]# make && make install
安装pdo_dblib
[root@localhost ~]# wget http://cn2.php.net/distributions/php-7.2.21.tar.gz
下载比较慢,可下载后上传
[root@localhost ~]# tar -zxvf php-7.2.21.tar.gz
[root@localhost ~]# cd php-*/ext/pdo_dblib
[root@localhost pdo_dblib]# /usr/local/php/bin/phpize
[root@localhost pdo_dblib]# /usr/bin/phpize
[root@localhost pdo_dblib]# ./configure --with-php-config=/usr/bin/php-config --with-pdo-dblib=/usr/local/freetds
[root@localhost pdo_dblib]# make && make install
配置PHP
在/etc/php.d/目录下建立文件
[root@localhost php.d]# vi pdo_dblib.ini
; Enable pdo_dblib extension module
extension=pdo_dblib.so
此时可以使用dblib来访问SQL Server了。
加入微软源
[root@localhost ~]# curl https://packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repos.d/mssqlrelease.repo
[root@localhost ~]# yum install msodbcsql mssql-tools unixODBC-devel
[root@localhost ~]# wget https://pecl.php.net/get/ pdo_sqlsrv-5.6.1.tgz
[root@localhost ~]# tar -zxvf pdo_sqlsrv-5.6.1.tgz
[root@localhost ~]# cd pdo_sqlsrv-5.6.1
[root@localhost pdo_sqlsrv-5.6.1]# phpize
[root@localhost pdo_sqlsrv-5.6.1]# ./configure --with-php-config=/usr/bin/php-config
出错,gcc和g++要这样安装:
[root@localhost pdo_sqlsrv-5.6.1]# yum install gcc-c++
继续编译安装
[root@localhost pdo_dblib]# make && make install
在/etc/php.d/目录下建立文件
[root@localhost php.d]# vi pdo_sqlsrv.ini
; Enable pdo_sqlsrv extension module
extension=pdo_sqlsrv.so
测试ThinkPHP
上传至系统至
/var/www/tp5
修改/etc/httpd/conf/httpd.conf
将DocumentRoot "/var/www/html"修改为DocumentRoot "/var/www/tp5/public"
将<Directory "/var/www/html">修改为<Directory "/var/www/tp5">
修改数据库连接database.php
return [
// 数据库类型
'type' => 'Sqlsrv',
// 服务器地址
'hostname' => '127.0.0.1',
// 数据库名
'database' => 'BokMES',
// 用户名
'username' => 'sa',
// 密码
'password' => ',
// 端口
'hostport' => '',
// 连接dsn
'dsn' => '',
// 数据库连接参数
'params' => [],
// 数据库编码默认采用utf8
'charset' => 'gbk',
测试
http://192.168.1.90/phpinfo.php
http://192.168.1.90/index.php/Index/index/myhello
通过!
ThinkPHP URL重写
可以通过URL重写隐藏应用的入口文件index.php
(也可以是其它的入口文件,但URL重写通常只能设置一个入口文件),下面是相关服务器的配置参考:
[ Apache ]
httpd.conf
配置文件中加载了mod_rewrite.so
模块 (这个没有找到,但不影响)AllowOverride None
将None
改为All
- 把下面的内容保存为
.htaccess
文件放到应用入口文件的同级目录下
<IfModule mod_rewrite
.c
>
Options
+FollowSymlinks
-Multiviews
RewriteEngine On
RewriteCond
%{REQUEST_FILENAME
}!-
d
RewriteCond
%{REQUEST_FILENAME
}!-
f
RewriteRule
^(.*)$ index
.php
/$
1[
QSA
,PT
,L
]
</IfModule
>
然后URL可以直接这样写了:
http://192.168.1.90/index.php/index/Index/index
安装SQL Server
[root@localhost php.d]# curl -o /etc/yum.repos.d/mssql-server.repo https://packages.microsoft.com/config/rhel/7/mssql-server-2017.repo
[root@localhost php.d]# yum install -y mssql-server
[root@localhost php.d]# /opt/mssql/bin/mssql-conf setup
然后测试从windows sql server 2008 R2迁移至Linux SQL Server 2017下。
首先在windows下备份数据库,比如是a.bak,将备份文件上传至Linux。Linux默认数据路径是/var/opt/mssql/data。然后在Linux下运行命令:
RESTORE DATABASE newdbname FROM DISK = '/var/opt/mssql/data/a.bak'
WITH
MOVE 'db1' TO '/var/opt/mssql/data/newdb.mdf',
MOVE 'db1_log' TO '/var/opt/mssql/data/newdb_log.ldf', STATS = 1, REPLACE, RECOVERY
此处注意,newdbname为新创建的数据库名字,而db1和db2_log为原数据库中的逻辑数据文件的名字。
设置防火墙:
sudo firewall-cmd --zone=public --add-port=1433/tcp --permanent
sudo firewall-cmd --reload
CentOS7防火墙相关命令:
//临时关闭
systemctl stop firewalld
//禁止开机启动
systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
恢复成功,然后可以用SSMS或navicat或Azure Data Studio等工具正常连接数据库使用了。
安装mssql-tools:
添加源:
curl https://packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repos.d/msprod.repo
安装:
yum install mssql-tools unixODBC-devel
添加登录会话访问路径:
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
source ~/.bash_profile
交互式/非登录会话:
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bashrc
Ubuntu 1904下安装会出现错误提示:
The following packages have unmet dependencies:
mssql-server : Depends: libjemalloc1 but it is not installable
Depends: libssl1.0.0 but it is not installable
需要下载安装下面的文件:
wget http://ftp.osuosl.org/pub/ubuntu/pool/universe/j/jemalloc/libjemalloc1_3.6.0-11_amd64.deb
然后安装
sudo dpkg -i libssl1.0.0_1.0.1t-1+deb8u11_amd64.deb
sudo dpkg -i libjemalloc1_3.6.0-11_amd64.deb
再安装SQL Server就可以了。