第1章 LNMP单机环境安装
1.Nginx安装
groupadd www -g 1000
useradd www -s /sbin/nologin -M -u 1000 -g 1000
id www
cat > /etc/yum.repos.d/nginx.repo << 'EOF'
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
EOF
yum makecache
yum install nginx -y
rm -rf /etc/nginx/conf.d/default.conf
systemctl start nginx
systemctl enable nginx
sed -i '/^user/c user www;' /etc/nginx/nginx.conf
2.PHP安装
cat > /etc/yum.repos.d/php74.repo << 'EOF'
[remi-php74]
name=Remi's PHP 7.4 RPM repository for Enterprise Linux 7 - $basearch
baseurl=https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/7/php74/$basearch/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi
[remi-safe]
name=Safe Remi's RPM repository for Enterprise Linux 7 - $basearch
baseurl=https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/7/safe/$basearch/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi
EOF
yum makecache
yum install -y php php-fpm php-cli php-gd php-mysqlnd php-pdo php-mcrypt php-xml php-opcache php-pecl-redis5
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 start php-fpm
systemctl enable php-fpm
3.MySQL安装配置
yum install mariadb-server mariadb -y
systemctl start mariadb
systemctl enable mariadb
4.检查
ps -ef|egrep "nginx|php|mysql"
02-mysql光速入门
mysql基础命令:
1.设置密码
mysqladmin password
2.使用账号密码连接
mysql -uroot -p123
3.查看数据库
> show databases;
4.切换到数据库里
> use mysql;
5.查看库里面有哪些表
> show tables;
6.查询当前数据库有哪些用户
> select user,host from mysql.user;
查询 字段 从 库.表;
MariaDB [(none)]> select user,host from mysql.user;
+------+-----------+
| user | host |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1 |
| | localhost |
| root | localhost |
| | web-7 |
| root | web-7 |
+------+-----------+
7.退出登录
> exit
03-LNMP调试
第1章 Nginx和PHP调试
1.配置nginx
cat > /etc/nginx/conf.d/php.conf << 'EOF'
server {
server_name www.oldboy.com;
listen 80;
root /code;
index index.php index.html;
location ~ \.php$ {
root /code;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
EOF
nginx -t
systemctl restart nginx
2.创建php测试文件
mkdir /code -p
cat > /code/info.php << 'EOF'
<?php
phpinfo();
?>
EOF
chown -R www:www /code/
3.访问测试
http://www.oldboy.com/info.php
第2章 nginx+php+mysql测试
1.创建php测试文件
cat > /code/mysql.php << 'EOF'
<?php
$servername = "localhost";
$username = "root";
$password = "123";
// 创建连接
$conn = mysqli_connect($servername, $username, $password);
// // 检测连接
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "php 连接 MySQL 数据库成功";
?>
EOF
2.访问测试
http://www.oldboy.com/mysql.php
04-部署博客wordpress
第1章 部署博客Wordpress
1.nginx配置
rm -rf /etc/nginx/conf.d/*
cat > /etc/nginx/conf.d/wordpress.conf << 'EOF'
server {
listen 80;
server_name blog.oldboy.com;
root /code/wordpress;
index index.php index.html;
location ~ \.php$ {
root /code/wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
EOF
nginx -t
systemctl restart nginx
2.下载解压wordpress到代码目录
rm -rf /code/
mkdir -p /code
cd /code/
wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz
tar xf wordpress-4.9.4-zh_CN.tar.gz
chown -R www:www /code/
3.创建数据库
mysql -uroot -p123 -e 'create database wordpress;'
mysql -uroot -p123 -e 'show databases;'
4.测试访问
blog.oldboy.com
05部署问答系统wecenter
1.nginx配置
cat > /etc/nginx/conf.d/zh.conf << 'EOF'
server {
listen 80;
server_name zh.oldboy.com;
root /code/zh;
index index.php index.html;
location ~ \.php$ {
root /code/zh;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
EOF
nginx -t
systemctl restart nginx
2.下载解压wordpress到代码目录
cd /code/
mkdir zh
cd zh
lz
unzip WeCenter_3-6-0.zip
chown -R www:www /code/
3.创建数据库
mysql -uroot -p123 -e 'create database zh;'
mysql -uroot -p123 -e 'show databases;'
4.删除与微博接口通信代码
sed -i '126,137d' /code/zh/install/index.php
4.测试访问
zh.oldboy.com
先在web-7上查找zh和wordpross图片存储的位置
[root@web-7 /]# find / -type f -name "*.jpg"
找到位置
/code/wordpress/wp-content/uploads/2021/11/fangzhi-100x100.jpg
/code/zh/uploads/article/20211126/7c0988ed52cb308b203b1477e1a1237c.jpg
挂载目录
web-7 /code/wordpress/wp-content/uploads/ 挂载到 nfs-31的/data/目录
web-7/code/zh/uploads/article/目录挂载到nfs-31的/backup/目录
安装图片备份
nfs-31主机安装nfs-utils
NFS服务端-安装
yum install nfs-utils -y
NFS服务端-配置
cat > /etc/exports << EOF
/data 172.16.1.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
/backup 172.16.1.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
EOF
创建用户和目录
groupadd -g 1000 www
useradd -u 1000 -g 1000 -s /sbin/nologin -M www
mkdir /data /backup -p
chown -R www:www /data /backup
ls -ld /data
ls -ld /backup
启动并查看状态
systemctl start rpcbind
systemctl start nfs
systemctl is-active rpcbind
systemctl is-active nfs
测试
showmount -e 172.16.1.31
web-7安装nfs-utils客户端
NFS客户端-安装
yum install nfs-utils -y
NFS客户端新建目录及用户
groupadd -g 1000 www
useradd -g 1000 -u 1000 -s /sbin/nologin -M www
mkdir /data /backup -p
chown -R www:www /data 这个可以不要创建
chown -R www:www /backup 这个可以不要创建
ls -ld /data
ls -ld /backup
测试-非常重要
showmount -e 172.16.1.31
远程临时挂载NFS共享目录
mount -t nfs 172.16.1.31:/data /data
写入开机启动挂载并载入开机挂载
cat >> /etc/fstab << 'EOF'
172.16.1.31:/data /code/wordpress/wp-content/uploads nfs defaults 0 0
172.16.1.31:/backup /code/zh/uploads/article nfs defaults 0 0
EOF
mount -a
backup-41安装rsync服务端
1.安装rsync
yum install rsync -y
2.创建配置文件
cat > /etc/rsyncd.conf << 'EOF'
uid = www
gid = www
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
[backup]
path = /backup
[data]
path = /data
EOF
3.创建用户以及数据目录
groupadd www -g 1000
useradd www -u 1000 -g 1000 -M -s /sbin/nologin
mkdir -p /data
mkdir -p /backup
chown -R www:www /data/ /backup/
4.创建密码文件并更改授权
echo "rsync_backup:oldboy" > /etc/rsync.passwd
chmod 600 /etc/rsync.passwd
5.启动服务
systemctl start rsyncd
nfs-31端安装rsync客户端
1.安装
yum install rsync -y
2.配置密码文件及授权
echo "oldboy" > /etc/rsync.pass
chmod 600 /etc/rsync.pass
3.测试传输 测试方法1:
rsync --password-file=/etc/rsync.pass -avz /data/ rsync_backup@10.0.0.41::data
nfs-31端安装lsyncd
nfs-31上lsyncd安装
yum install lsyncd -y
修改配置文件(同步/data /backup目录)
cat > /etc/lsyncd.conf << 'EOF'
settings {
logfile = "/var/log/lsyncd/lsyncd.log",
statusFile = "/var/log/lsyncd/lsyncd.status",
inotifyMode = "CloseWrite",
maxProcesses = 8,
}
sync {
default.rsync,
source = "/data",
target = "rsync_backup@172.16.1.41::data",
delete = true,
exclude = { ".*" },
delay = 1,
rsync = {
binary = "/usr/bin/rsync",
archive = true,
compress = true,
verbose = true,
password_file = "/etc/rsync.pass",
_extra = {"--bwlimit=2"}
}
}
settings {
logfile = "/var/log/lsyncd/lsyncd.log",
statusFile = "/var/log/lsyncd/lsyncd.status",
inotifyMode = "CloseWrite",
maxProcesses = 8,
}
sync {
default.rsync,
source = "/backup",
target = "rsync_backup@172.16.1.41::backup",
delete = true,
exclude = { ".*" },
delay = 1,
rsync = {
binary = "/usr/bin/rsync",
archive = true,
compress = true,
verbose = true,
password_file = "/etc/rsync.pass",
_extra = {"--bwlimit=2"}
}
}
EOF
启动命令
systemctl start lsyncd