很多人在练习部署LNMP环境的时候,大都数是部署在同一个虚拟机上面的。但是实际工作中,我们一般都是分离部署的。
今天我就用3台虚拟机,部署下LNMP环境。以供参考!
网络拓扑图:
首先准备3台虚拟机:
nginx:192.168.1.214
php:192.168.1.202
mysql:192.168.1.217
首先安装ngix(192.168.1.214):
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#解决依赖 yum install -y gcc,openssl-devel,pcre-devel,zilb-devel pcre-devel #关闭防火墙和修改selinux service iptables stop chkconfig iptables off sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config |
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
|
#添加一个nginx用户 [root@nginx ~] # groupadd -g 108 -r nginx [root@nginx ~] # useradd -u 108 -r -g 108 nginx [root@nginx ~] # tar zxvf nginx-1.6.0.tar.gz [root@nginx ~] # cd nginx-1.6.0 [root@nginx ~] # ./configure --prefix= /usr/local/ --sbin-path= /usr/sbin/nginx/ --conf-path= /etc/nginx/nginx .conf/ --error-log-path= /var/log/nginx/error .log/ --http-log-path= /var/log/nginx/access .log/ --pid-path= /var/run/nginx/nginx .pid/ --lock-path= /var/lock/nginx .lock/ --user=nginx/ --group=nginx/ --with-http_ssl_module/ --with-http_flv_module/ --with-http_stub_status_module/ --with-http_gzip_static_module/ --http-client-body-temp-path= /var/tmp/nginx/client/ --http-proxy-temp-path= /var/tmp/nginx/proxy/ --http-fastcgi-temp-path= /var/tmp/nginx/fcgi/ --http-uwsgi-temp-path= /var/tmp/nginx/uwsgi/ --http-scgi-temp-path= /var/tmp/nginx/scgi/ --with-pcre make && make install |
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
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
92
93
94
95
96
97
98
99
|
[root@nginx ~] # cat /etc/init.d/nginx #!/bin/sh # config: /etc/nginx/nginx.conf # config: /etc/sysconfig/nginx # pidfile: /var/run/nginx.pid # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx= "/usr/sbin/nginx" prog=$( basename $nginx) NGINX_CONF_FILE= "/etc/nginx/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile= /var/lock/subsys/nginx make_dirs() { # make required directories user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=([^ ]*).*/1/g' -` options=`$nginx -V 2>&1 | grep 'configure arguments:' ` for opt in $options; do if [ ` echo $opt | grep '.*-temp-path' ` ]; then value=` echo $opt | cut -d "=" -f 2` if [ ! -d "$value" ]; then # echo "creating" $value mkdir -p $value && chown -R $user $value fi fi done } start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 make_dirs echo -n $ "Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval - eq 0 ] && touch $lockfile return $retval } stop() { echo -n $ "Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval - eq 0 ] && rm -f $lockfile return $retval } restart() { configtest || return $? stop sleep 1 start } reload() { configtest || return $? echo -n $ "Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status > /dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $ "Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2 esac |
添加执行权限:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@nginx ~] # chmod +x /etc/init.d/nginx #添加脚本到开机启动项 [root@nginx ~] # chkconfig --add nginx [root@nginx ~] # chkconfig nginx on [root@nginx ~] # chkconfig nginx --list nginx 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭 #启动nginx [root@nginx ~] # service nginx start 正在启动 nginx: [确定] #查看端口是否正常 [root@nginx ~] # netstat -ntlp | grep :80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3889 /nginx # Nginx安装与配置完毕 |
#安装MySQL(192.168.1.217)
#注意:这里是直接解压MySQL的
1
2
3
4
|
#添加MySQL用户 [root@localhost ~] #groupadd -r mysql [root@localhost ~] #useradd -r -g mysql -s [root@localhost ~] #/sbin/nologin mysql |
1
2
3
4
5
6
7
|
#解压到指定路径 [root@localhost ~] #tar xf mysql-5.5.33-linux2.6-x86_64.tar.gz -C /usr/local/ [root@localhost ~] #cd /usr/local/ [root@localhost ~] #ln -sv /usr/local/mysql-5.5.33-linux2.6-x86_64/ /usr/local/mysql ` /usr/local/mysql ' -> `/usr/local/mysql-5.5.33-linux2.6-x86_64/' [root@localhost ~] #cd mysql [root@localhost ~] #chown -R root.mysql * |
1
2
3
4
5
6
|
#初始化数据库;并指定其用户和data目录 scripts /mysql_install_db --user=mysql--datadir= /Mysql/data ls /Mysql/data/ lost+found mysql mysql-bin.000001 mysql-bin.000002 mysql-bin.index performance_schema test |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#没有报错并生成以上文件即可 cp support-files /my-large .cnf /etc/my .cnf 配置一个配置文件 vi /etc/my .cnf 更改已下配置选项;具体选项根据自己需要配置 # The MySQL server [mysqld] port = 3306 socket = /tmp/mysql .sock skip-external-locking key_buffer_size = 256M max_allowed_packet = 1M table_open_cache = 256 sort_buffer_size = 1M read_buffer_size = 1M read_rnd_buffer_size = 4M myisam_sort_buffer_size = 64M thread_cache_size = 8 query_cache_size= 16M # Try number of CPU's*2 for thread_concurrency thread_concurrency = 4 datadir = /Mysql/data 新增一行 |
1
2
3
4
5
6
7
8
9
10
11
12
|
#提供一个服务脚本 cp support-files /mysql .server /etc/rc .d /init .d /mysqld chmod +x /etc/rc .d /init .d /mysqld chkconfig --add mysqld chkconfig mysqld on chkconfig --list mysqld mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off service mysqld start Starting MySQL... [ OK ] |
1
2
3
4
5
6
|
#用mysql命令启动成功后错误信息 vi /etc/profile .d /mysql .sh export PATH= /usr/local/mysql/bin :$PATH ln -sv /usr/local/mysql/include/ /usr/include/mysql ` /usr/include/mysql ' ->`/usr/local/mysql/include/' vi /etc/man .config |
1
2
3
4
5
6
7
8
9
|
定位到MANPATH MANPATH /usr/man MANPATH /usr/share/man MANPATH /usr/local/man MANPATH /usr/local/share/man MANPATH /usr/X11R6/man MANPATH /usr/local/mysql/man #添加一行 echo /usr/local/mysql/lib/ > /etc/ld .so.conf.d /mysql .conf |
#若是想要编译安装MySQL可以参考我之前的文章:
http://liangey.blog.51cto.com/9097868/1626945
安装PHP
1
|
yum install zlib libxml libjpegfreetype gd curl libiconv zlib-devel libxml2-devel libjpeg-devel freetype-devellibpng-devel gd-devel curl-devel libmcrypt-devel libxslt* bzip2 * -y |
1
2
3
4
5
6
7
8
9
10
11
|
#添加nginx用户 useradd nginx #安装libiconv-1.14库 tar zxvf libiconv-1.14. tar .gz cd libiconv-1.14 . /configure --prefix= /usr/local/libiconv echo $? make echo $? make install echo $? |
1
2
3
4
5
6
|
#安装libmcrypt-2.5.8.tar.gz库 tar zxvflibmcrypt-2.5.8. tar .gz cd libmcrypt-2.5.8 . /configure make make install |
1
2
3
4
5
6
|
#安装mhash-0.9.9.9 tar zxvf mhash-0.9.9.9. tar .gz cd mhash-0.9.9.9 . /configure make make install |
1
2
3
4
5
6
7
|
#安装mcrypt tar zxvf mcrypt-2.6.8. tar .gz cd mcrypt-2.6.8 /sbin/ldconfig . /configureLD_LIBRARY_PATH = /usr/local/lib make make install |
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
|
tar zxvf php-5.3.27. tar .gz cd php-5.3.27 . /configure--prefix = /application/php5 .3.27 --with-mysqli=mysqlnd / --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 -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/ -- enable - ftp make && make install |
1
2
3
|
#若make出错 /home/tools/php-5 .3.27 /sapi/cli/php :error while loading shared libraries: libmysqlclient.so.18: cannot open sharedobject file : No such file or directory make : *** [ext /phar/phar .php] 错误 127 |
1
2
3
4
5
|
#解决方法 ln -s /application/mysql/lib/libmysqlclient .so.18 /usr/lib64/ make make install ln -s /application/php5 .3.27 //application/php |
1
2
3
4
5
6
7
8
9
|
#配置PHP cp php.ini-production /application/php/lib/php .ini cd /application/php/etc/ vim php-fpm.conf #修改如下: user = nginx group = nginx listen = 192.168.1.202:9000 |
1
2
3
4
|
#启动php-fpm /etc/init .d /php-fpm start netstat -lntup| grep 9000 tcp 0 0 192.168.1.202:9000 0.0.0.0:* LISTEN 1422 /php-fpm |
#整合Nginx和PHP
#这里是在Nginx(192.168.1.214)下操作:
1
2
3
4
5
6
7
8
9
10
11
12
|
vim /etc/nginx/nginx .conf location / { root /www ; #更改目录 index index.php index.html index.htm; #添加一个index.php } location ~ .php$ { root /www ; #更改目录 fastcgi_pass 192.168.1.202:9000; #这里为PHP服务器的地址 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /scripts $fastcgi_script_name; include fastcgi_params; } |
1
2
3
|
vim /etc/nginx/fastcgi_params #添加以下这行: fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; |
1
2
3
4
5
6
7
8
|
#创建一个/www目录,并且创建一个test.php文件 mkdir /www chown nginx:nginx /www/ cd /www vim test .php <?php phpinfo(); ?> |
#注意:这里先不要重启nginx
#以下操作是在PHP:192.168.1.202中进行
1
2
3
4
5
6
7
|
mdkir /www chown -R nginx:nginx /www/ cd /www/ vim test .php <?php phpinfo(); ?> |
1
2
|
#重启php-fpm /etc/init .d /php-fpm restart |
1
2
|
#在nginx中重启nginx服务 /etc/init .d /nginx restart |
在浏览器中输入:192.168.1.214/test.php
出现以下页面,证明nginx和php整合成功了!
#最后是创建一个wordpress站点
1
2
3
4
5
6
7
8
|
创建站点,首先我们的nginx必须要跟MySQL是互通的。现在我们先在MySQL下授权nginx这台服务器能够访问数据库: #这里是在MySQL(192.168.1.217)下操作的: mysql -uroot -proot 登陆数据库 #创建一个数据库 mysql> create database wp; mysql> use wp; mysql> grant all privileges on wp.* to 'wpuser' @ '192.168.1.%' identified by '123456' ; mysql> flush privileges; |
1
2
3
|
#在nginx的/www目录下面解压wordpress-4.1-zh_CN.tar.gz [root@localhost www] #tar zxvf wordpress-4.1-zh_CN.tar.gz [root@localhost www] #cp -r wordpress/* . |
1
2
3
4
|
#在php的/www目录下面解压 wordpress-4.1-zh_CN. tar .gz [root@localhost www] #tar zxvf wordpress-4.1-zh_CN.tar.gz [root@localhost www] #cp -r wordpress/* . |
1
2
3
4
|
#重启nginx服务和php-fpm服务 /etc/init .d /nginx restart /etc/init .d /php-fpm restart |
#浏览器中输入:192.168.1.214,会出现设置的页面
本文出自 “9527” 博客,请务必保留此出处http://liangey.blog.51cto.com/9097868/1630038