CentOS8 源码编译 Nginx、MySQL、PHP 并设置自启动以及环境变量

最后更新时间 2020-02-13 13:48:10

使用 CentOS8  源码编译 NMP。Centos8 发布了新的包管理器 dnf(地下城与勇士?),编译的 MySQL 版本是当前最新稳定版本 8.0.19,而编译的 PHP 版本是 7.4.2 ,PHP 从 7.4 版本解决了不支持 MySQL 默认密码插件(caching_sha2_password)的问题。整个过程还是非常流畅,就好像买了最新的皮肤,顿时感觉要无敌了。

新的包管理器 dnf 常用命令

dnf search <名字>
dnf info <名字>
dnf remove <名字>
# 删除无用软件包
dnf autoremove

升级所有包和安装基础软件

def upgrade
dnf install -y epel-release gcc gcc-c++ make autoconf wget screen vim bzip2 unzip git

Nginx

下载地址

https://nginx.org/en/download.html 复制最新稳定版本(Stable version)的地址。

安装

cd /usr/local/src
wget https://nginx.org/download/nginx-1.16.1.tar.gz
tar -zxvf nginx-1.16.1.tar.gz
cd nginx-1.16.1

dnf -y install pcre-devel openssl-devel gd-devel GeoIP-devel

./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_stub_status_module --with-mail --with-mail_ssl_module
make
make install
#启动
/usr/local/nginx/sbin/nginx

自启动

官方启动脚本 https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/

# 建立空白文件,把官方脚本复制粘贴进来,并把脚本内的 nginx、NGINX_CONF_FILE 变量修改实际的值
touch /etc/init.d/nginx-server.init
chmod +x /etc/init.d/nginx-server.init
chkconfig --add nginx-server.init

MySQL

下载地址

在 https://dev.mysql.com/downloads/mysql/ 中的 Select Operating System 选择 Source Code,Select OS Version 选择 Generic Linux (Architecture Independent)

给出的列表中选择包含 boot 版本的。「Compressed TAR Archive, Includes Boost Headers(mysql-boost-8.0.19.tar.gz)」

点击「Download」会跳转页面,从「No thanks, just start my download. 」可以复制出下载地址。

安装

cd /usr/local/src
tar -zxvf mysql-boost-8.0.19.tar.gz
cd mysql-8.0.19

dnf install -y bison ncurses-devel ncurses-compat-libs libaio libtirpc-devel 
yum --enablerepo=PowerTools install -y rpcgen

mkdir bld
cd bld

cmake .. -DFORCE_INSOURCE_BUILD=on -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DSYSCONFDIR=/usr/local/mysql/my.cnf -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DDEFAULT_COLLATION=utf8mb4_unicode_ci -DWITH_SSL=system -DWITH_BOOST=/usr/local/src/mysql-8.0.19/boost/boost_1_70_0/
make 
make install

配置文件 

cd /usr/local/mysql
mkdir mysql-files
chmod 750 mysql-files
touch my.cnf

编辑 my.cnf 文件

[client]
port=3306
socket=/usr/local/mysql/mysql.sock

[mysql]
no-auto-rehash

[mysqld]
user=mysql
port=3306
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/mysql.sock
pid-file=/usr/local/mysql/mysqld.pid
secure_file_priv=/usr/local/mysql/mysql-files

slow_query_log=ON
log_timestamps=SYSTEM
slow_query_log_file = /usr/local/mysql/slow.log
log-error=/usr/local/mysql/error.log

[mysql.server]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
pid-file=/usr/local/mysql/mysqld.pid

[mysqldump]
quick

设置安装目录用户和用户组

groupadd mysql
useradd -r -g mysql -s /bin/false mysql
chown -R mysql:mysql /usr/local/mysql

初始化和启动服务

bin/mysqld --defaults-file=/usr/local/mysql/my.cnf --initialize --user=mysql
bin/mysqld_safe --user=mysql &

获得 root 密码并登陆以及修改密码

# 获得密码
cat error.log
# 登陆
./bin/mysql -u root -p
# 重置 root 密码
ALTER USER 'root'@'localhost' IDENTIFIED BY 'root-password';

自启动

cp support-files/etc/init.d/mysql-server.init
chmod +x /etc/init.d/mysql-server.init
chkconfig --add mysql-server.init

PHP

下载地址

https://www.php.net/downloads 

安装

cd /usr/local/src
wget https://www.php.net/distributions/php-7.4.2.tar.gz
tar -zxvf php-7.4.2.tar.gz

dnf install -y libxml2-devel libsq3-devel libcurl-devel automake libtool readline-devel libzip-devel

git clone https://github.com/kkos/oniguruma.git oniguruma
cd oniguruma
autoreconf -vfi
./configure
make
make install
export ONIG_CFLAGS="-I/usr/local/include" ONIG_LIBS="-L/usr/local/lib -lonig"


cd /usr/local/src/php-7.4.2

./configure --prefix=/usr/local/php7 --with-config-file-path=/usr/local/php7/etc --enable-fpm --enable-ftp --enable-mbstring --enable-soap --enable-exif --enable-calendar --enable-bcmath --enable-sockets --enable-pcntl --enable-shmop --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-mysqlnd -with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-mysql-sock=/usr/local/mysql/mysql.sock --enable-gd --with-webp --with-jpeg --with-xpm --with-freetype --with-gettext --with-mhash --with-zlib --with-bz2 --with-curl --with-openssl --with-iconv-dir --with-readline --with-xmlrpc --with-zip
make
make install

cp php.ini-production /usr/local/php7/etc/php.ini
cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf
cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf

启动

/usr/local/php7/sbin/php-fpm

自启动

cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm-server.init
chmod +x /etc/init.d/php-fpm-server.init
chkconfig --add php-fpm-server.init

环境变量

编辑 /etc/profile 文件

export PATH=$PATH:/usr/local/nginx/sbin:/usr/local/mysql/bin:/usr/local/php7/bin
source /etc/profile

完成!