月度归档:2017年02月

CentOS 7 搭建LNMP服务器环境

CentOS漫长的支持周期使得对系统更新的需求不是那么迫切,只要用得顺手。不过,新安装的话,就应该直接安装最新版,这样就可以用很久了。Centos 7 搭建LNMP(nginx, MariaDB, PHP)服务器和在CentOS 6 搭建LNMP服务器环境大同小异,整体过程和方法都是一样的,只需将NMP的源由CentOS 6 改成CentOS 7 的,修改几条命令就可以了。

1 更新系统:

yum update -y

查看系统版本:

cat /etc/centos-release

CentOS Linux release 7.3.1611 (Core)

2 配置源:

2.1 配置MariaDB官方源
首先需要定制MariaDB的官方源
选择合适的系统,系统版本,及MariaDB版本(最新是10.2, 目前处于RC阶段),获得CentOS 7 64位系统MariaDB 10.2 RC版本的源地址。

CentOS > CentOS 7 (x86_64) > 10.2 [Release Candidate]

配置源方法

vi /etc/yum.repos.d/MariaDB.repo

填入如下内容

# MariaDB 10.2 CentOS repository list - created 2017-02-25 08:07 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.2/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

保存退出(按ESC键,输入:wq)。

2.2 配置PHP源
webtatic源更新较快,且其命名有自己的特色方式,可以避免与其他源的某些冲突:

rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

2.3 配置nginx官方源
官方nginx有两个版本,mainline和stable,即开发板和稳定版,区别是前者引入新特性但可能有新bug,后者足够稳定。事实上,两者均比较稳定,nginx的网站总是运行在mainline版上。
以下提供两个版本供选择,请选择其一,推荐使用mainline版。

2.3.1 mainline 版
nginx的mainline版

vi /etc/yum.repos.d/nginx.repo

系统是CentOS 7,故写入如下内容

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck=0
enabled=1

保存退出。

2.3.2 stable 版

vi /etc/yum.repos.d/nginx.repo

系统是CentOS 7,故写入如下内容


[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

保存退出。

3 安装、启动服务及设置开机启动

3.1.1 安装MariaDB

yum install MariaDB-server -y

3.1.2 安装PHP

yum install php71w-fpm -y

安装扩展

yum install php71w-gd php71w-mysqlnd php71w-pdo php71w-mcrypt php71w-mbstring php71w-xmlrpc -y

3.1.3 安装nginx

yum install nginx -y

3.2 启动服务并设置开机启动

systemctl start nginx

systemctl start mariadb

systemctl start php-fpm

systemctl enable nginx

systemctl enable mariadb

systemctl enable php-fpm

4 配置

4.1 设置MariaDB

MariaDB对MySQL的命令具有良好的兼容性。
此步主要是MariaDB的安全设置:

mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 

因为是初次设置MariaDB,所以root密码是空的,此处直接回车

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y

设置数据库的密码

New password: 

设置密码,设置一个你自己知道的密码。

Re-enter new password: 

再次输入密码

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

4.2 配置PHP

vi /etc/php.ini

找到

;cgi.fix_pathinfo=1

去掉注释,并将1改成0

cgi.fix_pathinfo=0

保存退出。

4.3 配置nginx

4.3.1 默认配置

直接用浏览器打开你的主机空间的IP地址或者域名(假设IP地址为1.2.3.4,域名为www.urwp.com,后面也会用到),就可以看到nginx的欢迎页面,说明nginx已经在工作了。

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

4.3.2 配置nginx,以支持PHP

vi /etc/nginx/conf.d/default.conf

修改前的默认配置是这样的:

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

修改如下区块,取消注释,并修改部分内容:

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           /usr/share/nginx/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

4.3.3 测试PHP是否正常运行

vi /usr/share/nginx/html/phpinfo.php

写入如下代码,并保存

<?php
phpinfo();
?>

重启nginx和PHP

systemctl restart nginx
systemctl restart php-fpm

再次访问你的主机地址或域名:

http://1.2.3.4/phpinfo.php

或者

http://www.urwp.com/phpinfo.php

可见到php相关信息,说明PHP和nginx已经配合工作了。
此时LNMP网络服务环境就已初步搭建了。

接下来,可以部署自己的网站,或者开个简单的博客,比如WordPress
部署好LNMP后,不管是CentOS 6,还是CentOS 7,安装WordPress步骤都是一样的:CentOS 6系统LNMP环境下安装WordPress

为TP-Link TL-WR703N的无luci的LEDE设置PPPoE或无线桥接(bridge)的方法

更新

20170222 snapshots固件,或者通过imagebuilder自行构建的正式版固件,都可以使用该方法。 20170212 设置并不复杂。 对于无luci的LEDE(OpenWrt)来说,避免麻烦的最好方法是:由于LEDE(OpenWrt)的网络配置文件目前没有大改,暂时还可以通用。所以可以通过安装某一带luci的正式版,在luci中设置好网络,然后保存好/etc/config 下的网络相关的配置文件network、wireless和firewall。在安装Snapshop版后,通过Winscp传回或用putty对照修改就可以了。

 一 背景

一代经典路由器TP-Link TL-WR703N具备一个网口,工作模式可以是wan口也可以是lan口,使得其玩法很多。在设置没有luci的Snapshot版的LEDE(OpenWrt)时看起来很复杂,如何简化设置思路呢?

1.1 从网络数据传输来分析:

1.1.1 703N之后的网络连接:
即703N到客户端的连接:可以通过无线网络(Wireless)或者通过703N的网口经网线与客户端连接。
个人认为在现实世界中大部分人是将703N作为无线路由器来使用的,因此为了表达和简化设置说明,在此假定:客户端是通过访问703N建立的Wireless来上网的,而不是使用网线连接其网口来达到上网的目的。
此外,部分光猫的设置地址是192.168.1.1,而LEDE的默认管理地址也是192.168.1.1,会出现想访问703N的路由器设置地址但出现光猫管理界面的问题, 因此需要为703N的LEDE管理地址设置为新的ip,比如:192.168.2.1。此时,客户端的ip会变成192.168.2.*。
1.1.2  703N之前的网络连接:
有两种比较常见的场景:
一是PPPoE方式,即需要输入用户名和密码进行验证的上网方式,此时703N的网口需要工作在wan口模式;
二是无线桥接(Bridge)方式,即Bridge一个已知的无线网络。

1.2 简化的网络连接图


	+-----------+
	|   PPPoE   +---+
	+-----------+	|	+-----------+	    +-----------+
			+  ==>  +   703N    +  ==>  +  Wireless	|
	+-----------+	|	+-----------+	    +-----------+
	|   Bridge  +---+
	+-----------+

简化的设置思路是:
1.2.1 设置703N的无线网络(Wireless):
开启703N的无线网络(需要网线连接电脑与路由器的网口),通过接入无线网络完成后续的设置(不再需要网线连接电脑与路由器的网口)。
1.2.2 设置PPPoE(入户网线插入路由器的网口)或者桥接(Bridge)。

 

二 具体的设置方法

LEDE的网络相关设置的文件保存在/etc/config/下,主要包括3个:
network wireless firewall

2.1 设置703N无线网络

2.1.1 开启无线网络:
安装好LEDE后,用putty登陆路由器的管理地址:192.168.1.1.设置路由器密码后,就可以开始设置网络了。
官方提到的命令行工具是uci,但是vi直接改配置文件也挺方便。

vi /etc/config/wireless

将option disabled ‘1’改为option disabled ‘0’


config wifi-device 'radio0'
        option type 'mac80211'
        option channel '11'
        option hwmode '11g'
        option path 'platform/ar933x_wmac'
        option htmode 'HT20'
        option disabled '0'

config wifi-iface 'default_radio0'
        option device 'radio0'
        option network 'lan'
        option mode 'ap'
        option ssid 'LEDE'
        option encryption 'none'

2.1.2 更改LEDE的管理地址:

vi /etc/config/network

将option ipaddr ‘192.168.1.1’改为option ipaddr ‘192.168.2.1’


config interface 'loopback'
        option ifname 'lo'
        option proto 'static'
        option ipaddr '127.0.0.1'
        option netmask '255.0.0.0'

config globals 'globals'
        option ula_prefix 'fdd8:a2c4:707f::/48'

config interface 'lan'
        option type 'bridge'
        option ifname 'eth0'
        option proto 'static'
        option ipaddr '192.168.2.1'
        option netmask '255.255.255.0'
        option ip6assign '60'

注:此时可以看到lan区块下的option ifname 为’eth0’。

应用新的网络设置:

/etc/init.d/network restart

此时断开有线连接,搜索并接入刚开启的默认的无密码无线网络:LEDE。
注:开启的无线网络名称、密码、加密方式等可后续再设置,这里为了简化就不说了。

2.2 设置PPPoE连接或无线桥接

2.2.1 设置PPPoE连接的方法
将入户网线接入703N的网口;通过无线网络连接路由器LEDE,putty登陆LEDE管理地址:192.168.2.1。

vi /etc/config/network

将lan区域下的option ifname ‘eth0’改为option ifname ‘eth1’,并在最下面添加wan区块信息:


config interface 'loopback'
        option ifname 'lo'
        option proto 'static'
        option ipaddr '127.0.0.1'
        option netmask '255.0.0.0'

config globals 'globals'
        option ula_prefix 'fdd8:a2c4:707f::/48'

config interface 'lan'
        option type 'bridge'
        option ifname 'eth1'
        option proto 'static'
        option ipaddr '192.168.2.1'
        option netmask '255.255.255.0'
        option ip6assign '60'

config interface 'wan'
        option ifname 'eth0'
        option proto 'pppoe'
        option username 'PPPoE账户'
        option password 'PPPoE密码'

根据实际情况,更改PPPoE的用户名和密码。

保存,并应用新的网络配置:

/etc/init.d/network restart

设置完毕,应该可以上网了。

2.2.2设置无线桥接的方法
假设按“2.1设置703N无线网络”开启了无线网络LEDE,不需要PPPoE,而是需要桥接已有网络,则设置方法如下:
还是通过无线连接路由器LEDE,putty登陆LEDE管理地址:192.168.2.1。

vi /etc/config/network

在最下面添加wwan区块:


config interface 'loopback'
        option ifname 'lo'
        option proto 'static'
        option ipaddr '127.0.0.1'
        option netmask '255.0.0.0'

config globals 'globals'
        option ula_prefix 'fdd8:a2c4:707f::/48'

config interface 'lan'
        option type 'bridge'
        option ifname 'eth0'
        option proto 'static'
        option ipaddr '192.168.2.1'
        option netmask '255.255.255.0'
        option ip6assign '60'

config interface 'wwan'
        option proto 'dhcp'
vi /etc/config/wireless

将LEDE区块下的’default_radio0’删除,并增加桥接区块
config wifi-iface ‘default_radio0’改为config wifi-iface


config wifi-device 'radio0'
        option type 'mac80211'
        option channel '11'
        option hwmode '11g'
        option path 'platform/ar933x_wmac'
        option htmode 'HT20'
        option disabled '0'

config wifi-iface
        option device 'radio0'
        option network 'lan'
        option mode 'ap'
        option ssid 'LEDE'
        option encryption 'none'

config wifi-iface 'default_radio0'
        option device 'radio0'
        option network 'wwan'
        option mode 'sta'
        option ssid '已知无线网络的名称'
        option bssid '12:34:56:78:90:01'
        option encryption '无线网络的加密方法'
        option key '无线网络的密码'

需要更具实际情况更改的信息有:ssid,bssid,encryption,key。
ssid就是我们在手机或电脑WIFI那里看到的无线网络名称,bssid其实就是已知无线网络对应的无线网卡的MAC地址,encryption加密方法可尝试设为psk2,key是无线网络的密码。
更改防火墙配置:

vi /etc/config/firewall

在wan区块最后添加一行:option network ‘wan wan6 wwan’


...

config zone
        option name wan
        list network 'wan'
        list network 'wan6'
        option input REJECT
        option output ACCEPT
        option forward REJECT
        option masq 1
        option mtu_fix 1
        option network 'wan wan6 wwan'

...

应用新的网络配置:

/etc/init.d/network restart

设置完毕,应该可以上网了。