WordPress安装篇(5):源码编译安装LNMP并部署WordPress
阅读原文时间:2021年08月25日阅读:1

与YUM方式安装相比,源码编译安装方式更灵活,安装过程中能自定义功能和参数,特别是在批量部署服务器又要求软件版本及配置一致时,源码编译安装的优势很明显。本文介绍如何通过源码编译方式安装Nginx1.18 + MySQL8.0 + PHP7.4并部署WordPress。

源码编译安装当然也有缺点,一是需要解决软件包依赖问题,对新手来讲有一定的难度,二是编译耗时较长,需要耐心等待。爱好技术,喜欢折腾的朋友可以好好研究一下编译安装,这对提升技术能力很有帮助。

环境信息

系统、软件名称

版本

下载地址

Windows(物理机)

Windows 10

/

Linux(虚拟机)

CentOS 7.9

CentOS 7.9

VMware Workstation

VMware WorkStation 16 pro

/

Nginx

Nginx 1.18.0

Nginx 1.18.0

MySQL

MySQL 8.0.23

MySQL 8.0.23

PHP

PHP 7.4.19

PHP 7.4.19

WordPress

WordPress 5.7

WordPress5.7中文版

CMake

CMake 3.18.6

CMake 3.18.6

GCC

GCC 7.3.0

GCC 7.3.0

ISL

ISL-0.18

ISL-0.18

操作步骤

在Linux上通过源码编译方式安装LNMP并部署WordPress的安装流程如下所示:

①下载软件 → ②配置编译环境 → ③安装与配置MySQL → ④安装与配置NGINX → ⑤安装与配置PHP → ⑥安装WordPress前的准备 → ⑦安装WordPress。

步骤一:下载软件

1、 在Linux服务器上下载相关软件,为编译安装做准备,也可以在本地电脑下载软件然后上传到Linux服务器上,本文选择在Linux服务器直接下载软件。

新建software目录,下载的软件全部存放到software目录

mkdir /opt/software #新建目录
cd /opt/software    

#下载MySQL 8.0.23
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-boost-8.0.23.tar.gz --no-check-certificate 

#下载Nginx 1.18.0
wget http://nginx.org/download/nginx-1.18.0.tar.gz 

#下载PHP 7.4.19
wget https://www.php.net/distributions/php-7.4.19.tar.gz 

#下载WordPress 5.7
wget https://cn.wordpress.org/wordpress-5.7-zh_CN.tar.gz 

#下载CMake 3.18.6
wget https://cmake.org/files/v3.18/cmake-3.18.6.tar.gz --no-check-certificate 

#下载GCC 7.3.0
wget https://mirrors.aliyun.com/gnu/gcc/gcc-7.3.0/gcc-7.3.0.tar.gz 

#下载ISL-0.18wget ftp://gcc.gnu.org/pub/gcc/infrastructure/isl-0.18.tar.bz2

步骤二:配置编译环境

1、安装依赖包

编译安装需要安装相关开发工具软件,例如CMake、gcc、gcc-c++等。

yum -y install epel-release      #安装扩展软件仓库
yum -y install gcc gcc-c++ cmake bison ncurses ncurses-devel libaio-devel openssl openssl-devel gmp gmp-devel mpfr mpfr-devel libmpc libmpc-devel libxml2-devel sqlite-devel libcurl-devel oniguruma-devel libpng-devel libjpeg-devel freetype-devel libzip-devel bzip2

2、升级CMake

通过YUM安装的CMake版本为2.8.12,不能满足MySQL8.0数据库的编译要求,需要升级CMake版本至3.4.3或以上,本文以升级到3.18.6版本为例。

2.1 解压CMake并进入CMake目录。

cd /opt/softwaretar -zxvf cmake-3.18.6.tar.gz    cd cmake-3.18.6

2.2 升级CMake

./bootstrap

2.3 编译CMake

make -j 4

说明:

-j 4 参数充分利用多核CPU优势,加快编译速度,参数-j后数字为CPU核数,可用 cat /proc/cpuinfo | grep processor | wc -l 进行查看,此数值应小于等于CPU核数。

2.4 安装CMake

make -j 4 install

2.5 确认CMake的版本是否为3.18.6。

[root@Linux cmake-3.18.6]# /usr/local/bin/cmake --version
cmake version 3.18.6
CMake suite maintained and supported by Kitware (kitware.com/cmake).

3、升级GCC

说明:通过YUM安装的GCC版本较老,需要升级GCC版本至5.3或以上,本文以升级到7.3.0版本为例。

3.1 升级ISL

升级GCC之前需要将ISL升级到0.15或以上,否则升级GCC会报错,无法升级到7.3.0,本文以升级ISL到0.18为例。

解压ISL并进入ISL目录

cd /opt/softwaretar -jxvf isl-0.18.tar.bz2cd isl-0.18mkdir buildcd build

配置

../configure --prefix=/usr/local/isl-0.18

编译

make -j 4

安装

make -j 4 install

3.2 升级GCC

解压GCC文件并进入GCC目录

cd /opt/softwaretar -zxvf gcc-7.3.0.tar.gzcd gcc-7.3.0

配置

./configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-bootstrap --with-isl=/usr/local/isl-0.18 --disable-multilib

参数说明:

--perfix=PATH:指定GCC软件安装目录。默认路径/usr

--mandir=PATH:指定GCC软件文档目录。默认路径/usr/share/man

--infodir=PATH:指定GCC软件日志信息目录。默认路径/usr/share/info

--enable-bootstrap:指定启用bootstrap方式安装。

编译GCC(编译时间较长,请耐心等待编译完成。)

make -j 4

安装GCC

make -j 4 install

确认GCC的版本是否为7.3.0。

[root@Linux gcc-7.3.0]# gcc -v
Using built-in specs.
COLLECT_GCC=gccCOLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-pc-linux-gnu/7.3.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ./configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-bootstrap--with-isl=/usr/local/isl-0.18 --disable-multilib
Thread model: posix
gcc version 7.3.0 (GCC)

步骤三:安装与配置MySQL

1、安装MySQL

1.1、清理环境

如果您的系统中已经安装了MySQL或者MariaDB,请卸载MySQL和MariaDB数据库后再安装MySQL8.0。命令如下:

rpm -qa | grep mysql*          # 查询是否安装了
mysqlrpm -e --nodeps mysql*    # 卸载mysql
rpm -qa | grep mariadb*        # 查询是否安装了MariaDB
rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64    # 卸载MariaDB

1.2、创建用户组和用户

创建用户组mysql

groupadd mysql

创建用户mysql,添加到mysql用户组,不允许登录操作系统

useradd -r -g mysql -s /bin/false mysql

1.3、目录规划并创建目录

MySQL的目录规划:MySQL数据库的根目录设置为:/usr/local/mysql,data子目录存放数据文件,conf子目录存放配置文件,log子目录存放日志。

创建目录

mkdir -p /usr/local/mysql/datamkdir -p /usr/local/mysql/confmkdir -p /usr/local/mysql/log

1.4、解压MySQL并进入源码文件夹

cd /opt/softwaretar -zxvf mysql-boost-8.0.23.tar.gzcd mysql-8.0.23

1.5、配置MySQL选项。

新建编译目录build并进入目录

mkdir /opt/software/mysql-8.0.23/buildcd build

配置MySQL选项

cmake .. \
-DBUILD_CONFIG=mysql_release \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DSYSCONFDIR=/usr/local/mysql/conf \
-DTMPDIR=/tmp \
-DWITH_BOOST=/opt/software/mysql-8.0.23/boost/boost_1_73_0

MySQL源配置选项说明:

选项

描述

-DBUILD_CONFIG=mysql_release

使用与Oracle相同的构建选项来配置源发行版

-DCMAKE_INSTALL_PREFIX

MySQL安装根目录

-DMYSQL_DATADIR

MySQL数据文件目录

-DSYSCONFDIR

MySQL的配置文件目录

-DTMPDIR

MySQL临时目录

-DWITH_BOOST

Boost库目录位置,Boost库是构建MySQL的必选项

更多MySQL源配置选项请见官方的《MySQL 8.0参考手册》,MySQL源配置选项。

1.6、编译MySQL

make -j 4

编译完成后测试

make test

1.7、安装MySQL

make -j 4 install

查看数据库版本

[root@Linux build]# /usr/local/mysql/bin/mysql --version
/usr/local/mysql/bin/mysql Ver 8.0.23 for Linux on x86_64 (Source distribution)

1.8、新建my.cnf配置文件

vim /usr/local/mysql/conf/my.cnf

配置文件内容如下:

[mysqld_safe]
log-error=/usr/local/mysql/log/error.log

[mysqldump]
quick

[mysql]
no-auto-rehashsocket=/tmp/mysql.sock

[client]
default-character-set=utf8mb4

[mysqld]
basedir=/usr/local/mysql
socket=/tmp/mysql.sock
tmpdir=/tmpdata
dir=/usr/local/mysql/data
default_authentication_plugin=mysql_native_password
character-set-server=utf8mb4
port=3306
user=mysql

1.9、MySQL目录授权

chown -R mysql:mysql /usr/local/mysql/
chmod -R 750 /usr/local/mysql/

1.10、数据库初始化

/usr/local/mysql/bin/mysqld \
--initialize --user=mysql \
--basedir=/usr/local/mysql/ \
--datadir=/usr/local/mysql/data/

输出信息

[root@Linux conf]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
2021-05-26T22:44:54.654100Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.23) initializing of server in progress as process 46206
2021-05-26T22:44:54.664214Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2021-05-26T22:44:55.250186Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2021-05-26T22:44:56.717990Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: eagg5tQuN/B1

记住root用户的初始密码 eagg5tQuN/B1

初始化不成功如何重新初始化?

删除 data目录下的所有目录和文件再执行初始化操作

rm -rf /usr/local/mysql/data/*
/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/

1.11、配置MySQL加入systemctl管理服务

创建mysqld.service文件

vim /usr/lib/systemd/system/mysqld.service

mysqld.service文件的内容如下:

[Unit]
Description=MySQL
ServerDocumentation=man:mysqld(8)
Documentation=http://dev/mysql.com/doc/refman/en/using-systemd.html
After=network.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults- file=/usr/local/mysql/conf/my.cnf
LimitNOFILE=65536
LimitNPROC=65536
[Install]
WantedBy=multi-user.target

通过systemctl管理MySQL

systemctl daemon-reload  #重新加载系统服务
systemctl start mysqld   #启动MySQL
systemctl status mysqld  #查看MySQL状态
systemctl enable mysqld  #MySQL加入开机自启动

1.12、更改root密码

连接MySQL Server

mysql -uroot -p     #输入root的初始密码

更改root用户的密码

mysql> ALTER USER root@'localhost' IDENTIFIED BY 'Your@pass2021';Query OK, 0 rows affected (0.01 sec)

2、配置MySQL

2.1、创建wordpress数据库供wordpress程序使用。

mysql> create database wordpress;Query OK, 1 row affected (0.01 sec)

2.2、创建数据库用户wordpress并授权,wordpress程序使用wordpress用户连接数据库。

创建wordpress用户

CREATE USER wordpress@'localhost' IDENTIFIED BY 'Your@pass2021';

授权wordpress @'localhost'用户对数据库wordpress具有完全访问权限

GRANT ALL privileges ON wordpress.* TO wordpress @'localhost';

设置wordpress用户的密码永不过期

ALTER USER 'wordpress'@'localhost' PASSWORD EXPIRE NEVER;

刷新权限并退出。

flush privileges;
exit;

步骤四:安装与配置NGINX

 

1、解压并进入目录

cd /opt/software
tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0

2、创建用户组和用户

创建用户组nginx

groupadd nginx

创建用户nginx,添加到mysql用户组,不允许登录操作系统

useradd -r -g nginx -s /bin/false nginx

3、 生成编译需要的配置文件

执行以下命令

./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-threads \
--with-file-aio \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_gzip_static_module

4、编译Nginx

make -j 4

5、 安装Ningx

make -j 4 install

6、Nginx加入Systemctl管理服务

创建nginx.service文件

vim /usr/lib/systemd/system/nginx.service

nginx.service文件的内容如下:

[Unit]
Description=nginx web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target

7、Nginx目录授权

chown -R nginx:nginx /usr/local/nginx
chmod -R 750 /usr/local/nginx

8、配置nginx.conf

vim /usr/local/nginx/conf/nginx.conf

第1行user nobody修改为user nginx

第9行 #pid logs/nginx.pid 删除#号注释符

:wq保存退出

9、启动Nginx并将Nginx服务加入开机自启动

systemctl start nginx   #启动nginx
systemctl status nginx  #查看nginx运行状态
systemctl enable nginx  #nginx加入开机自启动

10、(可选)设置环境变量

配置好环境变量就能使用nginx命令,例如:nginx -t nginx -v

vim /etc/profile

在export PATH= 这行最后面添加 :/usr/local/nginx/sbin

添加后的配置如下:

export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin:/usr/local/nginx/sbin

使环境变量生效

source /etc/profile

11、验证Nginx是否安装成功

看到如下输出,代表Nginx安装成功。

[root@Linux software]# curl 127.0.0.1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

步骤五:安装与配置PHP

 

1、解压PHP并进入目录

cd /opt/softwaretar -zxvf php-7.4.19.tar.gzcd php-7.4.19

2、 生成配置文件(参数可根据实际情况增减)

./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--with-curl \
--enable-gd \
--with-gettext \
--with-iconv-dir \
--with-kerberos \
--with-libdir=lib64 \
--with-mysqli \
--with-openssl \
--with-pdo-mysql \
--with-jpeg \
--with-xmlrpc \
--with-zlib \
--with-zip \
--enable-fpm \
--enable-inline-optimization \
--enable-mbregex \
--enable-mbstring \
--enable-pcntl \
--enable-soap \
--enable-sockets \
--enable-sysvsem \
--enable-sysvshm \
--enable-xml

配置时报错如下,提示libzip的版本未达到要求,要求libzip版本0.11或以上。卸载libzip并编译安装libzip,然后在重新生成PHP配置文件前,使用EXPORT命令指定libzip库文件的位置。

checking for libzip >= 0.11 libzip != 1.3.1 libzip != 1.7.0... no
configure: error: Package requirements (libzip >= 0.11 libzip != 1.3.1 libzip != 1.7.0) were not met:

No package 'libzip' found
No package 'libzip' found
No package 'libzip' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables LIBZIP_CFLAGS
and LIBZIP_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

卸载低版本的libzip

yum -y remove libzip

下载高版本libzip并安装,以安装libzip1.2.0为例。

cd /opt/software
wget https://nih.at/libzip/libzip-1.2.0.tar.gz
tar -zxvf libzip-1.2.0.tar.gz
cd libzip-1.2.0

配置libzip

./configure

编译并安装libzip

make && make install

指定libzip库文件的位置

export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig/"

4、重新生成PHP配置文件

cd /opt/software/php-7.4.19
./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--with-curl \
--enable-gd \
--with-gettext \
--with-iconv-dir \
--with-kerberos \
--with-libdir=lib64 \
--with-mysqli \
--with-openssl \
--with-pdo-mysql \
--with-jpeg \
--with-xmlrpc \
--with-zlib \
--with-zip \
--enable-fpm \
--enable-inline-optimization \
--enable-mbregex \
--enable-mbstring \
--enable-pcntl \
--enable-soap \
--enable-sockets \
--enable-sysvsem \
--enable-sysvshm \
--enable-xml

看到如下输出,说明生成php的配置文件成功。

Thank you for using PHP.

 

3、编译PHP

make -j 4

4、安装PHP

make -j 4 install

5、准备PHP的配置文件

cp /opt/software/php-7.4.19/php.ini-production /usr/local/php/etc/php.ini
cp /opt/software/php-7.4.19/sapi/fpm/init.d.php-fpm /usr/local/php/
cd /usr/local/php/etc
cp php-fpm.conf.default php-fpm.conf
cd php-fpm.d
cp www.conf.default www.conf

6、php-fpm加入systemctl管理

新建php-fpm.service文件

vim /usr/lib/systemd/system/php-fpm.service

php-fpm.service内容如下:

[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target
[Service]
Type=simple
PIDFile=/usr/local/php/var/run/php-fpm.pid
ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/bin/kill -SIGINT $MAINPID
[Install]
WantedBy=multi-user.target

7、启动php-fpm、查看php状态

systemctl daemon-reload   #重新加载系统服务
systemctl start php-fpm   #启动php-fpm
systemctl status php-fpm  #查看php-fpm状态
systemctl enable php-fpm  #设置php-fpm开机启动

步骤六:安装WordPress前的准备

1、解压WordPress

cd /opt/softwaretar -zxvf wordpress-5.7-zh_CN.tar.gz

2、 创建wordpress工作目录,将wordpress程序复制到此目录

mkdir -p /data/wordpress

cp -pR /opt/software/wordpress/* /data/wordpress/

3、 目录授权

chown -R nginx:nginx /data/wordpress
find /data/wordpress -type d -exec chmod 755 {} \; #目录设置755权限
find /data/wordpress -type f -exec chmod 644 {} \; #文件设置644权限

4、 配置Nginx

vim /usr/local/nginx/conf/nginx.conf

大约在第44行,根目录设置为 /data/wordpress

第45行添加index.php

修改后的location上下文配置如下:

location / {
    root /data/wordpress;
    index index.php index.html index.htm;
}

删除第65至71行的#号注释符,root目录修改为 /data/wordpress,/scripts$fastcgi_script_name 修改为:$document_root$fastcgi_script_name

第65至71行初始配置如下:

#location ~ \.php$ {
    # root html;
    # fastcgi_pass 127.0.0.1:9000;
    # fastcgi_index index.php;
    # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    # include fastcgi_params;
#}

修改后的配置如下:

location ~ \.php$ {
   root /data/wordpress;
   fastcgi_pass 127.0.0.1:9000;
   fastcgi_index index.php;
   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
   include fastcgi_params;
}

检查配置文件的配置是否正确,看到如下输出,代表配置正确。

[root@Linux conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重新加载Nginx配置文件

systemctl reload nginx

查看Nginx的运行状态

systemctl status nginx

步骤七:安装WordPress

现在开始安装WordPress,在物理机的浏览器地址栏输入Linux服务器的IP地址,例如:http://192.168.1.100,点【现在就开始!】开始安装WordPress。如果无法打开安装界面,可能是Linux防火墙没有放行80端口,关闭Linux防火墙或者放行80端口。

如下图,填写前面已经创建好的数据库名,用户名以及密码,数据库主机使用默认的localhost,表前缀使用默认wp_ ,点击【提交】。

接下来点击【现在安装】按钮继续安装

根据下图填写网站的基本信息,填好后点击【安装WordPress】进入下一步。

站点标题:站点的名称,给你站点起一个响亮或者令人印象深刻的名字

用户名:这个用户是WordPress的后台管理员账户,具有后台最高管理权限,用户名确认后不可更改。建议用户名不要使用admin、administrator、root等常用的管理员账号名称,因为黑K很喜欢破解这类用户的密码。

密码:默认会生成一个复杂的随机密码,请保管好,初始密码在后台可以更改。建议在这里输入你自己准备好的复杂密码(设置密码16位或以上,由大小写字母,数字与特殊符号组成),密码如果太简单很容易被破解。

电子邮箱:输入电子邮箱地址

对搜索引擎的可见性:建议不要勾选,如果你的网站不想被搜索引擎收录的话可以勾选,就算勾选了也不能保证搜索引擎就一定不收录。

提示WordPress安装成功,点击【登录】 按钮准备登录WordPress。

输入用户名和密码,点击【登录】按钮进入WordPress后台管理系统。

已成功登录WordPress后台管理系统,如下图:

WordPress已经完成初始化安装,目前使用的是默认主题而且网站只有一篇“世界,您好!”的文章,所以看起来很简单,安装一个精美的主题并完善网站的内容后,你期待的网站才会出现。

总结:

通过编译的方式安装Nginx、MySQL、PHP能灵活地设置参数和选择需要的功能,但需要有一定的Linux基础知识,编译非常耗时需要耐心等待。主要是安装好相关依赖包,编译安装过程才会比较顺利。


安装WordPress的更多方法如下:

WordPress安装篇(1):使用PHPStudy安装WordPress

WordPress安装篇(2):用宝塔面板在Windows上安装WordPress

WordPress安装篇(3):用宝塔面板在Linux上安装WordPress

WordPress安装篇(4):YUM方式安装LNMP并部署WordPress

WordPress安装篇(5):源码编译安装LNMP并部署WordPress


本文转自 云引未来-www.useclouds.cn