LNMP平台环境部署及应用
目的:(1)部署lnmp实现多个虚拟主机
(2)基于LNMP平台部署wordpress和phpmyadmin
(3)为其中一个主机提供https
环境:
192.168.1.104-------->nginx
192.168.1.110-------->php-fpm
192.168.1.113-------->mariadb
一、部署LNMP环境
nginx安装配置(192.168.1.104)
1、安装开发包组及依赖包
1
2
# yum -y groupinstall Server Platform Development Development Tools
# yum -y install pcre-devel openssl-devel zlib-devel
2、编译安装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
# tar xf nginx-1.10.0.tar.gz
# cd nginx-1.10.0/
# ./configure
--prefix=/usr/local/nginx
--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.pid
--lock-path=/var/run/nginx.lock
--user=nginx
--group=nginx
--with-http_ssl_module
--with-http_v2_module
--with-http_dav_module
--with-http_stub_status_module
--with-stream_ssl_module
--with-threads
--with-file-aio
--with-http_realip_module
--with-http_addition_module
--with-http_sub_module
--with-http_flv_module
--with-http_mp4_module
--with-http_gunzip_module
--with-http_gzip_static_module
--with-http_random_index_module
--with-http_secure_link_module
--with-http_auth_request_module
--with-stream --with-http_slice_module
--http-client-body-temp-path=/var/cache/nginx/client_temp
--http-proxy-temp-path=/var/cache/nginx/proxy_temp
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp
--http-scgi-temp-path=/var/cache/nginx/scgi_temp
# make -j 4 && make install
3、启动服务查看是否正常,此处需要注意用户是Nginx如果没人需要创建,否则服务无法启动。
1
2
3
# useradd -r nginx ##创建nginx服务用户
# mkdir -p /var/cache/nginx/client_temp##启动提示无此路径,创建即可
# nginx ##启动服务
4、配置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
server {
listen 80;
server_namewww.magedu.com;
location / {
root /web/host1;
indexindex.php index.html index.htm;
}
location ~ \.php$ {
root /web/host1;
fastcgi_pass 192.168.1.110:9000;
fastcgi_indexindex.php;
fastcgi_paramSCRIPT_FILENAME/web/host1/$fastcgi_script_name;
include fastcgi_params;
}
}
server {
listen 80;
server_namewww.maweijun.com;
location / {
root /web/host2;
indexindex.php index.html index.htm;
}
location ~ \.php$ {
root /web/host2;
fastcgi_pass 192.168.1.110:9000;
fastcgi_indexindex.php;
fastcgi_paramSCRIPT_FILENAME/web/host2/$fastcgi_script_name;
include fastcgi_params;
}
}
5、建立首页及其路径
1
2
3
4
5
# mkdir -p /web/host1
# mkdir -p /web/host2
# echo "host1" >/web/host1/index.html
# echo "host2" >/web/host2/index.html
6、检查配置文件是否正确,然后重新加载文件,测试即可
1
2
3
4
# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# nginx -s reload
部署php-fpm服务(192.168.1.110)
1、安装php-fpm程序
1
2
3
# yum -y install php-fpm
# yum -y install php-mysql
# yum -y install php-mbstring
2、编辑主机配置文件,修改相关配置选项
1
2
3
# vim /etc/php-fpm.d/
listen = 192.168.1.110:9000 ###监听php-fpm能够与外部通信的地址
listen.allowed_clients = 192.168.1.104####允许的客户端主机,此处指httpd主机
3、启动服务
1
# systemctl start php-fpm.service
4、建立与httpd服务主机上相同的网页路径,测试php和httpd连接是否正常。
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
# mkdir -p /web/host1
# mkdir -p /web/host2
# vim /web/host1/index.php
host1
<?php
$conn = mysql_connect('192.168.1.113','test','test');
if ($conn)
echo "mysql is ok";
else
echo "mysql is bad";
phpinfo();
?>
# vim /web/host1/index.php
host 2
<?php
$conn = mysql_connect('192.168.1.113','test','test');
if ($conn)
echo "mysql is ok";
else
echo "mysql is bad";
phpinfo();
?>
部署mariadb服务(192.168.1.113)
1、安装程序
1
2
# yum -y install mariadb-server
# systemctl start mariadb
2、授权用户,测试mariadb和php是否连接正常。
1
MariaDB [(none)]> grant all on test.* to 'test'@'192.168.%.%' identified by 'test';
1
2
3
4
5
MariaDB [(none)]> create database wpdb;####用于WordPress使用
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> grant all on wpdb.* to 'wpuser'@'192.168.%.%' identified by 'wppass';
Query OK, 0 rows affected (0.00 sec)
1
2
3
4
5
MariaDB [(none)]> create database phpmyadmin;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> grant all on phpmyadmin.* to 'phpuser'@'192.168.%.%' identified by 'phppass';
Query OK, 0 rows affected (0.00 sec)
3、测试
ok此时我们的LNMP环境就部署完成了。
二、基于LNMP平台部署WordPress和phpmyadmin应用
1、部署WordPress
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# unzip wordpress-4.3.1-zh_CN.zip
# mv wordpress /web/host1/
# cd /web/host1/WordPress
# cp wp-config-sample.php wp-config.php
# vim wp-config.php
/** WordPress数据库的名称 */
define('DB_NAME', 'wpdb');
/** MySQL数据库用户名 */
define('DB_USER', 'wpuser');
/** MySQL数据库密码 */
define('DB_PASSWORD', 'wppass');
/** MySQL主机 */
define('DB_HOST', '192.168.1.113');
/** 创建数据表时默认的文字编码 */
define('DB_CHARSET', 'utf8');
1
# scp -r wordpress/ root@192.168.1.104:/web/host1/ ###复制一份给httpd
2、部署phpmyadmin
1
2
3
4
5
6
7
8
9
# unzip phpMyAdmin-4.4.14.1-all-languages
# mv phpMyAdmin-4.4.14.1-all-languages /web/host2/phpmyadmin
# cd phpmyadmin/libraries/
# vim config.default.php
$cfg['blowfish_secret'] = 'Tfg6ORIzhZu/uA';
$cfg['Servers'][$i]['host'] = '192.168.1.113';
$cfg['Servers'][$i]['user'] = 'phpuser';
$cfg['Servers'][$i]['password'] = 'phppass';
1
# scp -r phpmyadmin/ root@192.168.1.104:/web/host2/
部署过程中phpmyadmin访问出现了session没有缓存,,此时只需要在/etc/php.ini中修改缓存路径,然后修改下权限就可了
1
2
3
4
session.save_path = "/var/lib/php/session"
# ll -d /var/lib/php/session/
drwxrwxrwx 2 root nginx 50 Jul 18 22:40 /var/lib/php/session/
三、为其中一个站点设置为https方式访问
(1)建立CA
1
2
3
4
5
6
7
# (umask 077; openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048)
# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -out
/etc/pki/CA/cacert.pem -days 3655
# touch {serial,index.txt}
# echo 01> serial
(2)httpd服务生成密钥及生成请求证书
1
2
3
4
5
6
7
8
# mkdir /etc/nginx/ssl
# cd /etc/nginx/ssl/
# (umask 077;openssl genrsa -out /etc/nginx/ssl/nginx.key 2048)
# openssl req -new -key /etc/nginx/ssl/nginx.key -out
/etc/nginx/ssl/nginx.csr -days 365
scp nginx.csr root@192.168.1.110:/
(3)CA上签署http证书申请:
1
2
3
# openssl ca -in /nginx.csr -out /etc/pki/CA/certs/nginx.crt -days 365
# scp nginx.crt root@192.168.1.104:/etc/nginx/ssl
(4)编辑httpd配置文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
server {
listen 80;
listen 443 ssl; ###指定使用ssl,且监听443端口
ssl_certificate/etc/nginx/ssl/nginx.crt; ###指定公钥路径
ssl_certificate_key/etc/nginx/ssl/nginx.key; ###指定私钥路径
server_namewww.maweijun.com;
location / {
root /web/host2;
indexindex.php index.html index.htm;
}
location ~ \.php$ {
root /web/host2;
fastcgi_pass 192.168.1.110:9000;
fastcgi_indexindex.php;
fastcgi_paramSCRIPT_FILENAME/web/host2/$fastcgi_script_name;
include fastcgi_params;
}
}
(5)测试
页:
[1]