开启 Aliyun Linux 之旅

概览

由于之前的机器系统使用比较久,是在几年前购买时安装的 CentOS 6.8 x64,如果升级核心版本需要重装系统,在此之际又想体验一下新机器和新配置网络带宽,遂重新下单了一台阿里云 ECS,经过综合比较和自身实际需求,最终选择了 Aliyun Linux 系统,Aliyun Linux 和最新 CentOS 的 Linux 内核系统版本相近以及操作模式兼容,并且可以长期支持服务,下面就我最近两天从新机到安装使用过程碰到的问题做一个记录,以备自查或可以帮助有需要的朋友。

Hello, Hunting Beagle!

Aliyun Linux 系统完整版本号是 Aliyun Linux release 2.1903 LTS (Hunting Beagle) ,Hello, Hunting Beagle!

更新公网 IP

如果对新购买 ECS 分配的公网 IP 不满意,可以在购买后的 6 小时内申请更换,可更换三次。

Nginx 域名配置

我按之前思路去配置,结果在 /etc/nginx/conf.d 是空白,没有任何配置文件或模版,后面才查到配置在 /etc/nginx/ 里面的 nginx.conf,这个跟 CentOS 6.8 x64 默认配置有点不一样,配置参数和域名绑定都默认在 /etc/nginx/里面,当然,知道了后可以自己再改成跟 CentOS 6.8 x64 一样,为了不影响其他有关配置,所以就按系统本身的路径来配置,不折腾了。后面由于配置 HTTPS 还是调整了 nginx.conf 的配置思路,将默认的 nginx.conf 域名配置注释,在 conf.d 里面单独做域名配置。

Ningx 支持 PHP

安装好了 PHP 和 PHP-FPM 后,启动服务发现还是不能正常访问 php 文件,变成下载了,这个就是没有处理 php 文件,我同样按之前思路去找配置文件,在/etc/nginx/里面的 nginx.conf 配置文件里耗费了很多时间才查到原因,以下附上常规的简洁 Nginx 配置模板。

# The default server
# conf_file in /etc/nginx/conf.d/

server {
    listen         80;
    # listen       80 default_server;
    # listen       [::]:80 default_server;
    server_name    ssl.hexingxing.cn;
    root           /myweb/ssl;
    #rewrite ^(.*)$  https://$host$1 permanent;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
    index index.html index.htm index.php;
    }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }


    # 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;
        fastcgi_param  SCRIPT_FILENAME   /myweb/ssl$fastcgi_script_name;
        include        fastcgi_params;
    }
}

配置 MySQL(MariaDB)

使用 yum install mysql mysql-server 命令安装,安装后懵了,竟然启动不了,提示 “Failed to start mysqld.service: Unit not found.”,经过多方查询,原来在安装命令 mysql mysql-server 中默认已经替换安装了 mariadb ,老早就知道 mysql 改成 mariadb 的原因,没想到命令都直接改了,当时安装了没有注意看,只注意了安装完成就去启动,才出现之前的一幕,在后面查看命令记录确认了是安装的 mariadb,使用 systemctl start mariadb.service 还是启动不了,提示 Failed to start mariadb.service: Unit not found.,估计还是前面的命令问题,看起来是服务器没有安装完整,遂使用新的命令安装,yum install mariadb mariadb-server -y,安装后启动成功。使用 mysql_secure_installation 命令配置 mariadb 安全选项,一顿萌操作后,Thanks for using MariaDB!

安装 MariaDB

yum install mariadb mariadb-server -y #安装 mariadb

Installed:
  mariadb.x86_64 1:5.5.65-1.1.al7                                                              
  mariadb-server.x86_64 1:5.5.65-1.1.al7 

Dependency Updated:
  mariadb-libs.x86_64 1:5.5.65-1.1.al7  
  perl-Compress-Raw-Bzip2.x86_64 0:2.061-3.1.al7           perl-Compress-Raw-Zlib.x86_64 1:2.06
  perl-DBD-MySQL.x86_64 0:4.023-6.1.al7                    perl-DBI.x86_64 0:1.627-4.1.al7     
  perl-Data-Dumper.x86_64 0:2.145-3.1.al7                  perl-IO-Compress.noarch 0:2.061-2.1.
  perl-Net-Daemon.noarch 0:0.48-5.1.al7                    perl-PlRPC.noarch 0:0.2020-14.1.al7 

Complete!
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): #初次配置无密码可直接回车,有密码输入密码回车
OK, successfully used password, moving on...

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

Set root password? [Y/n] y #是否设置 root 用户密码,输入 y 并回车或直接回车
New password: #设置 root 用户的密码
Re-enter new password: #再输入一次你设置的密码
Password updated successfully!
Reloading privilege tables..
 ... Success!


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 #是否禁止 root 远程登录, 根据自己的需求选择 Y/n 并回车, 建议禁止
 ... 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] #是否删除 test 数据库, 直接回车
 - 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] #是否重新加载权限表,直接回车
 ... 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! #最后看到这个提示说明已经配置成功
service mariadb restart #重启 MariaDB //这是旧命令,可使用新命令显示操作详情
Redirecting to /bin/systemctl restart mariadb.service

https 配置(Let’s Encrypt)

wget https://dl.eff.org/certbot-auto #下载安装程序
--2020-06-05 19:41:31--  https://dl.eff.org/certbot-auto
Resolving dl.eff.org (dl.eff.org)... 151.101.228.201, 2a04:4e42:1a::201
Connecting to dl.eff.org (dl.eff.org)|151.101.228.201|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 79897 (78K) [application/octet-stream]
Saving to: ‘certbot-auto’

100%[=============================================================================================>] 79,897       240KB/s   in 0.3s   

2020-06-05 19:41:32 (240 KB/s) - ‘certbot-auto’ saved [79897/79897]
mv certbot-auto /certbot/ #移动安装程序到中意目录
chown root /certbot/certbot-auto #添加到组
chmod 0755 /certbot/certbot-auto #配置权限
/certbot/certbot-auto #安装证书程序
Bootstrapping dependencies for RedHat-based OSes... (you can skip this with --no-bootstrap)
yum is /usr/bin/yum
yum is hashed (/usr/bin/yum)
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Package gcc-4.8.5-39.1.al7.x86_64 already installed and latest version
Package 1:openssl-1.0.2k-19.2.al7.x86_64 already installed and latest version
Package ca-certificates-2019.2.32-76.1.al7.noarch already installed and latest version
……
Install  9 Packages (+24 Dependent packages)
Upgrade             (  6 Dependent packages)

Total download size: 19 M
Is this ok [y/d/N]: y
……
Complete!
Creating virtual environment...
Installing Python packages...
Installation succeeded.
Saving debug log to /var/log/letsencrypt/letsencrypt.log

How would you like to authenticate and install certificates?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: Apache Web Server plugin (apache)
2: Nginx Web Server plugin (nginx)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2 #选择 apache 或 nginx 程序
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices)
 (Enter 'c' to cancel): it@hexingxing.cn #输入你常用的邮箱地址

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: a #接受协议或条款,必选 a

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: n #是否可以公开你的邮箱地址

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
No names were found in your configuration files. Please enter in your domain
name(s) (comma and/or space separated) (Enter 'c' to cancel): //第一种情况:没有检测到域名配置文件,手动输入域名

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Which names would you like to activate HTTPS for?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: ssl.hexingxing.cn //第二种情况,如果系统有配置文件,这里会显示你服务器已配置的域名列表
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel): #选择你要创建证书的域名或者回车全选
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for ssl.hexingxing.cn
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/nginx.conf
Redirecting all traffic on port 80 to ssl in /etc/nginx/nginx.conf #新版本默认重定向到 https

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://ssl.hexingxing.cn #恭喜! 您已成功启用 https://ssl.hexingxing.cn

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=ssl.hexingxing.cn #访问此链接测试你的 HTTPS 质量
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
 …… #此处省略了很多字,关于证书密钥文件的存放情况以及到期时间


   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le
{certbot 的绝对或相对路径}/certbot-auto renew --dry-run #测试手动模拟更新证书

数据内网迁移:使用 scp 命令快速传输

scp -r root@内网服务器 IP 地址:/被拷贝数据的目录或文件位置 /当前服务器的目录位置

这个功能很方便,连接成功后确认对方服务器身份以及输入密码后即可完成迁移,速度在 60MB/s 左右,但是只能针对静态数据,如果是网站还在运行,在迁移时网站产生的数据将会丢失或重复,建议在闲时停机操作。

php5.x.xx 升级 php7.x.xx

由于最新版本 Wordpess 需要较新版本 php,所以得把系统自带的 php 5.x.xx 手工升级,自带的 yum 源最新可用已经是 5.x.xx。

php -v #查看已安装的 php 版本
PHP 5.4.16 (cli) (built: Apr  2 2020 14:36:36) 
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
yum list installed |grep php #查看已安装的 php 程序条目
php.x86_64                            5.4.16-48.1.al7                  @updates 
php-bcmath.x86_64                     5.4.16-48.1.al7                  @updates 
php-cli.x86_64                        5.4.16-48.1.al7                  @updates 
php-common.x86_64                     5.4.16-48.1.al7                  @updates 
php-fedora-autoloader.noarch          1.0.1-2.el7                      @epel    
php-fpm.x86_64                        5.4.16-48.1.al7                  @updates 
php-gd.x86_64                         5.4.16-48.1.al7                  @updates 
php-mbstring.x86_64                   5.4.16-48.1.al7                  @updates 
php-mysql.x86_64                      5.4.16-48.1.al7                  @updates 
php-pdo.x86_64                        5.4.16-48.1.al7                  @updates 
php-php-gettext.noarch                1.0.12-1.el7                     @epel    
php-process.x86_64                    5.4.16-48.1.al7                  @updates 
php-tcpdf.noarch                      6.2.26-1.el7                     @epel    
php-tcpdf-dejavu-sans-fonts.noarch    6.2.26-1.el7                     @epel    
php-tidy.x86_64                       5.4.16-9.el7                     @epel    
php-xml.x86_64                        5.4.16-48.1.al7                  @updates 
phpMyAdmin.noarch                     4.4.15.10-4.el7                  @epel    
yum remove php* #删除所有旧版的 php 程序
Loaded plugins: fastestmirror
Resolving Dependencies
--> Running transaction check
---> Package php.x86_64 0:5.4.16-48.1.al7 will be erased
---> Package php-bcmath.x86_64 0:5.4.16-48.1.al7 will be erased
---> Package php-cli.x86_64 0:5.4.16-48.1.al7 will be erased
---> Package php-common.x86_64 0:5.4.16-48.1.al7 will be erased
---> Package php-fedora-autoloader.noarch 0:1.0.1-2.el7 will be erased
---> Package php-fpm.x86_64 0:5.4.16-48.1.al7 will be erased
---> Package php-gd.x86_64 0:5.4.16-48.1.al7 will be erased
---> Package php-mbstring.x86_64 0:5.4.16-48.1.al7 will be erased
---> Package php-mysql.x86_64 0:5.4.16-48.1.al7 will be erased
---> Package php-pdo.x86_64 0:5.4.16-48.1.al7 will be erased
---> Package php-php-gettext.noarch 0:1.0.12-1.el7 will be erased
---> Package php-process.x86_64 0:5.4.16-48.1.al7 will be erased
---> Package php-tcpdf.noarch 0:6.2.26-1.el7 will be erased
---> Package php-tcpdf-dejavu-sans-fonts.noarch 0:6.2.26-1.el7 will be erased
---> Package php-tidy.x86_64 0:5.4.16-9.el7 will be erased
---> Package php-xml.x86_64 0:5.4.16-48.1.al7 will be erased
---> Package phpMyAdmin.noarch 0:4.4.15.10-4.el7 will be erased
--> Finished Dependency Resolution

…… #这里省略很多过程细节

Complete!
rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm #添加第三方 yum 包
Retrieving https://mirror.webtatic.com/yum/el7/epel-release.rpm
warning: /var/tmp/rpm-tmp.pHwGN8: Header V4 RSA/SHA1 Signature, key ID 62e74ca5: NOKEY
Preparing...                          ################################# [100%]
	package epel-release-7-12.noarch (which is newer than epel-release-7-5.noarch) is already installed
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm #添加第三方 yum 包的 webtatic 库
Retrieving https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
warning: /var/tmp/rpm-tmp.yv8wmd: Header V4 RSA/SHA1 Signature, key ID 62e74ca5: NOKEY
Preparing...                          ################################# [100%]
	package webtatic-release-7-3.noarch is already installed
yum list php72* #查看 php 可用安装版本包
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * webtatic: uk.repo.webtatic.com
Available Packages
php72w-bcmath.x86_64                                                     7.2.27-1.w7                                           webtatic
php72w-cli.x86_64                                                        7.2.27-1.w7                                           webtatic
php72w-common.x86_64                                                     7.2.27-1.w7                                           webtatic
php72w-dba.x86_64                                                        7.2.27-1.w7                                           webtatic
php72w-devel.x86_64                                                      7.2.27-1.w7                                           webtatic
php72w-embedded.x86_64                                                   7.2.27-1.w7                                           webtatic
php72w-enchant.x86_64                                                    7.2.27-1.w7                                           webtatic
php72w-fpm.x86_64                                                        7.2.27-1.w7                                           webtatic
php72w-gd.x86_64                                                         7.2.27-1.w7                                           webtatic
php72w-imap.x86_64                                                       7.2.27-1.w7                                           webtatic
php72w-interbase.x86_64                                                  7.2.27-1.w7                                           webtatic
php72w-intl.x86_64                                                       7.2.27-1.w7                                           webtatic
php72w-ldap.x86_64                                                       7.2.27-1.w7                                           webtatic
php72w-mbstring.x86_64                                                   7.2.27-1.w7                                           webtatic
php72w-mysql.x86_64                                                      7.2.27-1.w7                                           webtatic
php72w-mysqlnd.x86_64                                                    7.2.27-1.w7                                           webtatic
php72w-odbc.x86_64                                                       7.2.27-1.w7                                           webtatic
php72w-opcache.x86_64                                                    7.2.27-1.w7                                           webtatic
php72w-pdo.x86_64                                                        7.2.27-1.w7                                           webtatic
php72w-pdo_dblib.x86_64                                                  7.2.27-1.w7                                           webtatic
php72w-pear.noarch                                                       1:1.10.4-1.w7                                         webtatic
php72w-pecl-apcu.x86_64                                                  5.1.9-1.w7                                            webtatic
php72w-pecl-apcu-devel.x86_64                                            5.1.9-1.w7                                            webtatic
php72w-pecl-geoip.x86_64                                                 1.1.1-1.2.w7                                          webtatic
php72w-pecl-igbinary.x86_64                                              2.0.5-1.w7                                            webtatic
php72w-pecl-igbinary-devel.x86_64                                        2.0.5-1.w7                                            webtatic
php72w-pecl-imagick.x86_64                                               3.4.3-1.2.w7                                          webtatic
php72w-pecl-imagick-devel.x86_64                                         3.4.3-1.2.w7                                          webtatic
php72w-pecl-libsodium.x86_64                                             1.0.6-1.2.w7                                          webtatic
php72w-pecl-memcached.x86_64                                             3.0.4-1.w7                                            webtatic
php72w-pecl-mongodb.x86_64                                               1.5.3-1.w7                                            webtatic
php72w-pecl-redis.x86_64                                                 3.1.6-1.w7                                            webtatic
php72w-pecl-xdebug.x86_64                                                2.6.1-1.w7                                            webtatic
php72w-pgsql.x86_64                                                      7.2.27-1.w7                                           webtatic
php72w-phpdbg.x86_64                                                     7.2.27-1.w7                                           webtatic
php72w-process.x86_64                                                    7.2.27-1.w7                                           webtatic
php72w-pspell.x86_64                                                     7.2.27-1.w7                                           webtatic
php72w-recode.x86_64                                                     7.2.27-1.w7                                           webtatic
php72w-snmp.x86_64                                                       7.2.27-1.w7                                           webtatic
php72w-soap.x86_64                                                       7.2.27-1.w7                                           webtatic
php72w-sodium.x86_64                                                     7.2.27-1.w7                                           webtatic
php72w-tidy.x86_64                                                       7.2.27-1.w7                                           webtatic
php72w-xml.x86_64                                                        7.2.27-1.w7                                           webtatic
php72w-xmlrpc.x86_64                                                     7.2.27-1.w7                                           webtatic
yum install php72w php72w-opcache  php72w-xml  php72w-mcrypt   php72w-gd php72w-devel php72w-mysqlnd  php72w-intl    php72w-mbstring   php72w-pear  php72w-pdo  php72w-fpm #安装 php7.2.xx 及插件
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * webtatic: uk.repo.webtatic.com
No package php72w-mcrypt available.
Resolving Dependencies

…… #这里省略很多安装过程

Installed:
  mod_php72w.x86_64 0:7.2.27-1.w7             php72w-devel.x86_64 0:7.2.27-1.w7           php72w-fpm.x86_64 0:7.2.27-1.w7             
  php72w-gd.x86_64 0:7.2.27-1.w7              php72w-intl.x86_64 0:7.2.27-1.w7            php72w-mbstring.x86_64 0:7.2.27-1.w7        
  php72w-mysqlnd.x86_64 0:7.2.27-1.w7         php72w-opcache.x86_64 0:7.2.27-1.w7         php72w-pdo.x86_64 0:7.2.27-1.w7             
  php72w-pear.noarch 1:1.10.4-1.w7            php72w-xml.x86_64 0:7.2.27-1.w7            

Dependency Installed:
  autoconf.noarch 0:2.69-11.2.al7        automake.noarch 0:1.13.4-3.2.al7               libargon2.x86_64 0:20161029-3.el7             
  libicu.x86_64 0:50.2-4.1.al7           perl-Test-Harness.noarch 0:3.28-3.1.al7        perl-Thread-Queue.noarch 0:3.02-2.1.al7       
  php72w-cli.x86_64 0:7.2.27-1.w7        php72w-common.x86_64 0:7.2.27-1.w7             php72w-process.x86_64 0:7.2.27-1.w7           

Complete!
php -v #查看更新版本号
PHP 7.2.27 (cli) (built: Jan 26 2020 15:49:49) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.27, Copyright (c) 1999-2018, by Zend Technologies

完成 php7.x.xx 的安装。

配置 phpMyAdmin 管理数据库

cd /var/www/html #切换到 web 目录
wget https://files.phpmyadmin.net/phpMyAdmin/4.9.5/phpMyAdmin-4.9.5-all-languages.tar.gz #官网下载最新版 PMA
tar zxvf phpMyAdmin-4.9.5-all-languages.tar.gz #解压
mv phpMyAdmin-4.9.5-all-languages phpmyadminkadsfjkafdj #修改文件目录的名称

通过 https://myweb.com/phpmyadminkadsfjkafdj 访问管理数据库

导入数据库报错:没有接收到要导入的数据。

登录数据管理平台 phpMyAdmin 操作时,php 默认配置的上传文件大小是 2M,如果导入的数据库文件大于 2M 将会报错,提示 “没有接收到要导入的数据。可能是文件名没有提交,也可能是文件大小超出 PHP 限制。参见 FAQ 1.16。”,此时根据 FAQ 1.16 的文档指引,编辑 php.ini 文件,vi /etc/php.ini,编辑模式下搜索关键字段 /post_max_size,修改其值为 12M,搜索关键字段 /upload_max_filesize,修改其值为 10M,其中,该值 post_max_size 必须大于 upload_max_filesize,现在可以上传了。

以上官方文档即在自己服务器的 phpMyAdmin 目录下,https://yourdomain.com/phpMyAdmin-dirname/doc/html/faq.html#faq1-16

导入数据库报错:#1046 – No database selected

错误:MySQL 反回:#1046 – No database selected
原因:错误原因是因为导入时未选择 左侧数据库
解决方法:导入数据库正确操作顺序:点击左侧对应数据库名称(如:test)> 点击右侧顶部 “导入” 按钮。

导入 WordPress xml 数据

安装导入器插件时显示 “安装失败: 无法复制文件。 wordpress-importer/class-wp-import.php”,这个我有经验,因为 WordPress 程序没有权限创建文件,运行以下命令完成授权。

chown -R apache:apache worepress #apache 是 wordpress 程序用户名,wordpress 是程序所在目录名称

完成以上命令授权后,即可导入数据。

导入数据库和导入 WordPress 只选其一即可,不用两个都执行,否则会重复两份数据或内容重复,如果只有 WordPress 有使用数据库,建议使用 “导入 WordPress xml 数据” 方法导入即可,这样就不会出现多余数据库和无用数据,纯净的 WordPress 数据。

如果数据库还用于其他程序,那么先导入数据库,再导入 WordPress xml 数据,这样最终以 WordPress xml 的数据做最终更新,以此为最终版本数据。

如果是更换域名后导入那么可以在导入选项时勾选下载并导入附件,并保持原域名还可以正常访问,即可下载附件或导入到新的 WordPress。

如果是之前和现在更新的是同一个域名,那就无法下载附件并导入了,因为现在的域名指向的正是当前操作的 WordPress,所以从当前网站找附件是无法找到的,最好的方法就是从之前的 WordPress 目录通过之前说的内网 scp 方式或 ftp 方法将 “/wordpress/wp-content/uploads” 目录传输到当前 WordPress 程序对应目录下即可完成链接导入。

Easy Hide Login 插件事件

WordPress 迁移后,之前用的 Easy Hide Login 使用配置的方法不能登录了,遂通过 ftp 方式将插件先重命名,再通过默认的方式访问登录页面,登录进来后才发现原来默认的字段是 root,我当时有想过可能是恢复默认了,但是却没有想到是这个,现在想想也应该是这个,当时也没有查官方文档,所以就直接干脆重命名插件目录来解决。

Easy Hide Login
Slug Text :root
Login url demo: example.com?slug_text

原服务器全盘备份

其实这个标题有些过了,当时是想做全盘备份的,但想想也不是全部都要用,做全盘操作一来时间会比较久,二来即使备份好了,也不是全盘的数据以后都要用,所以还是选了备份自己确保可能后面要用的相关目录,其中包括实际数据和程序相关配置文件,方便以后参考。

  • 网站数据目录:/wwwroot
  • 网站数据备份目录:/wwwrootbackup
  • 数据库导出目录:/var/lib/phpMyAdmin/save/
  • NGINX 配置文件目录:/etc/nginx/

登录 MySQL 数据库

[root@localhost ~]# mysql -u root -h 127.0.0.1 -P 3306 -p 

##登录 MySql;-u 指定用户;-h 指定数据库主机;-P 指定端口号;-p 指定密码;

mysql> show global variables like 'port'; 
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3306 |
+---------------+-------+
1 row in set (0.00 sec)

##查看 MySql 端口号

mysql> show databases; 
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| zabbix |
+--------------------+
3 rows in set (0.00 sec)

##显示数据信息

mysql> use zabbix 
Database changed

##选择该数据库,做后续相应操作

CentOS 7 安装 MariaDB MariaDB-Server(MySQL)数据库实例

[root@hexingxing]# yum -y install mariadb mariadb-server
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: centos.ustc.edu.cn
 * extras: centos.ustc.edu.cn
 * updates: centos.ustc.edu.cn
软件包 1:mariadb-5.5.56-2.el7.x86_64 已安装并且是最新版本
正在解决依赖关系
--> 正在检查事务
---> 软件包 mariadb-server.x86_64.1.5.5.56-2.el7 将被 安装
--> 正在处理依赖关系 perl-DBI,它被软件包 1:mariadb-server-5.5.56-2.el7.x86_64 需要
--> 正在处理依赖关系 perl-DBD-MySQL,它被软件包 1:mariadb-server-5.5.56-2.el7.x86_64 需要
--> 正在处理依赖关系 perl(Data::Dumper),它被软件包 1:mariadb-server-5.5.56-2.el7.x86_64 需要
--> 正在处理依赖关系 perl(DBI),它被软件包 1:mariadb-server-5.5.56-2.el7.x86_64 需要
--> 正在检查事务
---> 软件包 perl-DBD-MySQL.x86_64.0.4.023-5.el7 将被 安装
---> 软件包 perl-DBI.x86_64.0.1.627-4.el7 将被 安装
--> 正在处理依赖关系 perl(RPC::PlServer) >= 0.2001,它被软件包 perl-DBI-1.627-4.el7.x86_64 需要
--> 正在处理依赖关系 perl(RPC::PlClient) >= 0.2000,它被软件包 perl-DBI-1.627-4.el7.x86_64 需要
---> 软件包 perl-Data-Dumper.x86_64.0.2.145-3.el7 将被 安装
--> 正在检查事务
---> 软件包 perl-PlRPC.noarch.0.0.2020-14.el7 将被 安装
--> 正在处理依赖关系 perl(Net::Daemon) >= 0.13,它被软件包 perl-PlRPC-0.2020-14.el7.noarch 需要
--> 正在处理依赖关系 perl(Net::Daemon::Test),它被软件包 perl-PlRPC-0.2020-14.el7.noarch 需要
--> 正在处理依赖关系 perl(Net::Daemon::Log),它被软件包 perl-PlRPC-0.2020-14.el7.noarch 需要
--> 正在处理依赖关系 perl(Compress::Zlib),它被软件包 perl-PlRPC-0.2020-14.el7.noarch 需要
--> 正在检查事务
---> 软件包 perl-IO-Compress.noarch.0.2.061-2.el7 将被 安装
--> 正在处理依赖关系 perl(Compress::Raw::Zlib) >= 2.061,它被软件包 perl-IO-Compress-2.061-2.el7.noarch 需要
--> 正在处理依赖关系 perl(Compress::Raw::Bzip2) >= 2.061,它被软件包 perl-IO-Compress-2.061-2.el7.noarch 需要
---> 软件包 perl-Net-Daemon.noarch.0.0.48-5.el7 将被 安装
--> 正在检查事务
---> 软件包 perl-Compress-Raw-Bzip2.x86_64.0.2.061-3.el7 将被 安装
---> 软件包 perl-Compress-Raw-Zlib.x86_64.1.2.061-4.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

=================================================================================================================
 Package                            架构                   版本                      源                      大小
=================================================================================================================
正在安装:
 mariadb-server                      x86_64                1:5.5.56-2.el7            base                    11 M
为依赖而安装:
 perl-Compress-Raw-Bzip2             x86_64                2.061-3.el7               base                    32 k
 perl-Compress-Raw-Zlib              x86_64                1:2.061-4.el7             base                    57 k
 perl-DBD-MySQL                      x86_64                4.023-5.el7               base                   140 k
 perl-DBI                            x86_64                1.627-4.el7               base                   802 k
 perl-Data-Dumper                    x86_64                2.145-3.el7               base                    47 k
 perl-IO-Compress                    noarch                2.061-2.el7               base                   260 k
 perl-Net-Daemon                     noarch                0.48-5.el7                base                    51 k
 perl-PlRPC                          noarch                0.2020-14.el7             base                    36 k

事务概要
==================================================================================================================
安装  1 软件包 (+8 依赖软件包)

总下载量:13 M
安装大小:62 M
Downloading packages:
(1/9): perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64.rpm                                          |  32 kB  00:00:00     
(2/9): perl-Data-Dumper-2.145-3.el7.x86_64.rpm                                                 |  47 kB  00:00:00     
(3/9): perl-Compress-Raw-Zlib-2.061-4.el7.x86_64.rpm                                           |  57 kB  00:00:00     
(4/9): perl-IO-Compress-2.061-2.el7.noarch.rpm                                                 | 260 kB  00:00:00     
(5/9): perl-Net-Daemon-0.48-5.el7.noarch.rpm                                                   |  51 kB  00:00:00     
(6/9): perl-DBD-MySQL-4.023-5.el7.x86_64.rpm                                                   | 140 kB  00:00:00     
(7/9): perl-PlRPC-0.2020-14.el7.noarch.rpm                                                     |  36 kB  00:00:00     
(8/9): mariadb-server-5.5.56-2.el7.x86_64.rpm                                                  |  11 MB  00:00:01     
(9/9): perl-DBI-1.627-4.el7.x86_64.rpm                                                         | 802 kB  00:00:01     
-------------------------------------------------------------------------------------------------------------------
总计                                                                                  9.0 MB/s |  13 MB  00:00:01     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : perl-Data-Dumper-2.145-3.el7.x86_64                                                  1/9 
  正在安装    : perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64                                           2/9 
  正在安装    : 1:perl-Compress-Raw-Zlib-2.061-4.el7.x86_64                                          3/9 
  正在安装    : perl-IO-Compress-2.061-2.el7.noarch                                                  4/9 
  正在安装    : perl-Net-Daemon-0.48-5.el7.noarch                                                    5/9 
  正在安装    : perl-PlRPC-0.2020-14.el7.noarch                                                      6/9 
  正在安装    : perl-DBI-1.627-4.el7.x86_64                                                          7/9 
  正在安装    : perl-DBD-MySQL-4.023-5.el7.x86_64                                                    8/9 
  正在安装    : 1:mariadb-server-5.5.56-2.el7.x86_64                                                 9/9 
  验证中      : perl-DBI-1.627-4.el7.x86_64                                                          1/9 
  验证中      : perl-Net-Daemon-0.48-5.el7.noarch                                                    2/9 
  验证中      : perl-Data-Dumper-2.145-3.el7.x86_64                                                  3/9 
  验证中      : perl-PlRPC-0.2020-14.el7.noarch                                                      4/9 
  验证中      : 1:perl-Compress-Raw-Zlib-2.061-4.el7.x86_64                                          5/9 
  验证中      : perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64                                           6/9 
  验证中      : 1:mariadb-server-5.5.56-2.el7.x86_64                                                 7/9 
  验证中      : perl-IO-Compress-2.061-2.el7.noarch                                                  8/9 
  验证中      : perl-DBD-MySQL-4.023-5.el7.x86_64                                                    9/9 

已安装:
  mariadb-server.x86_64 1:5.5.56-2.el7                                                                                               

作为依赖被安装:
  perl-Compress-Raw-Bzip2.x86_64 0:2.061-3.el7  perl-Compress-Raw-Zlib.x86_64 1:2.061-4.el7  
  perl-DBD-MySQL.x86_64 0:4.023-5.el7           perl-DBI.x86_64 0:1.627-4.el7                 
  perl-Data-Dumper.x86_64 0:2.145-3.el7         perl-IO-Compress.noarch 0:2.061-2.el7 
  perl-Net-Daemon.noarch 0:0.48-5.el7           perl-PlRPC.noarch 0:0.2020-14.el7           

完毕!
[root@hexingxing]# systemctl start mariadb # 安装完成MariaDB,首先启动MariaDB
[root@hexingxing]# systemctl enable mariadb # 设置开机启动
[root@hexingxing]# mysql_secure_installation # 进行MariaDB的相关简单配置

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): # 初次运行直接回车
OK, successfully used password, moving on...

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

Set root password? [Y/n] y # 是否设置root用户密码,输入y并回车或直接回车
New password: # 设置root用户的密码
Re-enter new password: # 再输入一次你设置的密码
Password updated successfully!
Reloading privilege tables..
 ... Success!


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 # 是否禁止root远程登录, 回车
 ... 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 # 是否删除test数据库,回车
 - 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!
[root@hexingxing]# mysql -uroot -p # 测试登录
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 14
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

CentOS 6.8 配置 LNMP(Linux+Nginx+PHP5+MySQL)

1、使用 yum 直接安装 Nginx

[root@hexingxing ~]#  yum install -y nginx
已加载插件:fastestmirror, refresh-packagekit, security
设置安装进程
Determining fastest mirrors
* base: mirrors.163.com
* extras: mirrors.163.com
* updates: mirrors.nwsuaf.edu.cn
No package nginx available.
错误:无须任何处理

## 系统不能直接 yum install nginx,判断可能 yum 源有问题

2、下载 Nginx 源

[root@hexingxing html]# rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
Retrieving http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
warning: /var/tmp/rpm-tmp.KcjpHW: Header V4 RSA/SHA1 Signature, key ID 7bd9bf62: NOKEY
Preparing...                ########################################### [100%]
1:nginx-release-centos   ########################################### [100%]

##下载成功

3、查看 Nginx 源是否下载成功

[root@hexingxing ~]# ls /etc/yum.repos.d/
CentOS-Base.repo  CentOS-Debuginfo.repo  CentOS-fasttrack.repo  CentOS-Media.repo  CentOS-Vault.repo  nginx.repo

## 在/etc/yum.repos.d/ 存在 nginx.repo 文件,那么可以安装了,为了确认是否可安装,先查看下 Nginx 的软件信息

4、查看 Nginx 软件信息

[root@hexingxing html]# yum info nginx
已加载插件:fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
* base: mirror.bit.edu.cn
* extras: ftp.sjtu.edu.cn
* updates: centos.ustc.edu.cn
nginx                                                                                                                                                     | 2.9 kB     00:00
nginx/primary_db                                                                                                                                          |  15 kB     00:00
可安装的软件包
Name        : nginx
Arch        : x86_64
Version     : 1.10.1
Release     : 1.el6.ngx
Size        : 821 k
Repo        : nginx
Summary     : High performance web server
URL         : http://nginx.org/
License     : 2-clause BSD-like license
Description : nginx [engine x] is an HTTP and reverse proxy server, as well as
: a mail proxy server.

## 可以获取到 Nginx 的软件信息,说明已经可以安装了

5、安装 Nginx

[root@hexingxing ~]# yum install nginx
已加载插件:fastestmirror, refresh-packagekit, security
设置安装进程
Loading mirror speeds from cached hostfile
* base: mirror.bit.edu.cn
* extras: ftp.sjtu.edu.cn
* updates: centos.ustc.edu.cn
解决依赖关系
--> 执行事务检查
---> Package nginx.x86_64 0:1.10.1-1.el6.ngx will be 安装
--> 完成依赖关系计算

依赖关系解决

=================================================================================================================================================================================
软件包                                 架构                                    版本                                                仓库                                    大小
=================================================================================================================================================================================
正在安装:
nginx                                  x86_64                                  1.10.1-1.el6.ngx                                    nginx                                  821 k

事务概要
=================================================================================================================================================================================
Install       1 Package(s)

总下载量:821 k
Installed size: 2.1 M
确定吗?[y/N]:y
下载软件包:
nginx-1.10.1-1.el6.ngx.x86_64.rpm                                                                                                                         | 821 kB     00:23
运行 rpm_check_debug
执行事务测试
事务测试成功
执行事务
Warning: RPMDB altered outside of yum.
正在安装   : nginx-1.10.1-1.el6.ngx.x86_64                                                                                                                                 1/1
----------------------------------------------------------------------

Thanks for using nginx!

Please find the official documentation for nginx here:
* http://nginx.org/en/docs/

Commercial subscriptions for nginx are available on:
* http://nginx.com/products/

----------------------------------------------------------------------
Verifying  : nginx-1.10.1-1.el6.ngx.x86_64                                                                                                                                 1/1

已安装:
nginx.x86_64 0:1.10.1-1.el6.ngx

完毕!

##Nginx 安装完成

6、启动 Nginx

[root@hexingxing ~]# service nginx start
正在启动 nginx:nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
[失败]

##发现启动 Nginx 失败,根据以上错误信息,我发现可能是因为之前已安装并启动了 Apache,可能因此而报错,所以我现在停止 Apache 服务

6、停止 Apache

[root@hexingxing ~]# service httpd stop
停止 httpd:                                               [确定]

##停止 Apache

7、启动 Nginx

[root@hexingxing ~]# service nginx start
正在启动 nginx:                                           [确定]

##启动 Nginx 成功

8、访问服务器 IP

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.

##访问服务器 IP 出现以上提示,说明 Nginx 已经安装成功并且可用,Nginx 的 Web 默认目录为 /usr/share/nginx/html/ ,可在此目录下放置网站文件,安装了 PHP 后可运行 PHP 网站。

 

到此,Nginx 已经安装成功。如果 Nginx 的 Web 页面不能访问可能还需要配置服务器的防火墙,配置防火墙和继续安装 MySQL 和 PHP ,可以参考何星星之前写的一篇《CentOS 6.8 配置 LAMP(Linux+Apache+PHP+MySQL)环境》。

 

CentOS 6.8 配置 LAMP(Linux+Apache+PHP+MySQL)环境

前期准备

1、配置防火墙,开启 80 端口、3306 端口

[root@hexingxing ~]# vi /etc/sysconfig/iptables #编辑防火墙规则

 

将以下两条记录添加到默认的 22 端口这条规则的下面

-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT #允许 80 端口通过防火墙
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT #允许 3306 端口通过防火墙
[root@hexingxing ~]# cat /etc/sysconfig/iptables #检查配置是否正确,正常的配置如下:

########################################################
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
########################################################
[root@hexingxing ~]# /etc/init.d/iptables restart #重启防火墙使配置生效

 

2、关闭 SELINUX

[root@hexingxing ~]# vi /etc/selinux/config
#SELINUX=enforcing #注释掉此行
#SELINUXTYPE=targeted #注释掉此行
SELINUX=disabled #增加此记录
[root@hexingxing ~]# :wq #保存退出
[root@hexingxing ~]# shutdown -r now #重启

 

安装服务

一、安装 Apache

[root@hexingxing ~]# yum install httpd #安装 Apache 服务
[root@hexingxing ~]# /etc/init.d/httpd start #启动 Apache

备注:Apache 启动之后如果提示以下错误:

正在启动 httpd:httpd: Could not reliably determine the server's fully qualif domain name, using ::1 for ServerName

解决方法:

[root@hexingxing ~]# vi /etc/httpd/conf/httpd.conf #编辑 Apache 配置

找到 #ServerName www.example.com:80
修改为 ServerName localhost:80 #这里设置为你自己的域名,如果没有域名,可以设置为 localhost
[root@hexingxing ~]# :wq #保存退出

[root@hexingxing ~]# chkconfig httpd on #设为开机启动
[root@hexingxing ~]# /etc/init.d/httpd restart #重启 Apache

  

测试服务

在客户端浏览器输入服务器 IP 地址,如果配置正确,可以看到 Apache 相关的配置信息

 

二、安装 MySQL
1、安装 MySQL

[root@hexingxing ~]# yum install mysql mysql-server #安装 MySQL 服务
[root@hexingxing ~]# /etc/init.d/mysqld start #启动 MySQL
[root@hexingxing ~]# chkconfig mysqld on #设为开机启动
[root@hexingxing ~]# cp /usr/share/mysql/my-medium.cnf /etc/my.cnf #拷贝配置文件

 

2、为 root 账户设置密码

[root@hexingxing ~]# mysql_secure_installation  #设置 MySQL 密码
 
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we'll need the current
password for the root user. If you've just installed MySQL, 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):  #初次配置无密码可直接回车,有密码输入密码回车
OK, successfully used password, moving on…
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
Set root password? [Y/n]   #是否设置 root 用户密码,输入 y 并回车或直接回车
New password:   #设置 root 用户的密码
Re-enter new password:   #再输入一次你设置的密码
Password updated successfully!
Reloading privilege tables..
… Success!

By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL 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]   #是否删除匿名用户, 生产环境建议删除,所以直接回车
… 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]   #是否禁止 root 远程登录, 根据自己的需求选择 Y/n 并回车, 建议禁止
… Success!
 
By default, MySQL 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]   #是否删除 test 数据库, 直接回车
- 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]   #是否重新加载权限表,直接回车
… Success!
 
Cleaning up…

All done! If you've completed all of the above steps, your MySQL
installation should now be secure.
 
Thanks for using MySQL!   #最后看到这个提示说明已经配置成功

// 安装 MySQL 简单的记忆:首次配置出现 Enter current password for root (enter for none): 时,无密码可直接回车,后续配置如果有密码输入密码回车;然后根据提示输入 Y,首次配置需要相输入密码和确认密码,第二次即提示是否变换密码;输入 2 次密码,回车;根据提示一直输入 Y;最后出现:Thanks for using MySQL! 即表示成功。

MySql 密码设置完成,重新启动 MySQL:

[root@hexingxing ~]# /etc/init.d/mysqld restart #重启
[root@hexingxing ~]# /etc/init.d/mysqld stop #停止
[root@hexingxing ~]# /etc/init.d/mysqld start #启动

 

三、安装 PHP
1、安装 PHP

[root@hexingxing ~]# yum install php #安装 PHP 服务

 

2、安装 PHP 组件,使 PHP 支持 MySQL

[root@hexingxing ~]# yum install php-mysql php-gd libjpeg* php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-bcmath php-mhash libmcrypt #安装 PHP 组件
[root@hexingxing ~]# /etc/init.d/mysqld restart #重启 MySql
[root@hexingxing ~]# /etc/init.d/httpd restart #重启 Apache

 

测试服务

[root@hexingxing ~]# cd /var/www/html
[root@hexingxing ~]# vi index.php #编辑一个 phpinfo 的测试页面内容
[root@hexingxing ~]# :wq #保存退出

在客户端浏览器输入服务器 IP 地址,可以看到 PHP 相关的配置信息

调整配置

一、Apache 配置

[root@hexingxing ~]# vi /etc/httpd/conf/httpd.conf #编辑 Apache 配置文件
ServerTokens OS #在 44 行 修改为:ServerTokens Prod(在出现错误页的时候不显示服务器操作系统的名称)
ServerSignature On #在 536 行 修改为:ServerSignature Off(在错误页中不显示 Apache 的版本)
Options Indexes FollowSymLinks #在 331 行 修改为:Options Includes ExecCGI FollowSymLinks(允许服务器执行 CGI 及 SSI,禁止列出目录)
#AddHandler cgi-script .cgi #在 796 行 修改为:AddHandler cgi-script .cgi .pl(允许扩展名为.pl 的 CGI 脚本运行)
AllowOverride None #在 338 行 修改为:AllowOverride All(允许.htaccess)
AddDefaultCharset UTF-8 #在 759 行 修改为:AddDefaultCharset GB2312 (添加 GB2312 为默认编码)
Options Indexes MultiViews FollowSymLinks #在 554 行 修改为 Options MultiViews FollowSymLinks(不在浏览器上显示树状目录结构)
DirectoryIndex index.html index.html.var #在 402 行 修改为:DirectoryIndex index.html index.htm Default.html Default.htm index.php Default.php index.html.var #设置默认首页文件,增加 index.php
KeepAlive Off #在 76 行 修改为:KeepAlive On(允许程序性联机)
MaxKeepAliveRequests 100 #在 83 行 修改为:MaxKeepAliveRequests 1000(增加同时连接数)
[root@hexingxing ~]# :wq #保存退出
[root@hexingxing ~]# /etc/init.d/httpd restart #重启
[root@hexingxing ~]# rm -f /etc/httpd/conf.d/welcome.conf /var/www/error/noindex.html #删除默认测试页

二、PHP 配置

[root@hexingxing ~]# vi /etc/php.ini #编辑
date.timezone = PRC #在 946 行 把前面的分号去掉,改为 date.timezone = PRC
disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd, posix_getegid,posix_geteuid,posix_getgid, posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid, posix_getppid,posix_getpwnam,posix_getpwuid, posix_getrlimit, posix_getsid,posix_getuid,posix_isatty, posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid, posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname
#在 386 行 列出 PHP 可以禁用的函数,如果某些程序需要用到这个函数,可以删除,取消禁用。
expose_php = Off #在 432 行 禁止显示 php 版本的信息
magic_quotes_gpc = On #在 745 行 打开 magic_quotes_gpc 来防止 SQL 注入
short_open_tag = ON #在 229 行支持 php 短标签
open_basedir = .:/tmp/ #在 380 行 设置表示允许访问当前目录 (即 PHP 脚本文件所在之目录) 和/tmp/目录, 可以防止 php 木马跨站, 如果改了之后安装程序有问题,可以注销此行,或者直接写上程序的目录/data/www.osyunwei.com/:/tmp/
[root@hexingxing ~]# :wq #保存退出
[root@hexingxing ~]# /etc/init.d/mysqld restart #重启 MySql
[root@hexingxing ~]# /etc/init.d/httpd restart #重启 Apche

PS1:
Apache 解析 PHP,修改 httpd.conf 文件,添加
Addtype application/x-httpd-php .php .phtml
Addtype application/x-httpd-php-source .phps
PS2:
Apache 默认的程序目录是 /var/www/html

测试网站服务页面

在 /var/www/html 目录下新建 html 或 php 文件测试是否可访问