版本:
centos 7 x64
nginx: 1.18.0
php: 7.4.6
mariadb:10.4.13
内容
#!/bin/bash
#auto Source Code install LNMP
#nginx-1.18.0 mariadb10.4.6 php7.4.6
#by author toyix
#2020年7月2日 14:08:02
echo "#########################################################################################"
echo "##-------------------------源码安装nginx-1.18.0------------------------------------------"
echo "#########################################################################################"
repobase="/etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo"
repoepel="/etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo"
SOFT1="vim wget lrzsz"
INIT_DIR="/etc/init.d"
nginxsoft2="pcre-devel pcre gcc gcc-c++ openssl openssl-devel zlib-devel"
nginxversion="nginx-1.18.0"
NGINX_BASE_DIR="/usr/local/nginx"
nginxsoft="http://nginx.org/download/${nginxversion}.tar.gz"
nginx_configuremoudule="--user=nginx \
--group=nginx \
--prefix=${NGINX_BASE_DIR} \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_mp4_module \
--with-http_realip_module \
--with-pcre \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-stream"
netstat_nginx="netstat -tnlp |grep nginx"
selinuxconfigfile="/etc/selinux/config"
selinux_enforcing="SELINUX=enforcing"
selinux_disabled="SELINUX=disabled"
firewalld_open80="--zone=public --add-port=80/tcp --permanent"
firewalld_open3306="--zone=public --add-port=3306/tcp --permanent"
firewalld_open9000="--zone=public --add-port=9000/tcp --permanent"
firewalld_ports="--zone=public --list-ports"
yum install $SOFT1 -y
echo "安装国内base及epel源"
seleep 5
curl -o $repobase
wget -O $repoepel
yum clean all
yum makecache
echo "创建nginx用户nginx"
useradd -s /sbin/nologin -r nginx
echo "安装环境支持"
yum install $nginxsoft2 -y
echo "下载nginx"
cd /usr/src/
wget -c $nginxsoft
tar -xf ${nginxversion}.tar.gz
cd ${nginxversion}
echo "预编译"
./configure ${nginx_configuremoudule}
echo "编译"
make
echo "安装"
make install
${NGINX_BASE_DIR}/sbin/nginx
sed -i "s/${selinux_enforcing}/${selinux_disabled}/g" ${selinuxconfigfile}
setenforce 0
firewall-cmd ${firewalld_open80}
firewall-cmd ${firewalld_open3306}
firewall-cmd ${firewalld_open9000}
firewall-cmd --reload
firewall-cmd $firewalld_ports
echo "nginx-1.18.0安装完成"
echo "#########################################################################################"
echo "##----------------------------源码安装mariadb-10.4.16------------------------------------"
echo "#########################################################################################"
echo "安装软件环境支持"
DB_SOFT="wget lrzsz vim boost gcc ncurses-devel libaio bison gcc-c++ git cmake ncurses-devel openssl openssl-devel"
SRC_DIR="/usr/src/"
DB_BASE_DIR="/usr/local/mariadb/"
DB_DATA_DIR="/data/mariadb/"
PRO_mariadb_sh="/etc/profile.d/mariadb.sh"
DB_SOCK_DIR="/tmp/mysql.sock"
DB_SOFT_NAME="mariadb-10.4.13.tar.gz"
DB_VERSION="mariadb-10.4.13"
DB_MYSQLD="${INIT_DIR}/mysqld"
DB_DOWN_DIR="https://mirrors.tuna.tsinghua.edu.cn/mariadb//mariadb-10.4.13/source/${DB_SOFT_NAME}"
DB_CMAKE_MODULE="-DCMAKE_INSTALL_PREFIX=/usr/local/mariadb \
-DMYSQL_DATADIR=${DB_DATA_DIR} \
-DSYSCONFDIR=${DB_BASE_DIR} \
-DMYSQL_USER=mysql \
-DMYSQL_TCP_PORT=3306 \
-DWITHOUT_TOKUDB=1 \
-DMYSQL_UNIX_ADDR=${DB_SOCK_DIR} \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci"
yum install -y ${DB_SOFT}
echo "下载mariadb10.4.16源码包"
cd ${SRC_DIR}
wget -c ${DB_DOWN_DIR}
echo "解压"
tar -zxvf ${DB_SOFT_NAME}
cd ${DB_VERSION}
echo "创建mysql用户"
useradd -s /sbin/nologin -r mysql -M
echo "预编译"
cmake -j4 . \
${DB_CMAKE_MODULE}
echo "编译"
make -j4 && make install -j4
echo "查看安装目录"
ls ${DB_BASE_DIR}
echo "配置启动文件及权限等"
cd ${DB_BASE_DIR}
cp support-files/mysql.server ${DB_MYSQLD}
chmod +x ${DB_MYSQLD}
mkdir -p ${DB_DATA_DIR}
chown -R mysql:mysql ${DB_DATA_DIR}
chown -R mysql:mysql ${DB_BASE_DIR}
echo "配置数据库主配置文件"
cat >${DB_BASE_DIR}my.cnf<<EOF
[mysqld]
basedir=${DB_BASE_DIR}
datadir=${DB_DATA_DIR}
port=3306
pid-file=${DB_DATA_DIR}mysql.pid
socket=${DB_SOCK_DIR}
[mysqld_safe]
log-error=${DB_DATA_DIR}mysql.log
[client]
port=3306
socket=${DB_SOCK_DIR}
default-character-set=utf8
EOF
echo -e "\033[32m数据库初始化\033[0m"
${DB_BASE_DIR}scripts/mariadb-install-db --datadir=${DB_DATA_DIR}
echo "安装目录改回root权限"
chown -R root:root ${DB_BASE_DIR}
echo "数据目录权限改为mysql"
chown -R mysql:mysql ${DB_DATA_DIR}
ls -l ${DB_DATA_DIR}
echo "配置环境变量"
echo "export PATH=${DB_BASE_DIR}bin:$PATH" >${PRO_mariadb_sh}
. ${PRO_mariadb_sh}
添加开机启动
chkconfig --add mysqld
chkconfig --level 35 mysqld on
echo "启动mysql"
${DB_MYSQLD} restart
echo "查看进程"
ps -ef|grep mysql
netstat -tnlp
echo "#########################################################################################"
echo "##----------------------------源码安装php7.4.6-------------------------------------------"
echo "#########################################################################################"
PHP_SOFT="libjpeg libpng-devel freetype freetype-devel libxslt libxslt-devel libXpm-devel sqlite-devel bzip2 bzip2-devel libjpeg libjpeg-devel libpng libpng-devel oniguruma oniguruma-devel libcurl libcurl-devel postgresql-devel openssl openssl-devel libzip libxml2 libxml2-devel pcre2"
PHP_VERSION="php-7.4.6"
PHP_BASE_DIR="/usr/local/php7"
PHP_SOFT_NAME="${PHP_VERSION}.tar.gz"
PHP_DOWNLOAD_URL="https://www.php.net/distributions/${PHP_SOFT_NAME}"
PHP_MOUDLE=" --enable-fpm \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--enable-debug \
--enable-gd \
--with-jpeg \
--with-pgsql \
--enable-inline-optimization \
--with-bz2 \
--with-zlib-dir \
--with-freetype \
--enable-mbstring \
--enable-soap \
--with-openssl \
--enable-calendar \
--with-curl \
--enable-ftp \
--with-gettext \
--with-xmlrpc \
--with-xsl \
--enable-opcache \
--with-iconv \
--with-pdo-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-kerberos \
--with-pdo-sqlite \
--with-pear \
--enable-shmop \
--enable-xml \
--with-zlib \
--enable-sockets \
--enable-sysvsem \
--enable-pcntl \
--enable-mbregex \
--enable-exif \
--enable-bcmath \
--with-mhash \
--with-zlib-dir \
--with-pcre-jit \
--with-config-file-path=${PHP_BASE_DIR}/etc "
echo "安装软件环境支持"
yum install -y ${PHP_SOFT}
echo "软件下载"
cd /usr/src
wget -c ${PHP_DOWNLOAD_URL}
echo "创建nginx用户"
useradd -s /sbin/nologin -r nginx -M
echo "解压、编译、安装"
tar -zxvf ${PHP_SOFT_NAME}
cd ${PHP_VERSION}
./configure --prefix=${PHP_BASE_DIR} ${PHP_MOUDLE}
make -j4 && make install -j4
echo "主配置文件"
cp php.ini-development ${PHP_BASE_DIR}/etc/php.ini
echo "次配置文件"
cp ${PHP_BASE_DIR}/etc/php-fpm.conf.default ${PHP_BASE_DIR}/etc/php-fpm.conf
echo "修改Php-fpm配置文件中的include默认文件名"
cp ${PHP_BASE_DIR}/etc/php-fpm.d/www.conf.default ${PHP_BASE_DIR}/etc/php-fpm.d/default.conf
echo "配置启动文件php-fpm及软链接"
cp /usr/src/${PHP_VERSION}/sapi/fpm/init.d.php-fpm ${INIT_DIR}/php-fpm
chmod +x ${INIT_DIR}/php-fpm
ln -s ${INIT_DIR}/php-fpm /usr/sbin/php-fpm
echo "整合php与nginx"
sed -i "65,71 s/#//g" ${NGINX_BASE_DIR}/conf/nginx.conf
sed -i "69 s#\/scripts#\$document_root#g" ${NGINX_BASE_DIR}/conf/nginx.conf
${NGINX_BASE_DIR}/sbin/nginx -s reload
cat >${NGINX_BASE_DIR}/html/test.php<<EOF
<?php
phpinfo();
?>
EOF
echo "启动php-fpm"
${PHP_BASE_DIR}/sbin/php-fpm
ps -ef|grep mariadb
ps -ef|grep php-fpm
ps -ef|grep nginx
netstat -tnlp
echo "---------------end---------------------------"
运行结果
Installing phpdbg binary: /usr/local/php7/bin/
Installing PHP CGI binary: /usr/local/php7/bin/
Installing PHP FPM defconfig: /usr/local/php7/etc/
Installing PHP CGI man page: /usr/local/php7/php/man/man1/
Installing phpdbg man page: /usr/local/php7/php/man/man1/
Installing PHP FPM man page: /usr/local/php7/php/man/man8/
/usr/src/php-7.4.6/build/shtool install -c ext/phar/phar.phar /usr/local/php7/bin
ln -s -f phar.phar /usr/local/php7/bin/phar
Installing PHP FPM status page: /usr/local/php7/php/php/fpm/
Installing header files: /usr/local/php7/include/php/
主配置文件
次配置文件
修改Php-fpm配置文件中的include默认文件名
配置启动文件php-fpm及软链接
启动php-fpm
root 24621 2140 0 14:04 pts/0 00:00:00 grep mariadb
root 27486 1 0 13:58 ? 00:00:00 /bin/sh /usr/local/mariadb//bin/mysqld_safe --datadir=/data/mariadb/ --pid-file=/data/mariadb/mysql.pid
mysql 27595 27486 0 13:58 ? 00:00:00 /usr/local/mariadb/bin/mysqld --basedir=/usr/local/mariadb/ --datadir=/data/mariadb/ --plugin-dir=/usr/local/mariadb//lib/plugin --user=mysql --log-error=/data/mariadb/mysql.log --pid-file=/data/mariadb/mysql.pid --socket=/tmp/mysql.sock --port=3306
root 24617 1 0 14:04 ? 00:00:00 php-fpm: master process (/usr/local/php7/etc/php-fpm.conf)
nginx 24618 24617 0 14:04 ? 00:00:00 php-fpm: pool www
nginx 24619 24617 0 14:04 ? 00:00:00 php-fpm: pool www
root 24623 2140 0 14:04 pts/0 00:00:00 grep php-fpm
root 5115 1 0 13:18 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 5116 5115 0 13:18 ? 00:00:00 nginx: worker process
nginx 24618 24617 0 14:04 ? 00:00:00 php-fpm: pool www
nginx 24619 24617 0 14:04 ? 00:00:00 php-fpm: pool www
root 24625 2140 0 14:04 pts/0 00:00:00 grep nginx
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1779/master
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 24617/php-fpm: mast
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5115/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1549/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1779/master
tcp6 0 0 :::3306 :::* LISTEN 27595/mysqld
tcp6 0 0 :::22 :::* LISTEN 1549/sshd
---------------end---------------------------
测试
[root@localhost src]# /usr/local/mariadb/bin/mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.4.13-MariaDB Source distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
注:
为避免下载失败,可以先将源码包下载至/usr/src下