1.LNMP架构
LNMP:Linux+Nginx+MySQL+PHP服务器架构。
2.安装LNMP架构环境
yum remove php-mysql-5.4 php php-fpm php-common #卸载之前的旧版本
vim /etc/yum.repos.d/php.repo
------------------/etc/yum.repos.d/php.repo-------------------
[webtatic-php]
name = php Repository
baseurl = http://us-east.repo.webtatic.com/yum/el7/x86_64/
gpgcheck = 0
------------------/etc/yum.repos.d/php.repo结束----------------
yum -y install php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-gd php71w-mcrypt php71w-mbstring php71w-pdo php71w-xml php71w-fpm php71w-mysqlnd php71w-opcache php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb
systemctl enable php-fpm
systemctl start php-fpm
yum install mariadb mariadb-server -y #安装Mariadb数据库
systemctl start mariadb
systemctl enable mariadb
vim /etc/nginx/conf.d/php.conf
------------------/etc/nginx/conf.d/php.conf----------------
server {
listem 80;
server_name php.com;
root /code/php;
location / {
index index.php;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
------------------/etc/nginx/conf.d/php.conf结束----------------
mkdir -p /code/php
vim /code/php/index.php
------------------/code/php/index.php-------------------
<?php
phpinfo();
?>
------------------/code/php/index.php结束----------------
在Windows的c:\windows\system32\drives\etc\hosts中添加:
10.0.0.7 php.com
在Windows浏览器中输入域名php.com即可访问。
php连接到数据库
systemctl start mariadb
systemctl enable mariadb
mysqladmin -uroot -poldxu.com #设置MySQL的账户密码
mysql -uroot -poldxu.com #登陆数据库
#按CTRL+c退出MySQL
vim /code/mysql.php
-------------------/code/mysql.php-----------------------
<?php
//设定变量
$servername = "localhost";
$username = "root";
$password = "oldxu.com";
// 创建连接
$conn = mysqli_connect($servername, $username, $password);
// 检测连接
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "php连接MySQL数据库成功";
?>
-------------------/code/mysql.php结束--------------------
3.搭建wordpress与wecenter
#准备word press
mkdir -p /code/wordpress
tar xf wordpress-5.2.3-zh_CN.tar.gz -C /code/wordpress
#修改nginx php的进程用户为www
groupadd -g666 www
useradd -u666 -g 666 www
sed -i '/^user/c user www;' /etc/nginx/nginx.conf
systemctl restart nginx
sed -i '/^user/c user www' /etc/php-fpm.d/www.conf
sed -i '/^group/c group www' /etc/php-fpm.d/www.conf
systemctl restart php-fpm
chown -R www.www /code/wordpress #修改目录权限
#在数据库中创建word press库
mysql -uroot -poldxu.com
------------------mysql------------------------
create database wordpress;
show databases;
------------------mysql结束-----------------------
#配置nginx,添加wordpress网站
vim /etc/nginx/conf.d/blog.conf
-----------------/etc/nginx/conf.d/blog.conf---------------
server {
listen 80;
server_name blog.com;
root /code/wordpress;
client_max_body_size 100m; #限制上传100m
location / {
index index.php;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_param;
}
}
--------------/etc/nginx/conf.d/blog.conf结束---------------
nginx -t
systemctl reload nginx
Windows的c:\windows\system32\drives\etc\hosts中添加:
10.0.0.7 wordpress.com
在浏览器中输入域名blog.com即可访问。
mkdir /code/wecenter
unzip Wecenter_3-5-0.zip -d /code/wecenter/
chown -R www.www /code/wecenter
#数据库中创wecenter库
mysql -uroot -poldxu.com
------------------mysql------------------------
create database wecenter;
show databases;
------------------mysql结束-----------------------
#配置nginx文件
vim /etc/nginx/conf.d/wecenter.conf
--------------/etc/nginx/conf.d/wecenter.conf---------------
server {
listen 80;
server_name wecenter.com;
root /code/wecenter;
location / {
index index.php;
}
#解析php
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
--------------/etc/nginx/conf.d/wecneter.conf结束---------------
nginx -t
systemctl reload nginx
Windows的c:\windows\system32\drives\etc\hosts中添加:
10.0.0.7 wecenter.com
在浏览器中输入域名wecenter.com即可访问。
4.数据库独立到服务器
#1.导出数据库数据
mysqldump -uroot -poldxu.com -A > /opt/data.sql
scp /opt/data.sql root@10.0.0.51:~
systemctl disable mariadb
systemctl stop mariadb
#3.测试是否能远程登陆数据库
mysql -h 172.16.1.51 -uall -poldxu.com
#4.将10.0.0.7上应用程序连接的数据库地址指向 172.16.1.51
vim /code/wordpress/wp-config.php
----------------/code/wordpress/wp-config.php-----------------------
/** WordPress数据库的名称 */
define( 'DB_NAME', 'wordpress' );
/** MySQL数据库用户名 */
define( 'DB_USER', 'all' );
/** MySQL数据库密码 */
define( 'DB_PASSWORD', 'oldxu.com' );
/** MySQL主机 */
define( 'DB_HOST', '172.16.1.51' );
----------------/code/wordpress/wp-config.php结束--------------------
find /code/wecenter -type f | xargs grep -R "oldxu.com" #查找wecenter的配置文件
vim /code/wecenter/system/config/database.php
--------------/code/wecenter/system/config/database.php--------------
<?php
$config['charset'] = 'utf8mb4';
$config['prefix'] = 'aws_';
$config['driver'] = 'MySQLi';
$config['master'] = array (
'charset' => 'utf8mb4',
'host' => '172.16.1.51',
'username' => 'all',
'password' => 'oldxu.com',
'dbname' => 'wecenter',
);
$config['slave'] = false;
--------------/code/wecenter/system/config/database.php结束-----------
数据库服务器10.0.0.51
yum install -y mariadb mariadv-server
systemctl start mariadb
systemctl enable mariadb
#2.输入重定向(导入数据)
mysql < ~/data.sql
systemctl restart mariadb #数据生效
mysql -uroot -poldxu.com
------------------mysql------------------------
show databases;
#授权远程登陆所有的库.表给用户"all",来自所有网段
grant all privileges on *.* to 'all'@'%' identified by 'oldxu.com';
------------------mysql结束-----------------------
5.搭建NFS共享目录
NFS服务器(10.0.0.31):
yum install nfs-utils -y
mkdir /data/blog
vim /etc/exports
-------------------/etc/exports------------------------
/data/blog 172.16.1.0/24(rw,sync,anonuid=666,anongid=666)
-------------------/etc/exports------------------------
groupadd -g666 www
useradd -u666 -g666 www
chown -R www.www /data/blog
systemctl start nfs
systemctl enable nfs
web01服务器(10.0.0.7):
#将word press的上传目录挂载为NFS的/data/blog
mount -t nfs 172.16.1.31:/data/blog /code/wordpress/wp-content/uploads/
web02服务器(10.0.0.8):
#将word press的上传目录挂载为NFS的/data/blog
mount -t nfs 172.16.1.31:/data/blog /code/wordpress/wp-content/uploads/
测试:在web01上登陆该用户上传资源,在web02登陆该用户查看资源是否存在。