设为首页 收藏本站
查看: 2405|回复: 1

从零开始完整搭建LNMP环境+WordPress部署

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2017-3-6 13:37:53 | 显示全部楼层 |阅读模式
0.说明


    内容会有点多,但是下面的内容都是自己在学习LNMP环境搭建过程中的完整再现,所以非常具有参考价值!

    下面用一个以最小化方式(Minimal)安装的CentOS 6.5操作系统为例,演示LNMP环境完整搭建过程,并且最后部署了一个WordPress博客,最后完成的效果如下:

wKiom1i5WDzAvN4gAAHiOu2VhJI599.png



1.关于实验环境的说明

    本次实验的测试环境使用的宿主机操作系统为Windows 7,在Vmware虚拟机安装CentOS 6.5,说明如下:
  • 宿主机操作系统Windows 7
  • 虚拟机安装的操作系统CentOS 6.5
  • 虚拟机操作系统上网方式NAT

    而当使用NAT的方式进行上网时虚拟机、宿主机之间的网络连接关系可如下所示:
wKioL1i5WL-Du54UAABgyrRKH34580.png
    关于为什么网络拓扑结构是这样的,这里不展开说明,可以参考博主的另一篇博文《在实践中深入理解VMware虚拟机的上网模式NAT模式》,这篇文章深入地分析了VMware虚拟机使用NAT模式上网时的网络结构细节,相信看完这篇文章后,这里搭建Nginx的实验环境也就很容易理解了。
    所以首先,应该是自己先配置好网络环境,让宿主机跟我们的虚拟机可以通信,实际上,如果理解了VMware虚拟机上网方式的原理,同时对CentOS的网络配置也很熟悉,这一步是可以很轻松完成的,这里就不给出过程了,这里所用的IP地址跟上面的图示是一样的。
    最后,我们是在宿主机上访问我们搭建的WordPress博客的,非常不错,可以体验一下!



2.LNMP环境搭建:Nginx安装、测试与域名配置

    事实上,在另一篇博文中有非常详细的介绍《在CentOS上编译安装Nginx+实验环境搭建+测试》,不过这里为了完整性,依然会给出所有步骤,只是过程会简化很多。


(1)Nginx安装

  • 1.安装Nginx依赖函数库pcre、openssl-devel

1
2
3
4
5
6
7
[iyunv@leaf ~]# yum install -y pcre pcre-devel openssl openssl-devel
......
[iyunv@leaf ~]# rpm -q pcre pcre-devel openssl openssl-devel
pcre-7.8-7.el6.x86_64
pcre-devel-7.8-7.el6.x86_64
openssl-1.0.1e-48.el6_8.4.x86_64
openssl-devel-1.0.1e-48.el6_8.4.x86_64



  • 2.下载安装Nginx

    这里使用Nginx1.6.3,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 下载Nginx
[iyunv@leaf ~]# yum install -y wget
[iyunv@leaf ~]# mkdir tools
[iyunv@leaf ~]# cd tools/
[iyunv@leaf tools]# wget
[iyunv@leaf tools]# ll
总用量 788
-rw-r--r--. 1 root root 805253 4月   8 2015 nginx-1.6.3.tar.gz

# 解压缩
[iyunv@leaf tools]# tar zxf nginx-1.6.3.tar.gz
[iyunv@leaf tools]# ll
总用量 792
drwxr-xr-x. 8 1001 1001   4096 4月   7 2015 nginx-1.6.3
-rw-r--r--. 1 root root 805253 4月   8 2015 nginx-1.6.3.tar.gz

# 指定编译参数
[iyunv@leaf tools]# yum install -y gcc    # 需要先安装gcc
[iyunv@leaf tools]# mkdir /application    # 作为Nginx的安装目录
[iyunv@leaf tools]# useradd nginx -s /sbin/nologin -M
[iyunv@leaf tools]# tail -1 /etc/passwd
nginx:x:500:500::/home/nginx:/sbin/nologin
[iyunv@leaf tools]# cd nginx-1.6.3
[iyunv@leaf nginx-1.6.3]# ./configure --user=nginx --group=nginx --prefix=/application/nginx-1.6.3/ --with-http_stub_status_module --with-http_ssl_module
[iyunv@leaf nginx-1.6.3]# echo $?    # 结果输出0则说明命令执行成功
0

# 编译
[iyunv@leaf nginx-1.6.3]# make
[iyunv@leaf nginx-1.6.3]# echo $?
0

# 安装
[iyunv@leaf nginx-1.6.3]# make install
[iyunv@leaf nginx-1.6.3]# echo $?
0

# 建立安装目录的软链接
[iyunv@leaf nginx-1.6.3]# ln -s /application/nginx-1.6.3/ /application/nginx
[iyunv@leaf nginx-1.6.3]# ls -l /application/
总用量 4
lrwxrwxrwx. 1 root root   25 3月   4 04:28 nginx -> /application/nginx-1.6.3/
drwxr-xr-x. 6 root root 4096 3月   4 04:27 nginx-1.6.3




(2)Nginx测试

  • 1.启动Nginx

1
2
3
4
[iyunv@leaf ~]# /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
[iyunv@leaf ~]# /application/nginx/sbin/nginx    # 启动Nginx服务



  • 2.CentOS上验证Nginx服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
[iyunv@leaf ~]# netstat -lntup | grep 80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      3929/nginx  
[iyunv@leaf ~]# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>



  • 3.宿主机上验证Nginx服务


    在宿主机浏览器上输入CentOS主机的IP地址10.0.0.101,如下:

wKioL1i5Y4mRc1ykAADay0D-wgY534.png

(3)域名配置

    这一部分的内容在另一篇博文也有很详细的介绍《Nginx配置多个基于域名的虚拟主机+实验环境搭建+测试》,可以参考一下,所以这里不会给出非常详细的说明。

    因为要搭建一个博客服务,所以这里配置的域名为blog.xpleaf.org,操作过程如下:

  • 1.最小化配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
[iyunv@leaf ~]# cd /application/nginx/conf/
[iyunv@leaf conf]# wc -l nginx.conf
117 nginx.conf
[iyunv@leaf conf]# wc -l nginx.conf.default
117 nginx.conf.default
[iyunv@leaf conf]# egrep -v "#|^$" nginx.conf.default >nginx.conf
[iyunv@leaf conf]# wc -l nginx.conf
22 nginx.conf
[iyunv@leaf conf]# cat 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  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}



  • 2.修改配置文件

    修改nginx.conf,并且增加配置文件extra/blog.conf,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[iyunv@leaf conf]# cat nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    include extra/blog.conf;
}
[iyunv@leaf conf]# cat extra/blog.conf
server {
        listen       80;
        server_name  blog.xpleaf.org;
        location / {
            root   html/blog;
            index  index.html index.htm;
        }
    }



  • 3.创建域名对应的站点目录及文件

1
2
3
4
5
[iyunv@leaf conf]# cd ../html/
[iyunv@leaf html]# mkdir blog
[iyunv@leaf html]# echo "This page is: blog.xpleaf.org">blog/index.html
[iyunv@leaf html]# cat blog/index.html
This page is: blog.xpleaf.org



  • 4.重启Nginx服务

1
2
3
[iyunv@leaf html]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
[iyunv@leaf html]# /application/nginx/sbin/nginx -s reload    # 平滑重启



  • 5.CentOS 6.5上进行测试

    先修改/etc/hosts文件:

1
2
3
[iyunv@leaf html]# echo "127.0.0.1 blog.xpleaf.org" >>/etc/hosts
[iyunv@leaf html]# tail -1 /etc/hosts
127.0.0.1 blog.xpleaf.org



    再使用命令测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
[iyunv@leaf html]# curl blog.xpleaf.org
This page is: blog.xpleaf.org
[iyunv@leaf html]# wget blog.xpleaf.org
--2017-03-04 04:58:42--  http://blog.xpleaf.org/
正在解析主机 blog.xpleaf.org... 127.0.0.1
正在连接 blog.xpleaf.org|127.0.0.1|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:30 [text/html]
正在保存至: “index.html.1”

100%[====================================>] 30          --.-K/s   in 0s      

2017-03-04 04:58:42 (2.14 MB/s) - 已保存 “index.html.1” [30/30])



  • 6.宿主机Windows 7上进行测试

    同样是先修改hosts文件,Windows 7的hosts文件在C:\Windows\System32\drivers\etc,同样添加下面一行:
1
10.0.0.101 blog.xpleaf.org



    使用浏览器访问blog.xpleaf.org,如下:

wKioL1i6L3XhADOzAACDQcbW48I229.png

    那么到这里,LNMP的环境中,Nginx的安装已经完成了,你是否安装成功了呢?




3.LNMP环境搭建:MySQL安装与基本安全优化


    这里采用二进制安装的方式来安装MySQL,安装的版本为:MySQL Server 5.5.54,可以在https://dev.mysql.com/downloads/mysql/5.5.html#downloads中下载。
    MySQL安装完成后会做一些基本的安全优化。


(1)MySQL安装

  • 1.创建MySQL用户的账号

1
2
3
4
[iyunv@leaf ~]# groupadd mysql
[iyunv@leaf ~]# useradd -s /sbin/nologin -g mysql -M mysql
[iyunv@leaf ~]# tail -1 /etc/passwd
mysql:x:501:501::/home/mysql:/sbin/nologin



  • 2.下载MySQL

    可以使用wget来进行安装,也可以先下载到Windows 7上,然后使用SecureCRT,在CentOS上使用rz命令(需要使用yum install -y lrzsz命令安装)上传到我们的CentOS上,其实不管哪一种方式,只要有方式获取到该安装包就可以了,下面使用的是wget获取安装包的方式:
1
2
3
[iyunv@leaf tools]# wget
[iyunv@leaf tools]# ls -l mysql-5.5.54-linux2.6-x86_64.tar.gz
-rw-r--r--. 1 root root 185911232 3月   3 13:34 mysql-5.5.54-linux2.6-x86_64.tar.gz



  • 3.解压并移到指定目录

1
2
3
4
5
6
7
8
9
[iyunv@leaf tools]# tar xf mysql-5.5.54-linux2.6-x86_64.tar.gz
[iyunv@leaf tools]# mv mysql-5.5.54-linux2.6-x86_64 /application/mysql-5.5.54
[iyunv@leaf tools]# ln -s /application/mysql-5.5.54/ /application/mysql
[iyunv@leaf tools]# ls -l /application/
总用量 8
lrwxrwxrwx.  1 root root   26 3月   4 06:43 mysql -> /application/mysql-5.5.54/
drwxr-xr-x. 13 root root 4096 3月   4 06:42 mysql-5.5.54
lrwxrwxrwx.  1 root root   25 3月   4 04:28 nginx -> /application/nginx-1.6.3/
drwxr-xr-x. 11 root root 4096 3月   4 04:30 nginx-1.6.3



  • 4.初始化MySQL配置文件

1
2
[iyunv@leaf mysql]# cp support-files/my-small.cnf /etc/my.cnf
cp:是否覆盖"/etc/my.cnf"? y



  • 5.初始化MySQL数据库文件

1
2
3
4
5
6
7
8
9
10
[iyunv@leaf mysql]# mkdir -p /application/mysql/data/
[iyunv@leaf mysql]# chown -R mysql.mysql /application/mysql
[iyunv@leaf mysql]# yum install -y libaio    # 安装MySQL依赖函数库,否则下面的初始化会失败
[iyunv@leaf mysql]# /application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=/application/mysql/data --user=mysql
......
# 输出结果可以看到两个OK,即说明初始化成功
[iyunv@leaf mysql]# echo $?    # 或者通过该命令,输出为0,即说明上一个步骤的命令执行成功
0

# 上面之后可以看到/application/mysql/data/目录下生成的数据库文件



  • 6.配置并启动MySQL数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#(1)设置MySQL启动脚本
[iyunv@leaf mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[iyunv@leaf mysql]# chmod +x /etc/init.d/mysqld
[iyunv@leaf mysql]# ls -l /etc/init.d/mysqld
-rwxr-xr-x. 1 root root 10875 3月   4 06:56 /etc/init.d/mysqld

#(2)替换启动脚本中MySQL默认的安装路径/usr/local/mysql
[iyunv@leaf mysql]# sed -i 's#/usr/local/mysql#/application/mysql#g' /application/mysql/bin/mysqld_safe /etc/init.d/mysqld

#(3)启动MySQL数据库
[iyunv@leaf mysql]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/application/mysql/data/leaf.err'.
... SUCCESS!

#(4)检查MySQL数据库是否启动
[iyunv@leaf mysql]# netstat -lntup | grep mysql
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      4400/mysqld  

#(5)查看日志
[iyunv@leaf mysql]# tail -10 /application/mysql/data/leaf.err
InnoDB: Creating foreign key constraint system tables
InnoDB: Foreign key constraint system tables created
170304  7:00:28  InnoDB: Waiting for the background threads to start
170304  7:00:29 InnoDB: 5.5.54 started; log sequence number 0
170304  7:00:29 [Note] Server hostname (bind-address): '0.0.0.0'; port: 3306
170304  7:00:29 [Note]   - '0.0.0.0' resolves to '0.0.0.0';
170304  7:00:29 [Note] Server socket created on IP: '0.0.0.0'.
170304  7:00:29 [Note] Event Scheduler: Loaded 0 events
170304  7:00:29 [Note] /application/mysql/bin/mysqld: ready for connections.
Version: '5.5.54'  socket: '/tmp/mysql.sock'  port: 3306  MySQL Community Server (GPL)

#(6)设置MySQL开机启动
[iyunv@leaf mysql]# chkconfig --add mysqld
[iyunv@leaf mysql]# chkconfig mysqld on
[iyunv@leaf mysql]# chkconfig --list mysqld
mysqld          0:关闭  1:关闭  2:启用  3:启用  4:启用  5:启用  6:关闭

#(7)配置mysql命令的全局使用路径(注意这里配置的是命令,前面配置的只是启动脚本)
[iyunv@leaf mysql]# echo 'export PATH=/application/mysql/bin:$PATH' >>/etc/profile
[iyunv@leaf mysql]# source /etc/profile
[iyunv@leaf mysql]# echo $PATH
/application/mysql/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

#(8)登陆MySQL测试
[iyunv@leaf mysql]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.54 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.05 sec)

mysql> select user();    # 查看当前登陆的用户
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

mysql> select host, user from mysql.user;
+-----------+------+
| host      | user |
+-----------+------+
| 127.0.0.1 | root |
| ::1       | root |
| leaf      |      |
| leaf      | root |
| localhost |      |
| localhost | root |
+-----------+------+
6 rows in set (0.00 sec)

mysql> quit
Bye




(2)MySQL基本安全优化

  • 1.为root用户设置密码

1
[iyunv@leaf mysql]# mysqladmin -u root password '123456'



  • 2.清理无用的MySQL用户及数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
[iyunv@leaf mysql]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.54 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> select user, host from mysql.user;
+------+-----------+
| user | host      |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1       |
|      | leaf      |
| root | leaf      |
|      | localhost |
| root | localhost |
+------+-----------+
6 rows in set (0.00 sec)

mysql> drop user "root"@"::1";
Query OK, 0 rows affected (0.00 sec)

mysql> drop user ""@"leaf";
Query OK, 0 rows affected (0.00 sec)

mysql> drop user "root"@"leaf";
Query OK, 0 rows affected (0.01 sec)

mysql> drop user ""@"localhost";
Query OK, 0 rows affected (0.01 sec)

mysql> select user, host from mysql.user;
+------+-----------+
| user | host      |
+------+-----------+
| root | 127.0.0.1 |
| root | localhost |
+------+-----------+
2 rows in set (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

# 删除无用的数据库
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

mysql> drop database test;
Query OK, 0 rows affected (0.01 sec)

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




    到此为此,MySQL也安装完成了!




4.LNMP环境搭建:PHP(FastCGI方式)安装、配置与启动

(1)安装PHP依赖函数库

  • 1.安装lib库


    需要安装的lib库如下:

1
2
zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel
freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel



    其中除了libiconv库外,其他都可以通过yum的方式进行安装,安装如下:

1
2
3
4
5
6
7
8
9
10
# 使用yum安装除libiconv-devel之外的其它lib库
[iyunv@leaf mysql]# yum install -y zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel

# 编译安装libiconv-devel
[iyunv@leaf tools]# wget
[iyunv@leaf tools]# tar zxf libiconv-1.14.tar.gz
[iyunv@leaf tools]# cd libiconv-1.14
[iyunv@leaf libiconv-1.14]# ./configure --prefix=/usr/local/libiconv
[iyunv@leaf libiconv-1.14]# make
[iyunv@leaf libiconv-1.14]# make install



  • 2.安装libmcrypt库

1
2
[iyunv@leaf ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
[iyunv@leaf ~]# yum install -y libmcrypt-devel



  • 3.安装mhash加密扩展库

1
[iyunv@leaf ~]# yum install -y mhash



  • 4.安装mcrypt加密扩展库

1
[iyunv@leaf ~]# yum install -y mcrypt




(2)安装PHP

    使用的PHP版本号为5.3.27,如下:

  • 1.下载PHP安装包

1
2
3
4
[iyunv@leaf tools]# wget http://cn2.php.net/get/php-5.3.27.tar.gz/from/this/mirror
[iyunv@leaf tools]# mv mirror php-5.3.27.tar.gz
[iyunv@leaf tools]# ls -l php-5.3.27.tar.gz
-rw-r--r--. 1 root root 15008639 1月  21 2015 php-5.3.27.tar.gz



  • 2.解压缩

1
2
3
4
[iyunv@leaf tools]# tar zxf php-5.3.27.tar.gz
[iyunv@leaf tools]# cd php-5.3.27
[iyunv@leaf php-5.3.27]# pwd
/root/tools/php-5.3.27



  • 3.配置PHP的安装参数

    配置项非常多,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
./configure \
--prefix=/application/php5.3.27 \
--with-mysql=/application/mysql \
--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-safe-mode \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--with-curlwrappers \
--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-zip \
--enable-soap \
--enable-short-tags \
--enable-zend-multibyte \
--enable-static \
--with-xsl \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--enable-ftp



    可以将其直接复制到命令行进行配置,这样就可以减少出错的概率:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
[iyunv@leaf php-5.3.27]# ./configure \
> --prefix=/application/php5.3.27 \
> --with-mysql=/application/mysql \
> --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-safe-mode \
> --enable-bcmath \
> --enable-shmop \
> --enable-sysvsem \
> --enable-inline-optimization \
> --with-curl \
> --with-curlwrappers \
> --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-zip \
> --enable-soap \
> --enable-short-tags \
> --enable-zend-multibyte \
> --enable-static \
> --with-xsl \
> --with-fpm-user=nginx \
> --with-fpm-group=nginx \
> --enable-ftp
......
+--------------------------------------------------------------------+
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE.  By continuing this installation |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+

Thank you for using PHP.



  • 4.编译PHP

1
2
3
4
5
6
7
8
[iyunv@leaf php-5.3.27]# ln -s /application/mysql/lib/libmysqlclient.so.18
libmysqlclient.so.18      libmysqlclient.so.18.0.0
[iyunv@leaf php-5.3.27]# ln -s /application/mysql/lib/libmysqlclient.so.18 /usr/lib64/
[iyunv@leaf php-5.3.27]# touch ext/phar/phar.phar
[iyunv@leaf php-5.3.27]# make
......
[iyunv@leaf php-5.3.27]# echo $?
0



  • 5.安装PHP

1
2
3
4
5
6
7
[iyunv@leaf php-5.3.27]# make install
/root/tools/php-5.3.27/build/shtool install -c ext/phar/phar.phar /application/php5.3.27/bin
ln -s -f /application/php5.3.27/bin/phar.phar /application/php5.3.27/bin/phar
Installing PDO headers:          /application/php5.3.27/include/php/ext/pdo/
......
[iyunv@leaf php-5.3.27]# echo $?
0




(3)配置与启动PHP

  • 1.设置PHP安装目录软链接

1
2
3
[iyunv@leaf php-5.3.27]# ln -s /application/php5.3.27/ /application/php
[iyunv@leaf php-5.3.27]# ls -l /application/php
lrwxrwxrwx. 1 root root 23 3月   4 08:59 /application/php -> /application/php5.3.27/



  • 2.拷贝PHP配置文件到PHP默认目录

1
2
3
[iyunv@leaf php-5.3.27]# cp php.ini-production /application/php/lib/php.ini
[iyunv@leaf php-5.3.27]# ls -l /application/php/lib/php.ini
-rw-r--r--. 1 root root 69627 3月   4 09:00 /application/php/lib/php.ini



  • 3.配置php-fpm.conf文件

1
2
3
4
[iyunv@leaf php-5.3.27]# cd /application/php/etc/
[iyunv@leaf etc]# ls
pear.conf  php-fpm.conf.default
[iyunv@leaf etc]# cp php-fpm.conf.default php-fpm.conf



  • 4.启动PHP服务php-fpm

1
[iyunv@leaf etc]# /application/php/sbin/php-fpm



  • 5.检查启动进程与侦听端口号

1
2
3
4
5
6
7
[iyunv@leaf etc]# ps -ef | grep php-fpm
root     129256      1  0 09:05 ?        00:00:00 php-fpm: master process (/application/php5.3.27/etc/php-fpm.conf)
nginx    129257 129256  0 09:05 ?        00:00:00 php-fpm: pool www            
nginx    129258 129256  0 09:05 ?        00:00:00 php-fpm: pool www            
root     129260  13743  0 09:06 pts/1    00:00:00 grep php-fpm
[iyunv@leaf etc]# netstat -lntup | grep 9000
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      129256/php-fpm




    至此,PHP也安装完成了!LNMP的各个组件都安装好了,下面就要对LNMP环境进行测试了。




5.LNMP环境测试

(1)配置Nginx支持PHP程序请求访问

  • 1.查看当前Nginx配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[iyunv@leaf etc]# cd /application/nginx/conf/
[iyunv@leaf conf]# cat nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    include extra/blog.conf;
}
[iyunv@leaf conf]# cat extra/blog.conf
server {
        listen       80;
        server_name  blog.xpleaf.org;
        location / {
            root   html/blog;
            index  index.html index.htm;
        }
    }



  • 2.修改extra/blog.conf配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[iyunv@leaf conf]# cat extra/blog.conf
server {
        listen       80;
        server_name  blog.xpleaf.org;
        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;
        }
    }



  • 3.检查并启动Nginx

1
2
3
4
[iyunv@leaf 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
[iyunv@leaf conf]# /application/nginx/sbin/nginx -s reload




(2)测试LNMP环境是否生效

  • 1.配置域名站点目录

1
2
3
4
[iyunv@leaf conf]# cd /application/nginx/html/blog/
[iyunv@leaf blog]# echo "<?php phpinfo(); ?>" >test_info.php
[iyunv@leaf blog]# cat test_info.php
<?php phpinfo(); ?>



wKiom1i6L6HhFqtaAAGt2buX8k8440.png

(3)测试PHP连接MySQL是否正常

  • 1.编辑text_mysql.php

1
2
3
4
5
6
7
8
9
[iyunv@leaf blog]# cat test_mysql.php
<?php
        $link_id=mysql_connect('localhost', 'root', '123456');
        if($link_id){
                echo "mysql succesful by xpleaf !";
        }else{
                echo mysql_error();
        }
?>



wKioL1i6L9XTByb3AAB77elSaKg380.png
    至此,LNMP环境搭建与测试完成了,下面就可以开始部署WordPress了!




6.部署WordPress


(1)MySQL数据库准备


  • 1.登陆mysql

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[iyunv@leaf blog]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.5.54 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.创建数据库wordpress

1
2
3
4
5
6
7
8
9
10
11
12
13
mysql> create database wordpress;
Query OK, 1 row affected (0.32 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| wordpress          |
+--------------------+
4 rows in set (0.00 sec)



  • 3.创建wordpress blog管理用户

1
2
3
4
5
6
7
8
9
10
mysql> grant all on wordpress.* to wordpress@'localhost' identified by '123456';
Query OK, 0 rows affected (0.08 sec)
mysql> show grants for wordpress@'localhost';
+------------------------------------------------------------------------------------------------------------------+
| Grants for wordpress@localhost                                                                                   |
+------------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'wordpress'@'localhost' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |
| GRANT ALL PRIVILEGES ON `wordpress`.* TO 'wordpress'@'localhost'                                                 |
+------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)



  • 4.刷新MySQL用户权限

1
2
mysql> flush privileges;
Query OK, 0 rows affected (0.31 sec)



  • 5.检查MySQL登录用户


1
2
3
4
5
6
7
8
9
mysql> select user,host from mysql.user;
+-----------+-----------+
| user      | host      |
+-----------+-----------+
| root      | 127.0.0.1 |
| root      | localhost |
| wordpress | localhost |
+-----------+-----------+
3 rows in set (0.00 sec)




(2)Nginx配置准备

  • 1.修改blog.conf配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[iyunv@leaf conf]# cat extra/blog.conf
server {
        listen       80;
        server_name  blog.xpleaf.org;
        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;
            include fastcgi.conf;
        }
    }
    # 相比前面的配置文件,只是在/下添加了index.php
    # 不过需要注意的是,index.php一定要放在index关键字之后,
    # 这样访问blog.xpleaf.org时,才会打开我们的WordPress页面



  • 2.重启Nginx服务

1
[iyunv@leaf conf]# /application/nginx/sbin/nginx -s reload




(3)配置WordPress

  • 1.获取WordPress安装包

1
2
3
[iyunv@leaf tools]# wget
[iyunv@leaf tools]# ls -lh wordpress-4.7.2-zh_CN.tar.gz
-rw-r--r--. 1 root root 8.1M 1月  28 08:53 wordpress-4.7.2-zh_CN.tar.gz



  • 2.解压缩与配置站点目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[iyunv@leaf tools]# cp wordpress-4.7.2-zh_CN.tar.gz /application/nginx/html/blog/
[iyunv@leaf tools]# cd /application/nginx/html/blog/
[iyunv@leaf blog]# tar zxf wordpress-4.7.2-zh_CN.tar.gz
[iyunv@leaf blog]# ls
index.html     test_mysql.php  wordpress-4.7.2-zh_CN.tar.gz
test_info.php  wordpress
[iyunv@leaf blog]# rm -rf test_* wordpress-4.7.2-zh_CN.tar.gz    # 删除无用的文件
[iyunv@leaf blog]# ls
index.html  wordpress
[iyunv@leaf blog]# mv wordpress/* ./    # 将wordpress程序移到当前blog目录下
[iyunv@leaf blog]# ls
index.html       wp-admin              wp-includes        wp-signup.php
index.php        wp-blog-header.php    wp-links-opml.php  wp-trackback.php
license.txt      wp-comments-post.php  wp-load.php        xmlrpc.php
readme.html      wp-config-sample.php  wp-login.php
wordpress        wp-content            wp-mail.php
wp-activate.php  wp-cron.php           wp-settings.php
[iyunv@leaf blog]# ls -l
总用量 196
-rw-r--r--.  1 root   root     30 3月   4 04:54 index.html
-rw-r--r--.  1 nobody 65534   418 9月  25 2013 index.php
-rw-r--r--.  1 nobody 65534 19935 1月   3 02:51 license.txt
-rw-r--r--.  1 nobody 65534  6956 1月  28 08:53 readme.html
drwxr-xr-x.  2 nobody 65534  4096 3月   4 09:50 wordpress
......



  • 3.对blog下所有文件授予nginx用户和组的权限

1
2
3
4
5
6
7
8
9
[iyunv@leaf blog]# chown -R nginx.nginx ../blog/
[iyunv@leaf blog]# ls -l
总用量 196
-rw-r--r--.  1 nginx nginx    30 3月   4 04:54 index.html
-rw-r--r--.  1 nginx nginx   418 9月  25 2013 index.php
-rw-r--r--.  1 nginx nginx 19935 1月   3 02:51 license.txt
-rw-r--r--.  1 nginx nginx  6956 1月  28 08:53 readme.html
drwxr-xr-x.  2 nginx nginx  4096 3月   4 09:50 wordpress
......




(4)安装WordPress


    在宿主机浏览器上输入地址:http://blog.xpleaf.org,如下:
wKiom1i6KN-CvwskAAGp5-uFYR4799.png

    接下来的安装都是非常人性化的,点击“现在就开始”,出现下面的页面:
wKioL1i6KWqitsupAAFqk-sWf04902.png

    填好信息后,点击“提交”,如下:
wKiom1i6KaDy4w7TAADlXnh_If4404.png

    点击“进行安装”,接下来就会让我们填写一些信息,如下:
wKioL1i6Ktizgf4IAAFs9TRk9ig999.png

    点击“安装WordPress”,之后就会显示如下页面:
wKiom1i6K0PgeKWaAAFlUb7wwBo929.png

    显示上面的页面,就说明我们的WordPress安装成功了!接下来就可以好好管理自己的个人WordPress博客站点了!




7.下一步要做什么

    可以在云主机上,如腾讯云或者阿里云上搭建LNMP环境,再部署一个WordPress博客程序,为了达到域名访问的效果,可以购买一个域名,然后自己搭建DNS服务器,这会是非常不错的体验!

    接下来就可以考虑对LNMP进行优化了。



8.参考资料

MySQL官方站点:https://www.mysql.com/downloads/


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-350378-1-1.html 上篇帖子: 源码编译lnmp之简介与nginx安装 下篇帖子: LAMP平台部署(原理、安装php环境、LAMP项目流程思路) WordPress 从零开始
累计签到:335 天
连续签到:1 天
发表于 2017-3-6 15:01:03 | 显示全部楼层
老男孩老师的徒弟!!!

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表