Ubuntu Server安装图形桌面并且开启远程RDP访问

近期有项目需要使用图形桌面来完成,windows server肯定是最好的选择,但是昂贵的授权费用以及不低的硬件配置,让我想到了Ubuntu. Linux 系主要使用centos,debian系虽然也在用,但是确实不熟悉,因此debug确实耗费了不少时间.

主要步骤:

  1. 安装ubuntu 18.04 x64,这个没有什么,直接在你的云服务商上选择就可以了
  2. 安装updates
      apt update
      apt upgrade

3. 安装图形桌面

       apt install ubuntu-desktop

这里不建议使用tasksel来安装,过于复杂,安装时间太长,而且安装了很多没有必要的东西,对于配置低的cloud来说浪费资源,而且tasksel默认安装了很多和xrdp冲突的组件, 我对ubuntu不熟悉,实在没有办法修改

4. 安装xrdp

需要注意的是,ubuntu repo里面的xrdp 的版本有问题,参见这里:

http://c-nergy.be/blog/?p=13390

所以正确步骤是:

apt install xserver-xorg-core
apt install xserver-xorg-input-all
apt install xorgxrdp
apt install xrdp

5. 创建用户远程登录, 这点是和centos不一样的地方,centos 的root 可以干很多事情,debian系的root虽然权限最大,但是很多功能都被锁定了,debian 系推荐的办法是创建一个新的用户,在需要root权限的时候,用sudo提权。假如我们要创建的用户名是ccc,那么步骤是:

adduser ccc(中间会提示你设置密码)
usermod -aG sudo ccc (将ccc加入sudo group, 这样ccc就可以用sudo提权)

 

然后我们就可以用windows的RDP远程登录这台ubuntu服务器了

默认的情况下,这个GUI只有最基本的程序,大部分经常使用的app需要安装,最好是通过command line来安装,比如说安装firefox

apt install firefox

 

 

 

国行三星S8+ 打开wifi calling

刚把我的美国电话号码port到google fi了,本来把fi 的sim 卡插到了iPhone 上,但是没有想到Google Fi 不支持在iPhone上使用wifi calling。

对Google Fi 的wifi calling 支持最好的是他们自家卖的手机, designed for google fi系列,基本都是pixel,水果机支持google fi,但是不支持wifi calling. 三星的机器支持google fi, 也支持wifi calling,想起来了我还有个国行三星s8 plus,但是按照国内手机的尿性,不知道会不会阉割wifi calling。

今天早上和google fi 的客服聊了一个小时,确认国行三星s8 plus, model number 是SM-G9550, 支持google fi 的wifi calling,但是一般来说国行会缺少某些组件. 几个关键点:

  1. 你的登录google fi 的google play账号必须是美国,任何其他国家的都不行,就是这个原因,debug了整整一个早上
  2. Google Fi 的wifi calling 只支持安卓,在安卓手机上必须安装Google Fi应用,并且在里面打开wifi calling. Wifi Calling 在”Phone Setting”里面.
  3. 你的手机里必须安装有google fi,carrier service, google play services, google connectivity services,hangouts, 并且全部权限都打开, “Background data” 和”Unrestricted data usage” 也必须打开. 国行的手机可能没有carrier service,必须得从google play上下载,如果这样的话,google connectivity services就不需要安装了
  4. 安卓手机必须打开wifi calling选项,路径为”Open the Phone app > tap More (three dots icon) > Settings > Calls > Wi-Fi calling”

Linux 的 stdin, stdout 以及stderr

今天终于有时间来仔细看下stdin,stdout和stderr了

标准输入重定向(STDIN,文件描述符为 0):默认从键盘输入,也可从其他文件或命令中输入。
标准输出重定向(STDOUT,文件描述符为 1):默认输出到屏幕。
错误输出重定向(STDERR,文件描述符为 2):默认输出到屏幕。

“stdout”按理来说应该表示为”1>”, 我们可以默认的写成”>”, 重定向stderr可以用”2>”来表示

./error.sh 2> capture.txt

表示把错误信息重定向到capture.txt

./error.sh 1> capture.txt 2> error.txt

表示把标准输出定向到capture.txt, 把错误定向到error.txt

./error.sh > capture.txt 2&>1

capture.txt 表示标准输出重定向到capture.txt,  2>&1, 表示“redirect stream 2, stderr, to the same destination that stream 1, stdout, is being redirected to”

总结一下:

输入重定向:
命令 < 文件 将文件作为命令的标准输入
命令 << 分隔符 从标准输入中读入,直到遇见分隔符才停止
命令 < A1 > A2 将文件A1作为命令的标准输入并将标准输出到文件A2

 

命令 > 文件 将标准输出重定向到一个文件中(清空原有文件的数据)
命令 2> 文件 将错误输出重定向到一个文件中(清空原有文件的数据)
命令 >> 文件 将标准输出重定向到一个文件中(追加到原有内容的后面)
命令 2>> 文件 将错误输出重定向到一个文件中(追加到原有内容的后面)
命令 >> 文件 2>&1 或 命令 &>> 文件 将标准输出与错误输出共同写入到文件中(追加到原有内容的后面)

askubuntu.com上面有个老外写了一个解释,非常的好记:

List:
command > output.txt
The standard output stream will be redirected to the file only, it will not be visible in the terminal. If the file already exists, it gets overwritten.

command >> output.txt
The standard output stream will be redirected to the file only, it will not be visible in the terminal. If the file already exists, the new data will get appended to the end of the file.

command 2> output.txt
The standard error stream will be redirected to the file only, it will not be visible in the terminal. If the file already exists, it gets overwritten.

command 2>> output.txt
The standard error stream will be redirected to the file only, it will not be visible in the terminal. If the file already exists, the new data will get appended to the end of the file.

command &> output.txt
Both the standard output and standard error stream will be redirected to the file only, nothing will be visible in the terminal. If the file already exists, it gets overwritten.

command &>> output.txt
Both the standard output and standard error stream will be redirected to the file only, nothing will be visible in the terminal. If the file already exists, the new data will get appended to the end of the file..

command | tee output.txt
The standard output stream will be copied to the file, it will still be visible in the terminal. If the file already exists, it gets overwritten.

command | tee -a output.txt
The standard output stream will be copied to the file, it will still be visible in the terminal. If the file already exists, the new data will get appended to the end of the file.

(*)
Bash has no shorthand syntax that allows piping only StdErr to a second command, which would be needed here in combination with tee again to complete the table. If you really need something like that, please look at "How to pipe stderr, and not stdout?" on Stack Overflow for some ways how this can be done e.g. by swapping streams or using process substitution.

command |& tee output.txt
Both the standard output and standard error streams will be copied to the file while still being visible in the terminal. If the file already exists, it gets overwritten.

command |& tee -a output.txt
Both the standard output and standard error streams will be copied to the file while still being visible in the terminal. If the file already exists, the new data will get appended to the end of the file.

DiscuzX 固定顶部格子广告的列数

DiscuzX的顶部的格式广告的列数,是再3,4,5之间自动变化的.

具体的代码位于:

source/class/adv/adv_text.php, 从97行开始:

 
$advcount = count($adids);
if($advcount > 5) {
		$minfillpercent = 0;
		for($cols = 5; $cols >= 5; $cols--) {
			if(($remainder = $advcount % $cols) == 0) {
				$advcols = $cols;
			break;
		} elseif($remainder / $cols > $minfillpercent)  {
			$minfillpercent = $remainder / $cols;
			$advcols = $cols;
		}
	}
} else {
	$advcols = $advcount;
	}
因此我们可以看到格子广告的列数再3,4,5之间动态变化,如果要固定列数,只需要把for循环的两个5改成4或者3就可以了

ERROR 2006 (HY000): MySQL server has gone away

当你要导入一个比较大的mysql数据库的时候,可能会产生这个错误:

mysql> source file.sql
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 2
Current database: *** NONE ***

ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 3
Current database: *** NONE ***

这是因为max_allowed_packet设置的太小了,再/etc/my.cnf中,将这个参数设置为64M或者根据实际情况设为更大的值就可以了

max_allowed_packet=64M

mysql文档:

https://dev.mysql.com/doc/refman/5.5/en/replication-features-max-allowed-packet.html

 

Centos 7 Postfix使用第三方的smtp

参见前一篇文章,阿里云的ecs全部关闭了25端口,因此我们只能使用postfix来通过第三方的smtp服务发送一些系统邮件,比如说报警邮件.

这些第三方服务商一般提供25,587端口的smtp的服务,这里我们选择使用587端口

假设你的服务器的hostname是beta.iamhippo.com, 在/etc/aliases 中设定

root    [email protected]

表示所有发向root的邮件都转发到[email protected], 下面是详细的步骤:

1) 安装必须的library

centos/redhat/fedora:

yum install cyrus-sasl-plain

debian/ubuntu:

apt-get install libsasl2-modules

如果你不安装cyrus或者libsasl库,你会得到“no mechanism found error”

2) 设置smtp的用户名,密码

postfix的smtp的用户名和密码位于/etc/postfix/sasl_password

vi /etc/postfix/sasl_password

按照下面的格式添加验证信息:

[mail.isp.example]:587 username:password

我们需要hash一些这个文件来给postfix使用

postmap /etc/postfix/sasl_passwd

一切都正常的情况下,你会在这个目录/etc/postfix目录下看到sasl_passwd.db文件

必要的安全设置:

chown root:root /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
chmod 0600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db

3) 设置generic map

如果我们是通过配置csf,让系统自动发送报警邮件,那么一般是来自于[email protected], 我们想在邮件中替换这个邮件的话,可以使用generic的功能,比如说我们想把[email protected] 改成[email protected], 那么可以这么写:

[email protected] [email protected]

然后和sasl_password 一样,我们需要hash 一下

postmap /etc/postfix/generic
chown root:root /etc/postfix/generic /etc/postfix/generic.db
chmod 0600 /etc/postfix/generic /etc/postfix/generic.db

4) 配置reply server

这个配置起来就比较简单了,直接打开/etc/postfix/main.cf文件,在最末尾添加:

# specify SMTP relay host
relayhost = [mail.isp.example]:587


# enable SASL authentication
smtp_sasl_auth_enable = yes
# disallow methods that allow anonymous authentication.
smtp_sasl_security_options = noanonymous
# where to find sasl_passwd
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
# Enable STARTTLS encryption
smtp_use_tls = yes
# where to find CA certificates
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
# where to find generic
smtp_generic_maps = hash:/etc/postfix/generic

保存配置,然后重启postfix

systemctl restart postfix

当然了,建议在重启之前先把postfix 中的queue mails 都删掉。。要不然积累了大量的邮件会一次性发出的

postsuper -d ALL

或者

postfix -f

或者

postfix flush

查看mail queue:

mailq

 

阿里云邮件推送的坑

国内的服务器一直在使用阿里云的,没有办法目前的情况下阿里云确实是国内的No.1

阿里云的ecs默认是关掉25端口的,就算是你申请解封25端口成功了,你也无法用25端口发信,他只允许你用25端口连接到其他的smtp服务器来发信.

使用阿里云的服务,自然而然的想到阿里云的邮件推送服务,而且他还赠送一部分免费的额度,对于我们这种只需要发送触发邮件的人来说,这是非常合适的.

但是这里的一个坑就是smtp协议下只支持25,80,465端口,而465端口一般用来支持STARTTLS, 这个协议比较老,国际标准化组织已经不再推荐,很多的程序例如iredmail默认已经不支持这个端口了。如果你使用的是debian,ubuntu的话,那么这是没有任何问题的,因为自带的postfix 的版本相对来说要新很多, 是3.0 的版本,支持465端口. 如果你使用的是centos7 并且使用的是自带的postfix的话,那么恭喜你,你获奖了。。。

centos7自带的postfix的版本相对来说要低很多,是2.10的版本。。。

而且postfix 的2.10的版本,对于465端口,也就是smtps并不是很支持,你需要使用的stunnel的转发smtps的请求. 详细请看这里:

http://www.postfix.org/TLS_README.html#client_smtps

网上铺天盖地的教程都是apt-get, 我想他们可以也没有想到在centos7 里面会出现问题,不值得花时间去演就演stunnel,那么多的提供标准服务的smtp服务商,舍弃阿里云推送就好了.

如果你的网站位于国外,那么你可以使用sendgrid或者mailgun,都是非常的方便

PHP7 兼容性检测

PHP 7 都出到7.4 了,是时候把手中的discuz论坛升级到PHP 7了,考虑了半天兼容性的问题,准备升级到php 7.3

discuz 的主程序已经升级到最新的discuz 20191201版本了,兼容PHP 7.3应该是没有问题,剩下的就是安装的那些插件的兼容性的问题.

检测PHP7 的兼容性,我们这里使用主流的PHPCompatibility 配合PHP Code_Sniffer

https://github.com/squizlabs/PHP_CodeSniffer
https://github.com/PHPCompatibility/PHPCompatibility

PHPCompatibility 是PHP Code_Sniffer 的插件,因此我们应该先安装PHP Code_Sniffer, 在github上有很多中的安装方式,这里我们选择最近的wget 或者curl下载二进制程序,然后把二进制程序放到全局去

# Download using curl
curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar
curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar

# Or download using wget
wget https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar
wget https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar

# register as global commands
mv phpcs.phar phpcs
mv phpcbf.phar phpcbf
chmod 755 phpcs
chmod 755 phpcbf
mv phpcs /usr/local/bin/
mv phpcbf /usr/local/bin/

这样就安装完了phpcs,下一步我们需要下载PHPCompatibility这个插件,并且让phpcs使用这个插件

# Download PHPCompability
cd ~
wget https://github.com/PHPCompatibility/PHPCompatibility/archive/9.3.5.zip
unzip 9.3.5.zip
# path is /root/PHPCompability9.3.5
# config phpcs to use PHPCompatibility
phpcs --config-set installed_paths /root/PHPCompability9.3.5

这样phpcs就可以使用PHPCompatibility9.3.5了,当然了也可以使用git的形式来下载,这样更新PHPCompatibility比较方便

加入说我们需要检查的文件folder 是/home/plugin, 那么我们就可以用

phpcs -p --standard=PHPCompatibility --runtime-set testVersion 7.3 --report-full=/home/php.log /home/plugin

-p: 打印progress到console上面

–standard: 表示使用哪个标准

–runtime-set testVersion 7.3 : 表示用PHP7.3的标准来检查

–report-full: 表示将结果输出到某一文件

 

检查完毕,删掉了不兼容php7.3 的插件,然后就开始准备升级discuz到7.3了