LNMP部署(LNMP全部部署在一台上)
一、部署LNMP(将Nginx和PHP的程序用户设为同一个)
1 安装Nginx
[root@localhost ~]# tar xf nginx-1.10.2.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/nginx-1.10.2/
[root@localhost ~]# mount /dev/sr0 /media/cdrom
[root@localhost nginx-1.10.2]# yum -y install pcre-devel openssl-devel
[root@localhost nginx-1.10.2]# useradd -s /sbin/nologin -M www //创建程序用户
[root@localhost nginx-1.10.2]# ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
[root@localhost nginx-1.10.2]# make && make install
[root@localhost nginx-1.10.2]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
[root@localhost conf]# cd /usr/local/nginx/conf/
[root@localhost conf]# egrep -v "#|^$" nginx.conf.default > nginx.conf
[root@localhost 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.yunjisuan.com;
location / {
root html/www;
index index.html index.htm;
}
}
}
!Nginx编译安装完毕
[root@localhost conf]# /usr/local/nginx/sbin/nginx #启动Nginx进程
[root@localhost conf]# ss -antup | grep 80
[root@localhost conf]# echo "`hostname -I` www.yunjisuan.com" >index.html #做静态网页
[root@localhost conf]# echo "`hostname -I` www.yunjisuan.com" >> /etc/hosts #做本地映射
注意:若修改配置文件,重启配置文件,/usr/local/nginx/sbin/nginx -s reload /usr/local/nginx/sbin/nginx -t 通常重启配置文件之前要检查配置文件有无问题
2 安装MySQL(PHP需要MySQL的支持环境)
# 创建mysql用户的账号
[root@zhangminagjuan ~]# groupadd mysql
[root@zhangminagjuan ~]# useradd -s /sbin/nologin -g mysql -M mysql
[root@zhangminagjuan ~]# tail -1 /etc/passwd
mysql:x:501:501::/home/mysql:/sbin/nologin
[root@zhangminagjuan ~]# id mysql
uid=501(mysql) gid=501(mysql) groups=501(mysql)
# 采用二进制方式安装MySQL
[root@zhangminagjuan ~]# tar xf mysql-5.5.32-linux2.6-x86_64.tar.gz -C /usr/local/
[root@zhangminagjuan ~]# cd /usr/local/
[root@zhangminagjuan local]# mv mysql-5.5.32-linux2.6-x86_64 mysql-5.5.32
[root@zhangminagjuan local]# ln -s mysql-5.5.32 mysql
[root@zhangminagjuan local]# ls
bin games lib libexec mysql-5.5.32 sbin src
etc include lib64 mysql nginx share
[root@zhangminagjuan local]# cd /usr/local/mysql
[root@zhangminagjuan mysql]# ls
bin data include lib mysql-test scripts sql-bench
COPYING docs INSTALL-BINARY man README share support-files
#提示:
二进制安装包,仅需要解压就可以了,不需要执行cmake/configure,make,make install等过程
注意:
-
#当安装LNMP一体化环境时,MySQL数据库要装在Nginx所在的机器上。如果MySQL和Nginx不在一台机器上,那么,Nginx服务器上的MySQL数据库软件包只要解压移动到/usr/local/目录,改名为mysql就可以了,不需要进行后面的初始化配置。
-
#在非一体的LNMP环境(Nginx和MySQL不在一台机器上),编译PHP环境时,也是需要MySQL数据库环境的,但是高版本的PHP,例如5.3版本以上,内置了PHP需要的MySQL程序,因此,对于此类版本就不需要在Nginx服务器上安装MySQL软件了,只需要在编译PHP时指定相关参数即可。这个PHP的编译参数为--with-mysql=mysqld,表示PHP程序在编译时会调用内置的MySQL的库。
#初始化 MySQL配置文件my.cnf
[root@zhangminagjuan mysql]# cd /usr/local/mysql
[root@zhangminagjuan mysql]# ls -l support-files/*.cnf
-rw-r--r-- 1 7161 wheel 4691 Jun 19 2013 support-files/my-huge.cnf
-rw-r--r-- 1 7161 wheel 19759 Jun 19 2013 support-files/my-innodb-heavy-4G.cnf
-rw-r--r-- 1 7161 wheel 4665 Jun 19 2013 support-files/my-large.cnf
-rw-r--r-- 1 7161 wheel 4676 Jun 19 2013 support-files/my-medium.cnf
-rw-r--r-- 1 7161 wheel 2840 Jun 19 2013 support-files/my-small.cnf
[root@zhangminagjuan mysql]# /bin/cp support-files/my-small.cnf /etc/my.cnf
提示:
- support-files下有my.cnf的各种配置样例。
- 使用cp全路径/bin/cp,可实现拷贝而不出现替换提示,即如果有重名文件会直接覆盖
- 本例为测试安装环境,因此选择参数配置小的my-small.cnf配置模版,如果是生产环境可以根据硬件选择更高级的配置文件,上述配置文件模版对硬件的要求从低到高依次为:
-
my-medium.cnf (最低)
-
my-small.cnf
-
my-large.cnf
-
my-huge.cnf
-
my-innodb-heavy-4G.cnf(最高)
# 初始化MySQL数据库文件
[root@zhangminagjuan ~]# mkdir -p /usr/local/mysql/data //建立MySQL数据文件目录
[root@zhangminagjuan ~]# chown -R mysql.mysql /usr/local/mysql //授权mysql用户管理MySQL的安装目录
[root@zhangminagjuan ~]# yum -y install libaio //光盘源安装依赖包,否则下一步的编译会报错
[root@zhangminagjuan ~]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
#初始化MySQL数据库文件,会有很多信息提示,如果没有ERROR级别的错误,会有两个OK的字样,表示初始化成功,否则就要解决初始化的问题
以上命令的主要作用是在/usr/local/mysql/data/目录下生成MySQL正确运行所必需的基本数据库文件,其功能是对MySQL权限,状态等进行管理。
# 初始化故障排错集锦
错误示例1:
usr/local/mysql/bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared ob
#错误原因是没有libaio函数库的支持。需要
yum -y install libaio
错误示例2:
WARNING:The host'mysql'could not be looked up with resolveip
#需要修改主机名解析,使其和uname -n一样,修改后的结果如下:
[root@localhost ~] # grep `uname -n` /etc/hosts
错误示例3
ERROR:1004Can't create file '/tmp/#sql300e_1_o.frm'(errno:13)
#原因是/tmp目录的权限有问题。
解决办法为处理/tmp目录,如下:
[root@localhost ~]# ls -ld /tmp
drwxrwxrwt. 3 root root 4096 Jul 14 07:56 /tmp
[root@localhost ~]# chmod -R 1777 /tmp/
```
此故障必须解除,否则,后面会出现登陆不了数据库等问题。
#配置并启动MySQL数据库(PHP安装需要先有mysql环境,不然php会报错)
(1) 设置MySQL启动脚本
[root@zhangminagjuan mysql]# /bin/cp support-files/mysql.server /etc/init.d/mysqld //拷贝MySQL启动脚本到MySQL的命令路径
[root@zhangminagjuan mysql]# chmod +x /etc/init.d/mysqld //使脚本可执行
(2)MySQL二进制默认安装路径是/usr/local/mysql,启动脚本里是/usr/local/mysql。如果安装路径不同,那么脚本里路径等都需要替换
(3)启动MySQL数据库,命令如下:
[root@zhangminagjuan mysql]# /etc/init.d/mysqld start
Starting MySQL........ SUCCESS!
[root@zhangminagjuan mysql]# netstat -antup | grep mysql #检查MySQL数据库是否启动
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 4037/mysqld
[root@zhangminagjuan mysql]# netstat -antup | grep mysql #检查MySQL数据库是否启动
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 4037/mysqld
以上是启动数据库的规范方法之一,但还可以用如下方式启动,
/usr/local/mysql/bin/mysqld_safe --user=mysql &
这个命令结尾的“&”符号,作用是在后台执行MySQL服务,命令执行完还需要按下回车才能进入命令行状态。
如果发现3306端口没起来,请tail -100 /usr/local/mysql/data/主机名.err查看日志信息,看是否有报错信息,然后根据相关错误提示进行调试。经常查看服务运行日志是个很好的习惯,也是高手的习惯。
(5)查看MySQL数据库启动结果日志,命令如下:
[root@zhangminagjuan mysql]# tail -10 /usr/local/mysql/data/zhangminagjuan.err InnoDB: Creating foreign key constraint system tables
InnoDB: Foreign key constraint system tables created
181127 19:08:47 InnoDB: Waiting for the background threads to start
181127 19:08:48 InnoDB: 5.5.32 started; log sequence number 0
181127 19:08:48 [Note] Server hostname (bind-address): '0.0.0.0'; port: 3306
181127 19:08:48 [Note] - '0.0.0.0' resolves to '0.0.0.0';
181127 19:08:48 [Note] Server socket created on IP: '0.0.0.0'.
181127 19:08:48 [Note] Event Scheduler: Loaded 0 events
181127 19:08:48 [Note] /usr/local/mysql/bin/mysqld: ready for connections.
Version: '5.5.32' socket: '/tmp/mysql.sock' port: 3306 MySQL Community Server (GPL)
(6)设置MySQL开机自启动,命令如下:
[root@zhangminagjuan mysql]# chkconfig --add mysqld
[root@zhangminagjuan mysql]# chkconfig mysqld on
[root@zhangminagjuan mysql]# chkconfig --list mysqld
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
提示:也可以将启动命令/etc/init.d/mysqld start 放到/etc/rc.local里面
(7)配置mysql命令的全局使用路径,命令如下:
[root@zhangminagjuan mysql]# ln -s /usr/local/mysql/bin/* /usr/local/bin/
[root@zhangminagjuan mysql]# which mysqladmin
/usr/local/bin/mysqladmin
#MySQL安全配置
为MySQL的root用户设置密码,命令如下:
[root@zhangminagjuan mysql]# mysqladmin -u root password '666666'
[root@zhangminagjuan mysql]# mysql -uroot -p666666
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.32 MySQL Community Server (GPL)
Copyright (c) 2000, 2013, 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>
3 LNMP之PHP(FastCGI方式)服务的安装和准备
# 检查Nginx及MySQL的安装情况
[root@zhangminagjuan ~]# ls -ld /usr/local/nginx //检查确认Nginx及MySQL的安装路径
drwxr-xr-x 6 root root 4096 Nov 27 16:47 /usr/local/nginx
[root@zhangminagjuan ~]# ls -ld /usr/local/mysql
lrwxrwxrwx 1 mysql mysql 12 Nov 27 18:48 /usr/local/mysql -> mysql-5.5.32
[root@zhangminagjuan ~]# netstat -antup | grep -E "80|3306" //检查端口及启动情况
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 4037/mysqld
测试访问Nginx及mysql是否OK,
是不是能curl通
能不能登录mysql
#检查安装PHP所需的lib库
[root@localhost ~]# rpm -qa zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel
zlib-devel-1.2.3-29.el6.x86_64
[root@localhost ~]# rpm -qa freetype-devel libpng-devel gd libcurl-devel libxslt-devel
-
注:每个lib一般都会存在对应的以“*-devel”命名的包,安装lib对应的-devel包后,对应的lib包就会自动安装好,例如安装gd-devel时就会安装gd。
-
这些lib库不是必须安装的,但是目前的企业环境下一般都需要安装。否则,PHP程序运行时会出现问题,例如验证码无法显示等。
执行下面命令安装相关的lib软件包
[root@localhost ~]# yum -y install zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel
[root@localhost ~]# yum -y install freetype-devel libpng-devel gd libcurl-devel libxslt-devel
安装后的结果如下:
[root@zhangminagjuan ~]# rpm -qa zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel
libjpeg-turbo-devel-1.2.1-1.el6.x86_64
zlib-devel-1.2.3-29.el6.x86_64
libxml2-devel-2.7.6-14.el6.x86_64
1. #这里仅缺少libiconv-devel包 因为默认的yum源没有此包,后面会编译安装。
[root@zhangminagjuan ~]# rpm -qa freetype-devel libpng-devel gd libcurl-devel libxslt-devel
libxslt-devel-1.1.26-2.el6_3.1.x86_64
gd-2.0.35-11.el6.x86_64
libpng-devel-1.2.49-1.el6_2.x86_64
libcurl-devel-7.19.7-37.el6_4.x86_64
freetype-devel-2.3.11-14.el6_3.1.x86_64
安装yum无法安装的libiconv库
[root@zhangminagjuan libiconv-1.14]# tar xf libiconv-1.14.tar.gz -C /usr/src/
[root@zhangminagjuan libiconv-1.14]# cd /usr/src/libiconv-1.14/
[root@zhangminagjuan libiconv-1.14]# ./configure --prefix=/usr/local/libiconv && make && make install
安装libmcrypt库
推荐使用简单的在线yum的方式安装:wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
编译安装过程略
[root@localhost yum.repos.d]# yum -y install libmcrypt-devel
#开始安装PHP(FastCGI方式)服务
1. [root@localhost ~]# tar xf php-5.3.28.tar.gz -C /usr/src/
2. [root@localhost ~]# cd /usr/src/php-5.3.28/
3. [root@localhost php-5.3.28]# ./configure \
4. > --prefix=/usr/local/php5.3.28 \ #表示指定PHP的安装路径为/usr/local/php5.3.28
5. > --with-mysql=/usr/local/mysql \ #表示需要指定MySQL的安装路径,安装PHP需要的MySQL相关内容。当然,如果没有MySQL软件包,也可以不单独安装,这样的情况可使用--with-mysql=mysqlnd替代--with- mysql=/usr/local/mysql,因为PHP软件里已经自带了连接MySQL的客户端工具。
6. > --with-iconv-dir=/usr/local/libiconv \
7. > --with-freetype-dir \
8. > --with-jpeg-dir \
9. > --with-png-dir \
10. > --with-zlib \
11. > --with-libxml-dir=/usr \
12. > --enable-xml \
13. > --disable-rpath \
14. > --enable-safe-mode \
15. > --enable-bcmath \
16. > --enable-shmop \
17. > --enable-sysvsem \
18. > --enable-inline-optimization \
19. > --with-curl \
20. > --with-curlwrappers \
21. > --enable-mbregex \
22. > --enable-fpm \
23. > --enable-mbstring \
24. > --with-mcrypt \
25. > --with-gd \
26. > --enable-gd-native-ttf \
27. > --with-openssl \
28. > --with-mhash \
29. > --enable-pcntl \
30. > --enable-sockets \
31. > --with-xmlrpc \
32. > --enable-zip \
33. > --enable-soap \
34. > --enable-short-tags \
35. > --enable-zend-multibyte \
36. > --enable-static \
37. > --with-xsl \
38. > --with-fpm-user=www \ #nginx表示指定PHP-FPM进程管理的用户为www,此处最好和Nginx服务用户统一
39. > --with-fpm-group=www \ #表示指定PHP-FPM进程管理的组为www,此处最好与Nginx服务用户组统一。
40. > --enable-ftp #表示激活PHP-FPM方式服务,即以FastCGIF方式运行PHP服务。
1. #特别强调:上述每行结尾的换行符反斜线(\)之后不能再有任何字符包括空格
#编译PHP
[root@zhangminagjuan php-5.3.28]# ln -s /usr/local/mysql/lib/libmysqlclient.so.18
[root@zhangminagjuan php-5.3.28]# ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/
[root@zhangminagjuan php-5.3.28]# touch ext/phar/phar.phar
[root@zhangminagjuan php-5.3.28]# make
#make最后的正确提示
Build complete.
Don't forget to run 'make test'.
[root@zhangminagjuan php-5.3.28]# make install //安装PHP生成文件到系统
#配置PHP引擎配置文件php.ini
[root@zhangminagjuan ~]# ln -s /usr/local/php5.3.28/ /usr/local/php //设置软连接以方便访问
[root@zhangminagjuan ~]# ls -l /usr/local/php
lrwxrwxrwx 1 root root 21 Nov 27 22:24 /usr/local/php -> /usr/local/php5.3.28/
[root@zhangminagjuan ~]# cd /usr/src/php-5.3.28/ //查看PHP配置默认模板文件
[root@zhangminagjuan php-5.3.28]# ls php.ini*
php.ini-development php.ini-production
[root@zhangminagjuan php-5.3.28]# cp php.ini-production /usr/local/php/lib/php.ini //拷贝PHP配置文件到PHP默认目录,并更改文件名为php.ini
[root@zhangminagjuan php-5.3.28]# ls -l /usr/local/php/lib/php.ini
-rw-r--r-- 1 root root 69627 Nov 27 22:29 /usr/local/php/lib/php.ini
# 配置PHP(FastCGI方式)的配置文件php-fpm.conf
[root@zhangminagjuan php-5.3.28]# /bin/cp php.ini-production /usr/local/php/lib/php.ini
[root@zhangminagjuan php-5.3.28]# ls -l /usr/local/php/lib/php.ini
-rw-r--r-- 1 root root 69627 Nov 27 22:33 /usr/local/php/lib/php.ini
[root@zhangminagjuan php-5.3.28]# cd /usr/local/php/etc/
[root@zhangminagjuan etc]# ls
pear.conf php-fpm.conf.default
[root@zhangminagjuan etc]# /bin/cp php-fpm.conf.default php-fpm.conf
关于php-fpm.conf,暂时可用默认的配置,先把服务搭好,以后再进行优化。
#启动PHP服务(FastCGI方式)
[root@zhangminagjuan etc]# /usr/local/php/sbin/php-fpm //启动PHP服务php-fpm
[root@zhangminagjuan etc]# ps -ef | grep php-fpm //检查PHP服务php-fpm的进程及启动端口的情况
root 4961 1 0 22:35 ? 00:00:00 php-fpm: master process (/usr/local/php5.3.28/etc/php-fpm.conf)
www 4962 4961 0 22:35 ? 00:00:00 php-fpm: pool www
www 4963 4961 0 22:35 ? 00:00:00 php-fpm: pool www
root 4965 117449 8 22:35 pts/1 00:00:00 grep php-fpm
[root@zhangminagjuan yum.repos.d]# lsof -i:9000 //默认9000端口提供服务
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
php-fpm 4961 root 7u IPv4 123027 0t0 TCP localhost:cslistener (LISTEN)
php-fpm 4962 www 0u IPv4 123027 0t0 TCP localhost:cslistener (LISTEN)
php-fpm 4963 www 0u IPv4 123027 0t0 TCP localhost:cslistener (LISTEN)
#配置Nginx支持PHP程序请求访问
修改配置文件
[root@zhangminagjuan etc]# cd /usr/local/nginx/conf/
[root@zhangminagjuan conf]# cp nginx.conf nginx.conf.02
[root@zhangminagjuan 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 blog.yunjisuan.com;
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;
}
}
}
检查并启动Nginx
[root@zhangminagjuan conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@zhangminagjuan conf]# /usr/local/nginx/sbin/nginx -s reload
##此步在生产环境很关键,如不提前检查语法,重启后发现语法错误会导致Nginx无法提供服务,,给用户访问体验带来不好的影响。
#测试LNMP环境生效情况
测试PHP解析的请求是否OK
[root@zhangminagjuan conf]# cd /usr/local/nginx/html/
[root@zhangminagjuan html]# mkdir blog
[root@zhangminagjuan html]# cd blog/ //进入指定的默认站点目录后,编辑index.php
[root@zhangminagjuan blog]# echo "<?php phpinfo(); ?>" >yunjisuan.php
调整Windows下的host解析:本地IP 域名blog.yunjisuan.com
打开浏览器,输入http://blog.yunjisuan.com/test_info.php 即可打开如下图所示界面:
!表明: 出现上述界面,表示Nginx配合PHP解析已经正常。
#针对Nginx请求访问PHP,然后对PHP连接mysql的情况进行测试
[root@zhangminagjuan php-5.3.28]# cd /usr/local/nginx/html/
[root@zhangminagjuan html]# cd blog/
[root@zhangminagjuan blog]# vim test_info.php //编辑test_mysql.php
<?php
$link_id=mysql_connect('localhost','root','666666');
if($link_id){
echo "mysql successful by juAnne !";
}else{
echo mysql_error();
}
?>
!!至此,LNMP的组合已基本搭建完毕。还没有做相关优化
4 部署一个blog程序服务
开源博客程序WordPress介绍
WordPress 是一套利用PHP语言和MySQL数据库开发的开源免费的blog(博客,网站)程序,用户可以在支持PHP环境和MySQL数据库的服务器上建立blog站点。它的功能非常强大,拥有众多插件,易于扩充功能。其安装和使用也都非常方便。目前WordPress已经成为搭建blog平台的主流,很多发布平台都是根据WordPress二次开发的,如果你也想像他们一样拥有自己的blog,可购买网上的域名及空间,然后搭建LNMP环境,部署WordPress程序后就可以轻松成就自己的梦想了。
# MySQL数据库配置准备
[root@localhost ~]# mysql -uroot -p666666 //登陆MySQL数据库
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.5.32 MySQL Community Server (GPL)
Copyright (c) 2000, 2013, 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> create database wordpress; #创建一个数据库,名字为wordpress,用于存放blog数据
Query OK, 1 row affected (0.27 sec)
mysql> show databases like 'wordpress'; #查看
+----------------------+
| Database (wordpress) |
+----------------------+
| wordpress |
+----------------------+
1 row in set (0.37 sec)
mysql> grant all on wordpress.* to wordpress@'localhost' identified by '666666';
Query OK, 0 rows affected (0.33 sec)
mysql> flush privileges; #刷新权限,使得创建用户生效
Query OK, 0 rows affected (0.28 sec)
mysql> show grants for wordpress@'localhost'; #查看用户对应权限
+------------------------------------------------------------------------------------------------------------------+
| Grants for wordpress@localhost |
+------------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'wordpress'@'localhost' IDENTIFIED BY PASSWORD '*B2B366CA5C4697F31D4C55D61F0B17E70E5664EC' |
| GRANT ALL PRIVILEGES ON `wordpress`.* TO 'wordpress'@'localhost' |
+------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.06 sec)
mysql> select user,host from mysql.user; #查看数据库里创建的wordpress用户
+-----------+----------------+
| user | host |
+-----------+----------------+
| root | 127.0.0.1 |
| root | ::1 |
| | localhost |
| root | localhost |
| wordpress | localhost | #只允许本机通过wordpress用户访问数据库
| | zhangminagjuan |
| root | zhangminagjuan |
+-----------+----------------+
7 rows in set (0.12 sec)
mysql> quit
Bye
# Nginx及PHP环境配置准备
选择之前配置好的支持LNMP的blog域名对应的虚拟主机,命令如下:
[root@localhost ~]# cd /usr/local/nginx/conf
[root@localhost conf]# vim nginx.conf
server {
listen 80;
server_name blog.yunjisuan.com;
location / {
root html/blog;
index index.php index.html index.htm; #补充一个首页文件index.php
}
location ~ .*\.(php|php5)?$ {
root html/blog;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
[root@localhost conf]# /usr/local/nginx/sbin/nginx -s reload
获取WordPress博客程序,并放置到blog域名对应虚拟主机的站点目录下,即/usr/local/nginx/html/blog,操作命令如下:
[root@zhangminagjuan blog]# cd
[root@zhangminagjuan ~]# cd /usr/local/nginx/html/blog/
[root@zhangminagjuan blog]# ls #浏览www.wordpress.org下载博客程序
test_info.php test_mysql.php wordpress-4.7.4-zh_CN.tar.gz
[root@zhangminagjuan blog]# tar xf wordpress-4.7.4-zh_CN.tar.gz #解压
[root@zhangminagjuan blog]# ls
test_info.php test_mysql.php wordpress wordpress-4.7.4-zh_CN.tar.gz
[root@zhangminagjuan blog]# rm -f test_info.php test_mysql.php #删除无用文件
[root@zhangminagjuan blog]# ls
wordpress wordpress-4.7.4-zh_CN.tar.gz
[root@zhangminagjuan blog]# mv wordpress/* . #把目录里的内容移动到blogcom根目录下
[root@zhangminagjuan blog]# /bin/mv wordpress-4.7.4-zh_CN.tar.gz /root/ #移走源程序
[root@zhangminagjuan blog]# ls -l #完整的blog程序内容
total 192
-rw-r--r-- 1 nobody 65534 418 Sep 25 2013 index.php
-rw-r--r-- 1 nobody 65534 19935 Jan 3 2017 license.txt
-rw-r--r-- 1 nobody 65534 6956 Apr 23 2017 readme.html
drwxr-xr-x 2 nobody 65534 4096 Nov 28 01:08 wordpress
-rw-r--r-- 1 nobody 65534 5447 Sep 28 2016 wp-activate.php
drwxr-xr-x 9 nobody 65534 4096 Apr 23 2017 wp-admin
-rw-r--r-- 1 nobody 65534 364 Dec 19 2015 wp-blog-header.php
-rw-r--r-- 1 nobody 65534 1627 Aug 29 2016 wp-comments-post.php
-rw-r--r-- 1 nobody 65534 2930 Apr 23 2017 wp-config-sample.php
drwxr-xr-x 5 nobody 65534 4096 Apr 23 2017 wp-content
-rw-r--r-- 1 nobody 65534 3286 May 25 2015 wp-cron.php
drwxr-xr-x 18 nobody 65534 12288 Apr 23 2017 wp-includes
-rw-r--r-- 1 nobody 65534 2422 Nov 21 2016 wp-links-opml.php
-rw-r--r-- 1 nobody 65534 3301 Oct 25 2016 wp-load.php
-rw-r--r-- 1 nobody 65534 33939 Nov 21 2016 wp-login.php
-rw-r--r-- 1 nobody 65534 8048 Jan 11 2017 wp-mail.php
-rw-r--r-- 1 nobody 65534 16255 Apr 7 2017 wp-settings.php
-rw-r--r-- 1 nobody 65534 29896 Oct 19 2016 wp-signup.php
-rw-r--r-- 1 nobody 65534 4513 Oct 15 2016 wp-trackback.php
-rw-r--r-- 1 nobody 65534 3065 Sep 1 2016 xmlrpc.php
[root@zhangminagjuan blog]# chown -R www.www /usr/local/nginx/html/blog/ #授权用户访问
[root@zhangminagjuan blog]# ls -l #最终博客目录和权限
total 192
-rw-r--r-- 1 www www 418 Sep 25 2013 index.php
-rw-r--r-- 1 www www 19935 Jan 3 2017 license.txt
-rw-r--r-- 1 www www 6956 Apr 23 2017 readme.html
drwxr-xr-x 2 www www 4096 Nov 28 01:08 wordpress
-rw-r--r-- 1 www www 5447 Sep 28 2016 wp-activate.php
drwxr-xr-x 9 www www 4096 Apr 23 2017 wp-admin
-rw-r--r-- 1 www www 364 Dec 19 2015 wp-blog-header.php
-rw-r--r-- 1 www www 1627 Aug 29 2016 wp-comments-post.php
-rw-r--r-- 1 www www 2930 Apr 23 2017 wp-config-sample.php
drwxr-xr-x 5 www www 4096 Apr 23 2017 wp-content
-rw-r--r-- 1 www www 3286 May 25 2015 wp-cron.php
drwxr-xr-x 18 www www 12288 Apr 23 2017 wp-includes
-rw-r--r-- 1 www www 2422 Nov 21 2016 wp-links-opml.php
-rw-r--r-- 1 www www 3301 Oct 25 2016 wp-load.php
-rw-r--r-- 1 www www 33939 Nov 21 2016 wp-login.php
-rw-r--r-- 1 www www 8048 Jan 11 2017 wp-mail.php
-rw-r--r-- 1 www www 16255 Apr 7 2017 wp-settings.php
-rw-r--r-- 1 www www 29896 Oct 19 2016 wp-signup.php
-rw-r--r-- 1 www www 4513 Oct 15 2016 wp-trackback.php
-rw-r--r-- 1 www www 3065 Sep 1 2016 xmlrpc.php
#开始安装blog博客程序
打开浏览器输入blog.yunjisuan.com(提前做好hosts或DNS解析),回车后,出现下图:
仔细阅读页面的文字信息后,单击“现在就开始”按钮继续,然后在出现的页面表单上填写相应的内容,如下图所示:
实现WordPress博客程序URL伪静态化
/archives/%post_id%.html
##说明:%post_id%是数据库对应博文内容的唯一ID,例如423
在Nginx配置文件的server体里面中添加代码
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name blog.yunjisuan.com;
location / {
root html/blog;
index index.php index.html index.htm; 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;
}
}
}
#用户浏览器的图片解析
用户通过PC上网,当输入blog.yunjisuan.com时,PC机向Web服务器发起请求;访问浏览器首页index.html,web浏览器读取index.html文件的内容,发给用户浏览器;用户浏览器开始解析,解析到文字时,文字就会出现在浏览器上;解析到图片时,用户浏览器将图片的地址读取出来,再次向Web服务器发起请求,Web服务器收到请求后,将图片读取出来再次发给用户浏览器,用户浏览器再把返还的内容显示到浏览器的页面上,此时图片就解析出来了。
上传图片时,图片存在存储里(挂载存储NFS),再把图片放在存储里的位置写到数据库里;
设计上传时,网页目录html由两个子目录:static(读的)和upload(上传的),NFS存储既要挂载在static目录上,还要挂载在upload目录上;静态网页读取时经过static目录,php动态网页读取时也要经过static目录,但是,PHP还要经过upload目录;PHP经过upload目录后,图片上传进upload后,还要将图片的具体位置写入MySQL数据库;这样,在下次访问index.php,SQL语句直接到MySQL中查找。
PHP有两个作用:
(1)读动态网页(读取时可以不挂载存储NFS,直接从MySQL里把URL读出来反馈给用户,用户浏览器解析的图片会再发次请求,找static读取图片)
(2)上传数据,向服务器写入数据
图片加文字的部署
修改配置文件
[root@zhangminagjuan html]# vim /usr/local/nginx/conf/nginx.conf
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server{
listen 80;
server_name xxx.yunjisuan.com;
location / {
root html/xxx;
index index.html index.htm;
}
}
}
注意:修改完配置文件重启
[root@zhangminagjuan xxx]# /usr/local/nginx/sbin/nginx -s reload
[root@zhangminagjuan xxx]# ls
zhangming.jpg
[root@zhangminagjuan xxx]# mv zhangming.jpg 111.jpg
[root@zhangminagjuan xxx]# ls
111.jpg
[root@zhangminagjuan xxx]# vim index.html
welcome to yunjisuan
<img src="http://192.168.204.148/111.jpg" />
在本地做映射,本地IP xxx.yunjisuan.com;在网页输入域名,显示如下