基于LNMP架构搭建WordPress
目录
7.1 创建 nginx.service 文件 # vim /lib/systemd/system/nginx.service
7.3 查看Nginx的服务 # netstat -ntlp | grep nginx(查看端口)
7.4 查看Nginx的服务状态 # ps -ef | grep nginx
12.停止php-fpm并用systemctl启动,开机自启
一.WordPress简述
WordPress是一个开源的内容管理系统(CMS),用于创建和管理网站。它是使用PHP语言编写的,并使用MySQL数据库进行数据存储。具有大量的主题和插件,拥有庞大的社区支持,用户可以通过论坛、博客、教程等途径获取帮助和分享经验。总之,WordPress是一个强大而灵活的网站建设工具,适用于各种规模和类型的网站建设项目。无论是个人用户还是企业机构,都可以通过WordPress实现他们的网站目标。
二.基础架构
本次项目主要用到LNMP(Linux+Nginx+Mysql+PHP)架构。
三.准备工作
1.配置yum源
#备份原有的yum源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
#使用wget命令下载
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
注意:如果没有wget命令,请使用下面的命令
yum install -y wget
#清除yum缓存
[root@myhost1 ~]# yum clean all
#刷新缓存
[root@myhost1 ~]# yum makecache
2.关闭防火墙和selinux
# 关闭防火墙
[root@myhost1 ~]# systemctl stop firewalld
#开机不自启
[root@myhost1 ~]# systemctl disable firewalld
[root@myhost1 ~]# vim /etc/sysconfig/selinux
修改为SELINUX=disabled之后保存退出。
四.编译安装Nginx
1安装Nginx的依赖包
# yum -y install gcc gcc-c++ make zlib-devel pcre pcre-devel openssl-devel perl-devel perl-ExtUtils-Embed gd-devel
2.下载Nginx的安装包
# wget https://nginx.org/download/nginx-1.22.1.tar.gz
3.创建nginx运行用户
# useradd -s /sbin/nologin -M nginx
4.解压并配置nginx
[root@myhost1 ~]# tar zxvf nginx-1.22.1.tar.gz -C /usr/local/
[root@myhost1 ~]# cd /usr/local/nginx-1.22.1/
[root@myhost1 nginx-1.22.1]# ./configure \
--user=nginx \
--group=nginx \
--prefix=/usr/local/nginx \
--conf-path=/etc/nginx/nginx.conf \
--sbin-path=/usr/sbin/nginx \
--error-log-path=/var/log/nginx/nginx_error.log \
--http-log-path=/var/log/nginx/nginx_access.log \
--pid-path=/usr/local/nginx/run/nginx.pid
5.编译安装
[root@myhost1 nginx-1.22.1]# make && make install
6.测试nginx是否安装成功 # nginx -V
7.系统添加Nginx服务
7.1 创建 nginx.service 文件 # vim /lib/systemd/system/nginx.service
[root@myhost1 ~]# vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/usr/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
7.2以 systemctl 方式启动 Nginx
[root@myhost1 ~]# systemctl daemon-reload #重新加载系统服务
[root@myhost1 ~]# systemctl start nginx #启动nginx
7.3 查看Nginx的服务 # netstat -ntlp | grep nginx(查看端口)
7.4 查看Nginx的服务状态 # ps -ef | grep nginx
如图nginx服务已经启动。
7.5配置Nginx服务开机自启
[root@myhost1 ~]# systemctl enable nginx
nginx部署成功。
五.编译安装Mysql
1.清理安装环境
# yum erase mariadb mariadb-server mariadb-libs mariadb-devel -y
# userdel -r mysql
# rm -rf /etc/my*
# rm -rf /var/lib/mysql
2.创建mysql用户
useradd -r mysql -M -s /bin/nologin # -r创建一个系统用户 -M不创建家目录 -s 设置登录的shell
3.下载mysql安装包并解压
# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.27.tar.gz
# tar xzvf mysql-boost-5.7.27.tar.gz -C /usr/local/
4.安装编译工具
# yum -y install ncurses ncurses-devel openssl-devel bison gcc gcc-c++ make glibc automake autoconf
# yum -y install cmake
5.编译安装
[root@mysql-server ~]# cd /usr/local/mysql-5.7.27/
[root@mysql-server mysql-5.7.27]# cmake . \
-DWITH_BOOST=boost/boost_1_59_0/ \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ #指定安装目录
-DSYSCONFDIR=/etc \ #配置文件存放
-DMYSQL_DATADIR=/usr/local/mysql/data \ #数据目录
-DINSTALL_MANDIR=/usr/share/man \ #帮助文档
-DMYSQL_TCP_PORT=3306 \ #默认端口
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_READLINE=1 \
-DWITH_SSL=system \
-DWITH_EMBEDDED_SERVER=1 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1
# make && make install
(编译时间较长)
6.初始化
6.1 初始化mysql
[root@localhost mysql-5.7.27]# cd /usr/local/mysql
[root@localhost mysql]# chown -R mysql.mysql .
[root@localhost mysql]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data ---初始化完成之后,一定要记住提示最后的密码用于登陆或者修改密码
6.2 设置环境变量
[root@localhost mysql]# echo 'export PATH=/usr/local/mysql/bin:$PATH' >>/etc/profile
[root@localhost mysql]# source /etc/profile
[root@localhost mysql]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin[root@localhost mysql]# vim /etc/my.cnf
--如果打开文件有内容将文件中所有内容注释掉,在添加如下内容
[client]
port = 3306
socket = /tmp/mysql.sock
default-character-set = utf8[mysqld]
port = 3306
user = mysql
basedir = /usr/local/mysql #指定安装目录
datadir = /usr/local/mysql/data #指定数据存放目录
socket = /tmp/mysql.sock
character_set_server = utf8
7.启动mysql
[root@localhost mysql]# cd /usr/local/mysql
[root@localhost mysql]# ./bin/mysqld_safe --user=mysql &启动之后再按一下回车!即可后台运行
8.登录mysql
[root@localhost mysql]# /usr/local/mysql/bin/mysql -uroot -p'GP9TKGgY9i/8'
9.systemctl启动方式
拷贝启动脚本到/etc/init.d/目录下,并改名mysqld
[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld
重新加载系统服务
# systemctl daemon-reload
启动MySQL数据库
[root@localhost mysql]# systemctl stop mysqld --停止mysqld
[root@localhos mysql]# systemctl start mysqld --启动mysqld
设置开机自启
[root@localhos mysql]# systemctl enable mysqld
10.修改数据库密码并登录
[root@localhost ~]# mysqladmin -uroot -p'GP9TKGgY9i/8' password '123456'
[root@localhost ~]# mysql -uroot -p'123456'
六.编译安装PHP
1.下载PHP源码包并解压
[root@localhost ~]# wget https://www.php.net/distributions/php-7.3.6.tar.gz
[root@localhost ~]# tar xzvf php-7.3.6.tar.gz -C /usr/local/
如果php编译安装有问题实现以下操作
wget https://libzip.org/download/libzip-1.3.2.tar.gz
tar -xf libzip-1.3.2.tar.gz
cd libzip-1.3.2/
./configure
make
make install
2.安装依赖
# yum install -y apr* autoconf automake bison bzip2 bzip2* cloog-ppl cpp curl curl-devel fontconfig fontconfig-devel freetype freetype* freetype-devel gcc gcc-c++ gtk+-devel gd gettext gettext-devel glibc kernel kernel-headers keyutils keyutils-libs-devel krb5-devel libcom_err-devel libpng libpng-devel libjpeg* libsepol-devel libselinux-devel libstdc++-devel libtool* libgomp libxml2 libxml2-devel libXpm* libxml* libXaw-devel libXmu-devel libtiff libtiff* make mpfr ncurses* ntp openssl openssl-devel patch pcre-devel perl php-common php-gd policycoreutils telnet t1lib t1lib* nasm nasm* wget zlib-devel
# yum install libxml2-devel curl-devel
3.配置
[root@localhost ~]# cd /usr/local/php-7.3.6/
[root@ localhost php-7.3.6 ]# ./configure \
--prefix=/usr/local/php7 \ # 配置安装目录
--with-config-file-path=/usr/local/php7 \ # 启用 fpm 扩展
--with-config-file-scan-dir=/usr/local/php7/php.d \
--enable-mysqlnd \
--with-mysqli \
--with-pdo-mysql \
--enable-fpm \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--with-gd \
--with-iconv \
--enable-xml \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--enable-mbregex \
--enable-mbstring \
--enable-ftp \
--with-openssl \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-soap \
--without-pear \
--with-gettext \
--enable-session \
--with-curl \
--with-jpeg-dir \
--with-freetype-dir \
--enable-opcache
4.编译安装
# make -j 4 && make install #-j 指定cpu个数
5.创建 php.ini 配置文件
[root@localhost php-7.3.6]# cp php.ini-production /usr/local/php7/etc/php.ini
[root@localhost php-7.3.6]# vim /usr/local/php7/etc/php.ini
#1371行将注释打开 #php的Session存储目录
session.save_path = "/tmp"
6.设置php-fpm配置文件
[root@localhost ~]# cd /usr/local/php7/etc
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
[root@localhost etc]# vim php-fpm.conf
#17行将注释取消并修改
pid = /var/run/php-fpm.pid # php-fpm连接文件[root@localhost etc]# cd /usr/local/php7/etc/php-fpm.d/
[root@localhost php-fpm.d]# cp www.conf.default www.conf
[root@localhost php-fpm.d]# vim www.conf
user = nginx
group = nginx
listen = 127.0.0.1:9000
7.启动php-fpm
[root@localhost php-fpm.d]# /usr/local/php7/sbin/php-fpm
8.配置php-fpm系统环境变量
# vim /etc/profile
export PHP_HOME=/usr/local/php7
export PATH=$PATH:$PHP_HOME/bin:$PHP_HOME/sbin
9.重载环境变量
# source /etc/profile
10.配置php-fpm开机自启
# vim /lib/systemd/system/php-fpm.service
[Unit]
Description=php-fpm
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/php7/sbin/php-fpm
ExecStop=/bin/pkill -9 php-fpm
PrivateTmp=true
[Install]
WantedBy=multi-user.target
11.重载systemctl
systemctl daemon-reload
12.停止php-fpm并用systemctl启动,开机自启
# pkill php-fpm
# systemctl start php-fpm.service
# systemctl enable php-fpm.service
七.配置Nginx支持PhP
1.添加Nginx配置
[root@localhost ~]# cp /usr/local/nginx-1.22.1/conf/nginx.conf /etc/nginx/
[root@localhost ~]# vim /etc/nginx/nginx.conf
#在location /下添加 index.php
#在location~ \.php$ 将/scripts改为/usr/local/nginx/html
[root@localhost ~]# systemctl restart nginx
2.添加PHP测试文件
[root@localhost nginx]# cd /usr/local/nginx/html/
[root@localhost html]# vim index.php
<?php
phpinfo();
?>
3.重启php-fpm当出现以下界面是成功
八.LNMP上线WordPress
1.下载并解压wordpress安装包
[root@localhost ~]# tar xzvf wordpress-5.9.1-zh_CN.tar.gz
[root@localhost ~]# rm -rf /usr/local/nginx/html/*
[root@localhost ~]# cp -r wordpress/* /usr/local/nginx/html/ #将代码拷贝到网站发布目录中
[root@localhost ~]# systemctl2 restart nginx
2.创建数据库
3.打开浏览器输入ip
· 登陆成功
更多推荐
所有评论(0)