加入收藏 | 设为首页 | 会员中心 | 我要投稿 聊城站长网 (https://www.0635zz.com/)- 智能语音交互、行业智能、AI应用、云计算、5G!
当前位置: 首页 > 服务器 > 搭建环境 > Linux > 正文

教你如何在Linux安装LNMP并且把wordpress跑起来的

发布时间:2023-07-08 17:03:02 所属栏目:Linux 来源:
导读:这篇文章给大家分享的是“我是如何在Linux安装LNMP并且把wordpress跑起来的”,对大家学习和理解有一定的参考价值和帮助,有这方面学习需要的朋友,接下来就跟随小编一起学习一下吧。

第1章 安装Nginx
这篇文章给大家分享的是“我是如何在Linux安装LNMP并且把wordpress跑起来的”,对大家学习和理解有一定的参考价值和帮助,有这方面学习需要的朋友,接下来就跟随小编一起学习一下吧。
 
第1章 安装Nginx
 
1.1 Nginx官网
 
nginx news
 
http://nginx.org/
 
1.2 安装nginx
 
1.2.1 安装Nginx所需的pcre库
 
作用:实现伪静态的功能
 
yum install pcre pcre-devel -y
 
1.2.2 安装编译依赖包:
 
yum install gcc gcc-devel -y
 
yum install openssl openssl-devel -y
 
1.2.3 下载源码包:
 
wget -q http://nginx.org/download/nginx-1.6.3.tar.gz
 
参数:-q 下载不提示。
 
1.2.4 解压
 
tar xf nginx-1.6.3.tar.gz
 
cd nginx-1.6.3
 
1.2.5 查询yum仓库有没有rpm包
 
yum list |grep nginx 或yum list *nginx*
 
1.2.6 添加系统用户:
 
useradd www -s /sbin/nologin -M
 
1.2.7 开始编译安装nginx
 
1.2.7.1  配置编译参数
 
./configure --user=www --group=www --prefix=/application/nginx-1.6.3/ --with-http_stub_status_module --with-http_ssl_module
 
1.2.7.2  检查配置返回值
 
echo $?    #返回值为0 则正确
 
1.2.7.3  编译&安装
 
make
 
make install
 
1.2.7.4  配置软链接,方便以后升级版本。开发不用修改程序
 
ln -s /application/nginx-1.6.3/ /application/nginx
 
1.2.7.5  nginx启动前的检查语法
 
[root@web01 conf]# /application/nginx/sbin/nginx -t
 
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
 
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
 
1.2.7.6  启动nginx服务
 
/application/nginx/sbin/nginx
 
1.2.7.7  启动服务后查看监听端口
 
netstat -lntup|grep 80
 
lsof -i :80
 
1.2.7.8  测试结果:
 
windows下连接输入http://10.0.0.8
 
linux下可以用 wget 127.0.0.1显示200K就是正常的。
 
 
[root@web01 conf]# wget 127.0.0.1
 
--2016-08-26 03:28:39--  http://127.0.0.1/
 
Connecting to 127.0.0.1:80... connected.
 
HTTP request sent, awaiting response... 200 OK
 
Length: 4 [text/html]
 
Saving to: “index.html”
 
 
 
100%[======================================>] 4           --.-K/s   in 0s     
 
 
 
2016-08-26 03:28:39 (630 KB/s) - “index.html” saved [4/4]
 
1.2.7.9  查看nginx的版本
 
[root@web01 ~]# ls /application/
 
nginx  nginx-1.6.3
 
[root@web01 ~]# /application/nginx/sbin/nginx  -V
 
nginx version: nginx/1.6.3
 
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC)
 
TLS SNI support enabled
 
configure arguments: --user=www --group=www --prefix=/application/nginx-1.6.3/ --with-http_stub_status_module --with-http_ssl_module
 
1.2.7.10    查看错误日志
 
cat /application/nginx/logs/error.log
 
1.2.7.11    配置nginx配置文件
 
egrep -v "^$|#" nginx.conf.default >nginx.conf
 
1.2.7.12    配置文件内容说明
 
[root@web01 conf]# vim nginx.conf
 
worker_processes  1;
 
events {
 
    worker_connections  1024;
 
}
 
http {
 
    include       mime.types;
 
    default_type  application/octet-stream;
 
    sendfile        on;
 
    keepalive_timeout  65;
 
    server {
 
        listen       80;  #可改的监听端口
 
        server_name  www.chrishg.net;  #可改为的域名
 
autoindex on; #允许客户端浏览我的目录
 
        location / {
 
            root   html/www;  #对应的目录
 
            index  index.html index.htm;
 
        }
 
}
 
1.2.7.13    添加虚拟主机
 
添加虚拟主机在http{ } 模块里增加下面代码:多虚拟主机就添加多组以下内容。
 
 
server {
 
        listen       80;
 
        server_name  bbs.chrishg.net;
 
        location / {
 
            root   html/bbs;
 
            index  index.html index.htm;
 
1.2.7.14     单网卡多IP
 
 server {
 
        listen       10.0.0.102:80;
 
        server_name  bbs.chrishg.net;
 
        location / {
 
            root   html/bbs;
 
            index  index.html index.htm;
 
1.2.7.15    多域名
 
server {
 
        listen       10.0.0.102:80;
 
        server_name  bbs.chrishg.net chrishg.net;
 
        location / {
 
            root   html/bbs;
 
            index  index.html index.htm;
 
1.2.7.16    添加状态监控页面
 
##status
 
server {
 
        listen       80;
 
        server_name  status.chrishg.net;
 
        location / {
 
          stub_status on;
 
          access_log off;
 
        }
 
    }
 
添加后,打开网页会显示如下:
 
 
Active connections: 2                  #活动的连接
 
server accepts handled requests       
 
 4 4 17                                #
 
Reading: 0 Writing: 1 Waiting: 1       #
 
1.2.7.17    检查配置文件语法
 
/application/nginx/sbin/nginx  -t
 
1.2.7.18    启动服务
 
/application/nginx/sbin/nginx
 
1.2.7.19    重启服务
 
/application/nginx/sbin/nginx  -s reload
 
1.2.7.20    关闭服务
 
/application/nginx/sbin/nginx  -s stop
 
1.3 企业中重启nginx后的策略
 
在企业运维实践场中,每一个配置操作处理完毕后都应该进行快速有效的检查,这是一个合格运维人员的良好习惯。尽量使得在Nginx启动的同时,还会调用脚本通过获取header信息或模拟用户访问指定URL(wget等方式)来自动检查Nginx是否正常,最大限度的保证服务重启后,能迅速确定网站情况,而无须手工敲命令查看。这样如果配置有问题就可以迅速使用上一版本备份配置文件覆盖回来。
 
[root@localhost conf]# cat check_url.sh
 
#!/bin/bash
 
#author:chenfu 2017-10-22 QQ532088799
 
#--------function split--------
 
. /etc/rc.d/init.d/functions
 
function checkURL()
 
{
 
checkUrl=$1
 
echo 'check url start ...'
 
judge=($(curl -I -s --connect-timeout 2 ${checkUrl}|head -1 | tr " " "\n"))
 
if [[ "${judge[1]}" == '200' && "${judge[2]}" == 'OK' ]]
 
then
 
action "${checkUrl}" /bin/true
 
else
 
action "${checkUrl}" /bin/false
 
echo -n "retrying again...";sleep 3;
 
judgeagain=($(curl -I -s --connect-timeout 2 ${checkUrl}| head -1| tr "\r" "\n"))
 
if [[ "${judgeagain[1]}" == '200' && "${judgeagain[2]}" == 'OK' ]]
 
then
 
action "${chekcUrl}, retried again" /bin/true
 
else
 
action "${chekcUrl}, retried again" /bin/false
 
fi
 
fi
 
sleep 1;
 
}
 
# usage method
 
checkURL http://www.etiantian.org
 
Nginx status结果详解
 
Active connections: 1         #Nginx正处理的活动连接数有1个
 
server accepts handled requests   #第一个server表示Nginx启动到现在共处理20个连接
 
#第二个accepts表示Nginx启动到现在共成功创建20次握                                                                #手
 
#第三个handled requests表示共处理了23次请求
 
20 20 23
 
Reading: 0 Writing: 1 Waiting: 0    #Reading为Nginx读取到客户端的Header信息数
 
#Writing为Nginx返回给客户端的Header数
 
#Waiting为Nginx已经处理完正等候下一次请求指令的驻                                                                  #留连接。在开启keepalive的情况下
 
#这个值等于active-(reading+writing)
 
Nginx增加错误日志(error_log)配置
 
日志记录属于核心功能模块(ngx_core_module)的参数,该参数名字是error.log,针对自己来说最好定义在Main全局区块中,当然也可以放置不同的虚拟主机中单独记录。
 
error_log    file    level[debug|info|notice|warn|error|crit|alert|emerg];
 
关键字    日志文件  错误日志级别
 
error_log默认配置:
 
#default:error_log logs/error.log error;
 
可以放置的标签段:
 
#context:main,http,server,location
 
Nginx访问日志(access_log)
 
此功能由ngx_http_log_module模块负责
 
控制日志的参数
 
参数 说明
 
log_format 用来定义记录日志的格式(可以定义多种日志格式,取不同的名字即可)
 
access_log 用来指定日志文件的路径及使用何种日志格式记录日志Nginx日志变量说明
 
Nginx日志变量 说明
 
$remote_addr 记录访问网站的客户端地址
 
$http_x_forwarded_for 当前端有代理服务器时,设置Web节点记录客户端地址的配置,此参数生效的前提是代理服务器上也进行了相关的x_forwarded_for设置
 
$remote_user 远程客户端名称
 
$time_local 记录访问时间与时区
 
$request 用户的http请求起始行信息
 
$status http状态码,记录请求返回的状态,例如200,404,301
 
$body_bytes_sents 服务器发送给客户端的响应body字节数
 
$http_referer 记录此次请求是从哪个链接访问过来的,可以根据referer进行防盗链设置
 
$http_user_agent 记录客户端访问信息,例如浏览器手机客户端等
 
 
 
buffer=size            #存放访问日志的缓冲区大小
 
flush=time             #将缓冲区的日志刷到磁盘的时间
 
gzip[=level]              #表示压缩级别
 
[if=condition]           #表示其他条件(一般的场景中这些都无需配置,极端优化                                                              #时才可能会考虑这些参数)
 
默认的配置:
 
access_log    logs/access.log     combined;
 
日志内容配置:
 
log_format main '$remote_addr - $remote_user [$time_local] "$request"'
 
'$status $body_bytes_sent "$http_referer" '
 
'"$http_user_agent" "$http_x_forwarded_for"';
 
Nginx访问日志轮询切割
 
[root@localhost nginx]# cat /server/script/cut_nginx_log.sh
 
#!/bin/bash
 
Dateformat=`date +%Y%m%d`
 
Basedir="/application/nginx"
 
Nginxlogdir="$Basedir/logs"
 
Logname="access_www"
 
[ -d $Nginxlogdir ] && cd $Nginxlogdir || exit 1
 
[ -f ${Logname}.log ] || exit 1
 
/bin/mv ${Logname}.log ${Dateformat}_${Logname}.log
 
$Basedir/sbin/nginx -s reload
 
Nginx常用的日志收集分析工具有rsyslog、awstats、flume、ELK、storm等
 
1.4 yum安装nginx的方法
 
wget -q http://mirrors.utc.edu.cn/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm
 
rpm -ivh epel-release-6-8.narch.rpm
 
yum install nginx
 
亿级PV超大型网站集群架构图形化深度揭秘讲解 - CSDN学院 - CSDN.NET
 
http://edu.csdn.net/course/detail/2278
 
 
 
第2章 mysql二进制包的安装
 
2.1 安装包下载:
 
官方地址:http://dev.mysql.com/downloads/mysql/5.5.html#downloads
 
cd /home/oldboy/tools
 
wget http://cdn.mysql.com/archives/mysql-5.5/mysql-5.5.49-linux2.6-x86_64.tar.gz
 
2.2 解压并mv到统一目录 /application/
 
cd /home/oldboy/tools
 
tar xf mysql-5.5.49-linux2.6-x86_64.tar.gz
 
mv mysql-5.5.49-linux2.6-x86_64 /application/mysql-5.5.49
 
ln -s /application/mysql-5.5.49 /application/mysql
 
ls /application/
 
2.3 创建mysql用户及目录
 
mkdir /application/
 
useradd -s /sbin/nologin mysql -M
 
id mysql
 
2.4 mysql目录授权
 
chown -R mysql.mysql /application/mysql/data/
 
2.5 初始化mysql(如生成内部用户、库 等)
 
cd /application/mysql/
 
./scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/application/mysql/data/ --user=mysql
 
ll data/
 
2.6 主机名要做解释
 
在cat /etc/hosts 文件里添加解释:
 
172.16.1.8      web01
 
2.7 修改启动文件里的路径并复制到/etc/init.d/目录下并授权
 
\cp support-files/my-small.cnf /etc/my.cnf
 
cp support-files/mysql.server  /etc/init.d/mysqld
 
sed -i 's#/usr/local/#/application/#g' /application/mysql/bin/mysqld_safe /etc/init.d/mysqld
 
chmod +x /etc/init.d/mysqld
 
2.8 启动数据库并查看端口
 
/application/mysql/bin/mysqld_safe --user=mysql &
 
lsof -i :3306
 
netstat -lntup |grep 3306
 
2.9 默认登录mysql
 
 [root@web01 bin]# /application/mysql/bin/mysql
 
Welcome to the MySQL monitor.  Commands end with ; or \g.
 
Your MySQL connection id is 1
 
Server version: 5.5.49 MySQL Community Server (GPL)
 
 
 
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
 
 
 
Oracle is a registered trademark of Oracle Corporation and/or its
 
affiliates. Other names may be trademarks of their respective
 
owners.
 
 
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
 
 
mysql>            #这样代表成功安装了
 
2.10 设置环境变量
 
PATH="/application/mysql/bin/:$PATH"
 
[root@web01 bin]# echo $PATH
 
/application/mysql/bin/:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
 
[root@web01 bin]# which mysql
 
/application/mysql/bin/mysql
 
编辑vim /etc/profile
 
添加PATH="/application/mysql/bin/:$PATH" 到最后,实现环境变量永久生效
 
. /etc/profile    #添加完后重读配置文件。
 
2.11 实现stop|start|restart|开机自启动
 
/etc/init.d/mysqld stop
 
/etc/init.d/mysqld start
 
/etc/init.d/mysqld restart
 
chkconfig --add mysqld
 
chkconfig --list mysql
 
chkconfig mysql on
 
2.12 查看错误日志
 
/application/mysql/data/ 机器名.err   如:
 
cat /application/mysql/data/web01.err
 
2.12.1 故障点
 
2.12.1.1    /tmp目录权限必须为以下权限(1777)
 
[root@web01 mysql]# ls -ld /tmp
 
drwxrwxrwt. 4 root root 4096 Aug 30 01:22 /tmp
 
2.13 错误排查
 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
 
上面问题有3种可能原因及其解决方法如下:
 
1、删除并重新初始化数据库,出现此问题一般都是因为数据库初始货有问题,
 
2、或者是MySQL数据文件损坏了,也可能是MySQL数据目录权限有问题。
 
3、检查主机名对应主机IP解析是否正确。
 
MySQL安装完成后,默认情况下,管理员账号root是无密码的,这个必须设置。
 
show databases;      #查看当前所有数据库
 
select user();       #查看当前的登录用户
 
2.14 数据库的一些操作:
 
2.14.1 设置密码、登陆、改密码
 
mysqladmin -uroot password "oldboy123"  #设置密码
 
mysql -uroot -poldboy123               #用密码登陆
 
mysqladmin -uroot -poldboy123 password 123456   #改密码
 
2.14.2 清空history
 
history -c  #清空全部历史记录
 
history -d 1024  #清除第1024条历史记录
 
2.14.3 mysql 查询语句
 
show databases; 查看所有数据库
 
第3章 安装PHP服务
 
3.1 PHP(FastCGI方式)服务的安装准备
 
3.1.1 检查Nginx 及Mysql的安装情况
 
ls -ld /application/mysql/
 
ls -ld /application/nginx
 
3.1.2 检查端口及启动情况
 
[root@web01 ~]# netstat -lntup |egrep "3306|80"
 
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      51971/mysqld       
 
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      50047/nginx
 
3.1.3 测试Nginx及Mysql是否OK
 
wget 127.0.0.1
 
mysql -uroot -p
 
3.1.4 检查安装PHP所需的lib库
 
rpm -qa zlib-devel libxm12-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel freetype-devel libping-devel gd-devel libcurl-devel libxslt-devel
 
3.1.5 安装基础库
 
yum install -y zlib-devel libxm12-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel freetype-devel libping-devel gd-devel libcurl-devel libxslt-devel
 
3.1.6 安装yum无法安装的 libiconv库
 
cd /home/oldboy/tools
 
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
 
tar -zxvf libiconv-1.14.tar.gz
 
cd libiconv-1.14
 
./configure --prefix=/usr/local/libiconv
 
make
 
make install
 
cd ../
 
3.1.7 安装libmcrypt 库
 
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
 
yum install libmcrypt-devel -y
 
3.1.8 安装mhash 和mcrypt加密扩展库
 
yum install -y mhash mcrypt
 
3.2 安装PHP服务
 
3.2.1 下载、解压、配置PHP
 
cd /home/oldboy/tools
 
wget http://mirrors.sohu.com/php/php-5.5.32.tar.gz
 
tar xf php-5.5.32.tar.gz
 
cd php-5.5.32
 
#配置参数
 
./configure --prefix=/application/php-5.5.32 --with-mysql=/application/mysql/ --with-pdo-mysql=mysqlnd --with-iconv-dir=/usr/local/libiconv --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-soap --enable-short-tags --enable-static --with-xsl --with-fpm-user=nginx --with-fpm-group=nginx --enable-ftp --enable-opcache=no
 
echo $?
 
3.2.2 编译安装
 
3.2.2.1  防止编译报错。
 
方法一: (5.3版本有共享库的报错)
 
ln -s /application/mysql/lib/libmysqlclient.so.18 /usr/lib64/
 
方法二:找出这个文件将路径添加到 /etc/ld.so.conf
 
find /  -name "libmysqlclient.so.18"
 
/application/mysql-5.5.49-linux2.6-x86_64/lib/libmysqlclient.so.18
 
vim /etc/ld.so.conf
 
添加内容如下:
 
/application/mysql/lib/
 
然后用以下命令保存:
 
ldconfig
 
3.2.2.2  防止make报错先创建以下文件
 
touch ext/phar/phar.phar
 
如果在PHP编译时使用--with-mysql=mysqlnd替代--with-mysql=/application/mysql则没有上述2点的错误发生。
 
make    #说明如果在物理机多核可以用make -j 8  指定核心数
 
make install
 
3.2.3 配置PHP引擎配置文件php.ini
 
1、设置软链接以方便访问。
 
ln -s /application/php-5.5.32/ /application/php
 
ls /application/php/
 
2、查看PHP配置默认模板文件。
 
[root@web01 php-5.5.32]# ls -l php.*
 
-rw-r--r-- 1 1001 1001  2523 Feb  2  2016 php.gif
 
-rw-r--r-- 1 1001 1001 69236 Feb  2  2016 php.ini-development
 
-rw-r--r-- 1 1001 1001 69266 Feb  2  2016 php.ini-production
 
3、拷贝PHP配置文件到PHP默认目录,并更改文件名为php.ini
 
 
 
cp php.ini-production  /application/php/lib/php.ini
 
ls -l /application/php/lib/php.ini
 
3.2.4 配置PHP服务(FastCGI方式)的配置文件php-fpm.conf
 
cd /application/php/etc/
 
cp php-fpm.conf.default php-fpm.conf
 
3.2.5 启动PHP服务(FastCGI方式)
 
/application/php/sbin/php-fpm
 
ps -ef |grep php-fpm
 
lsof -i :9000
 
3.2.6 make报错排查
 
3.2.6.1  make 结束有以下报错
 
Fatal error: Uncaught exception 'BadMethodCallException' with message 'Cannot set any files or directories in magic ".phar" directory' in /home/oldboy/tools/php-5.5.32/ext/phar/phar.php:1173
 
Stack trace:
 
#0 /home/oldboy/tools/php-5.5.32/ext/phar/phar.php(1173): Phar->offsetSet('.phar', '')
 
#1 /home/oldboy/tools/php-5.5.32/ext/phar/phar.php(1139): PharCommand::phar_add_file(Object(Phar), 0, '.phar', Object(SplFileInfo), NULL, true)
 
#2 /home/oldboy/tools/php-5.5.32/ext/phar/phar.php(1077): PharCommand::phar_add(Object(Phar), 0, '/home/oldboy/to...', NULL, '/\\.svn/', Object(SplFileInfo), NULL, true)
 
#3 [internal function]: PharCommand->cli_cmd_run_pack(Array)
 
#4 /home/oldboy/tools/php-5.5.32/ext/phar/phar.php(225): call_user_func(Array, Array)
 
#5 /home/oldboy/tools/php-5.5.32/ext/phar/phar.php(2089): CLICommand->__construct(19, Array)
 
#6 {main}
 
  thrown in /home/oldboy/tools/php-5.5.32/ext/phar/phar.php on line 1173
 
make: *** [ext/phar/phar.phar] Error 255
 
原因是上面1.2.2.2在touch ext/phar/phar.phar 时变成了touch ext/phar/phar/.phar
 
解决:
 
cd ext/phar/phar/  #进入目录中
 
mv .phar /tmp     #删除创建错的文件
 
touch ext/phar/phar.phar    #重新创建文件
 
make   #重新make,这次的时间很短
 
第4章 如何关联LNMP
 
4.1 第一步配置Nginx与PHP的关联
 
cd /application/nginx/conf/extra
 
vim blog.conf
 
server {
 
        listen       80;
 
        server_name  blog.chrishg.net;
 
        location / {
 
            root   html/blog;
 
            index  index.html index.htm;
 
        }
 
#添加以下这一段代码
 
        location ~ .*\.(php|php5)?$ {
 
            root   html/blog;
 
            fastcgi_pass  127.0.0.1:9000;
 
            fastcgi_index index.php;
 
            include fastcgi.conf;
 
        }
 
    }
 
/application/nginx/sbin/nginx -t
 
/application/nginx/sbin/nginx -s reload
 
4.1.1 测试nginx与PHP的配置是否有效(因为安全问题,测试完后要删除)
 
cd /application/nginx/html/blog/
 
echo "<?php phpinfo(); ?>" >test_info.php #添加测试内容注意();这里是分号。
 
[root@web01 blog]# cat test_info.php
 
<?php phpinfo(); ?>
 
在网页上打开测试
 
blog.chrishg.net/test_info.php
 
4.1.2 第二步:测试PHP与mysql是否连接成功
 
cd /application/nginx/html/blog/
 
在这个目录里代添测试文件
 
vim test_mysql.php
 
<?php
 
        $link_id=mysql_connect('localhost','root','oldboy123') or mysql_error();
 
        if($link_id){
 
                echo "mysql successful by oldboy !";
 
        }else{
 
                echo mysql_error();
 
        }
 
?>
 
4.1.3 在浏览器中打开后
 
blog.chrishg.net/test_mysql.php
 
4.1.4 数据库改密码
 
mysqladmin -uroot -p123456 password oldboy123
 
第5章 wordpress博客的搭建
 
5.1 首先建库
 
mysql -uroot -poldboy123    #登陆mysql
 
show databases;             #显示当前所有库
 
drop database test;         #删除没用的库
 
show databases;
 
create database wordpress;  #创建库
 
select user();              #查看当前用户
 
system whoami               #在mysql外查看当前用户是谁
 
select user,host from mysql.user;      #查询所有用户
 
+------+-----------+
 
| user | host      |
 
+------+-----------+
 
| root | 127.0.0.1 |
 
| root | ::1       |
 
|      | localhost |
 
| root | localhost |
 
|      | web01     |
 
| root | web01     |
 
+------+-----------+
 
6 rows in set (0.00 sec)
 
********************分隔线*****************************
 
grant all on wordpress.* to wordpress@'localhost' identified by '123456';
 
上行geant为授权 ,第一个wordpress为库名,后面的”*“为所有表,第二个wordpress为用户名,@后的localhost为授权登陆的IP 为本地,identified by (身份)后的‘123456’为密码
 
-----------------------------------------
 
drop user wordpress@'localhost'  #此句为删用户
 
----------------------------------------------------------------
 
select user,host from mysql.user;
 
show grants for wordpress@'localhost';    #查看用户权限
 
flush privileges;                         #修改后刷新
 
5.1.1 在nginxs配置文件里添加index.php
 
cd /application/nginx/conf/extra
 
vim blog.conf
 
增加下面黄色部份内容
 
    server {
 
        listen       80;
 
        server_name  blog.chrishg.net;
 
        location / {
 
            root   html/blog;
 
            index  index.php index.html index.htm;
 
        }
 
        location ~ .*\.(php|php5)?$ {
 
            root   html/blog;
 
            fastcgi_pass  127.0.0.1:9000;
 
            fastcgi_index index.php;
 
5.1.2 下载wordpress博客
 
cd /home/oldboy/tools
 
wget https://cn.wordpress.org/wordpress-4.5.3-zh_CN.tar.gz
 
tar xf wordpress-4.5.3-zh_CN.tar.gz
 
cp -a wordpress/* /application/nginx/html/blog/
 
chown -R www.www /application/nginx/html/blog/
 
5.1.3 hosts解释,浏览安装博客
 
浏览器输入网址:http://blog.chrishg.net
 
 
 
配置***
 
 
mysql -uwordpress -p123456
 
use wordpress;
 
show tables;
 
5.2 伪静态网址
 
5.2.1 在博客里添加如下:
 
/archives/%post_id%.html
 
5.2.2 nginx配置文件中添加如下
 
server {
 
        listen       80;
 
        server_name  blog.chrishg.net;
 
            root   html/blog;
 
            index  index.php index.html index.htm;
 
        location / {
 
        if (-f $request_filename/index.html){
 
            rewrite (.*) $1/index.html break;
 
         }
 
        if (-f $request_filename/index.php){
 
            rewrite (.*) $1/index.php;
 
         }
 
        if (!-f $request_filename){
 
            rewrite (.*) /index.php;
 
         }
 
        }
 
        location ~ .*\.(php|php5)?$ {
 
            root   html/blog;
 
            fastcgi_pass  127.0.0.1:9000;
 
            fastcgi_index index.php;
 
            include fastcgi.conf;
 
        }
 
}
 
 
 
注:以下这行,也可以实现以上多行的功能
 
 try files $url/  /index.php?q=$uri%args;
 
第6章 LNMP组件分离
 
我们这里的IP地址是:172.16.1.51  db01
 
6.1 数据库mysql的分离
 
6.1.1 一键安装mysql脚本 (在独立数据库安装好mysql)
 
#创建用户
 
useradd -s /sbin/nologin mysql -M
 
#创建目录、解压、授权
 
cd /home/oldboy/tools
 
tar xf mysql-5.5.49-linux2.6-x86_64.tar.gz
 
mkdir /application/
 
mv mysql-5.5.49-linux2.6-x86_64 /application/mysql-5.5.49
 
ln -s /application/mysql-5.5.49 /application/mysql
 
chown -R mysql.mysql /application/mysql/data/
 
#初始化数据库
 
cd /application/mysql/
 
./scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/application/mysql/data/ --user=mysql
 
#复制配置文件
 
\cp /application/mysql/support-files/my-small.cnf /etc/my.cnf
 
cp /application/mysql/support-files/mysql.server  /etc/init.d/mysqld
 
#替换配置路径
 
sed -i  's#/usr/local/#/application/#g'  /application/mysql/bin/mysqld_safe /etc/init.d/mysqld
 
#授权配置文件
 
chmod +x /etc/init.d/mysqld
 
#配置环境变量
 
PATH="/application/mysql/bin/:$PATH"
 
echo 'PATH="/application/mysql/bin/:$PATH"' >>/etc/rc.local
 
#启动并查端口
 
/etc/init.d/mysqld start
 
lsof -i :3306
 
netstat -lntup |grep 3306
 
6.1.2 在web01主机上备份数据库并发送到172.16.1.51 (db01)的独立数据库机器上
 
cd /home/oldboy/tools
 
mysqldump -uroot -poldboy123 wordpress -B |gzip>bak.sql.gz
 
scp bak.sql.gz root@10.0.0.51:/tmp
 
6.1.3 在db01机器上创建用户密码、解压备份的数据并还原到数据库
 
mysqladmin -uroot password oldboy123
 
cd /tmp
 
gzip -d bak.sql.gz
 
mysql -uroot -poldboy123 </tmp/bak.sql   #还原
 
 
 
mysql -uroot -poldboy123 -e "show datablwes like 'wordpress';"     #还原后查看
 
mysql -uroot -poldboy123   #登录数据库
 
grant all on wordpress.* to wordpress@'172.16.1.%' identified by '123456';
 
flush privileges;
 
#创建wordpress数据管理员并刷新
 
select user,host from mysql.user;   #查看用户表
 
6.1.4 更改web01配置文件
 
cd /application/nginx/html/blog/
 
vim wp-config.php
 
修改第32行
 
sed -i 's#DB_HOST', 'localhost#DB_HOST', '172.16.1.51#g' wp-config.php
 
6.1.5 数据库里查看表
 
use wordpress;
 
show tables;
 
select * from old_posts\G;  #文字型式查看表
 
6.2 从web01上共亨目录挂载到nfs
 
机器IP:172.16.1.31  nfs01
 
6.2.1 创建跟web01上nginx使用一样的系统用户www
 
useradd -u 503  www
 
id www
 
6.2.2 修改nfs01的配置文件
 
vim /etc/exports
 
#share /data by oldboy for bingbing at 20160725
 
/data 172.16.1.0/24(rw,sync,all_squash,anonuid=503,anongid=503)  ##指定UID
 
6.2.3 检查看装包
 
 [root@web01 html]# rpm -qa rpcbind nfs-utils
 
rpcbind-0.2.0-12.el6.x86_64
 
nfs-utils-1.2.3-70.el6_8.1.x86_64
 
6.2.4 检查挂载
 
[root@web01 html]# showmount -e 172.16.1.31
 
Export list for 172.16.1.31:
 
/backup 172.16.1.0/24
 
/data   172.16.1.0/24
 
[root@web01 html]# /etc/init.d/rpcbind status
 
rpcbind (pid  1314) is running...
 
6.2.5 备份共亨目录里的文件再挂载
 
cd /application/nginx/html/blog/wp-content/uploads
 
mv 2016 /tmp
 
mount -t nfs 172.16.1.31:/data/nfs-blog /application/nginx/html/blog/wp-content/uploads/
 
echo " mount -t nfs 172.16.1.31:/data/nfs-blog /application/nginx/html/blog/wp-content/uploads/" >> /etc/rc.local
 
cp -a /tmp/2016/ /application/nginx/html/blog/wp-content/uploads
 
 

(编辑:聊城站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!