CentOS7 shell脚本一键安装 php mysql redis nginx 源码安装

PHP 7.3
nginx 1.16
mysql 5.7.22
redis 4.0

# 版本配置
# 注意: 修改版本可以在这直接修改。 修改路径,还是要结合代码做修改!!!
PHP="7.3.9"
NGINX="1.16.1"
MYSQL_MAIN="5.7"
MYSQL_DETIAL="5.7.22"
MYSQL="/usr/local/mysql"
MYSQL_DATA="/usr/local/mysql/mysql_data"
MYSQL_LOG="/usr/local/mysql/log/"
MYSQL_PID="/usr/local/mysql/pid/"
DB_USER="mysql"
LIB_ZIP="1.5.2"
REDIS="4.0.14"
CMAKE='3.11.4'
WWWUSER="www"

yum -y install libxslt libxslt-devel bzip2 bzip2-devel epel-release autoconf automake libaio libaio-devel ncurses-devel bison-devel git wget cmake gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel curl-devel libxml2 libxml2-devel libjpeg-devel kernel-devel libpng libpng-devel freetype freetype-devel

# 关闭以下所有进程
killall php-fpm
killall mysql
killall nginx
killall redis-server

yum remove cmake

groupadd $WWWUSER
useradd -r -g $WWWUSER -s /sbin/nologin -g $WWWUSER -M $WWWUSER

# install cmake 
cd /usr/local/src || exit 1
curl -L -o /usr/local/src/cmake-${CMAKE}.tar.gz https://cmake.org/files/v3.11/cmake-${CMAKE}.tar.gz
tar xzf cmake-${CMAKE}.tar.gz
cd cmake-${CMAKE} || exit 1
./configure  && make && make install

# 因为直接使用cmake系统回到默认的/usr/bin中去寻找,但是src中安装的cmake是在/usr/local/bin中  做一个链接即可
ln -s /usr/local/bin/cmake /usr/bin 

# php安装做需要的的libzip扩展
cd /usr/local/src || exit 1
wget https://nih.at/libzip/libzip-${LIB_ZIP}.tar.gz
tar xzf libzip-${LIB_ZIP}.tar.gz
cd libzip-${LIB_ZIP} || exit 1
mkdir -p build 
cd build && cmake .. && make && make install


# redis安装
cd /usr/local/src || exit 1
wget http://download.redis.io/releases/redis-${REDIS}.tar.gz
tar xzf redis-${REDIS}.tar.gz
cd redis-${REDIS} || exit 1
make PREFIX=/usr/local/redis install
cp redis.conf /usr/local/redis/bin
cd src
cp redis-server redis-cli redis-sentinel redis-benchmark redis-check-aof  /usr/local/redis/bin
mkdir /usr/local/redis/data
sed -i 's/dir \.\//dir \/usr\/local\/redis\/data/' /usr/local/redis/bin/redis.conf

#自定义系统服务命令( redis )
echo "Creating /usr/lib/systemd/system/redis.service"
(
cat <<'EOF'

[Unit]
Description=redis service
After=network.target

[Service]
# Type=forking
PIDFile=/usr/local/redis/redis_6379.pid
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/bin/redis.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

EOF
) | tee /usr/lib/systemd/system/redis.service

# nginx下载安装
cd /usr/local/src || exit 1
wget http://nginx.org/download/nginx-${NGINX}.tar.gz
tar xzf nginx-${NGINX}.tar.gz
cd nginx-${NGINX} || exit 1
./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx --pid-path=/usr/local/nginx/nginx.pid --with-select_module --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module
make && make install

#自定义系统服务命令( nginx )
echo "Creating /usr/lib/systemd/system/nginx.service"
(
cat <<'EOF'
[Unit]
Description=The Nginx Server
After=syslog.target network.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/nginx.pid
ExecStartPre=/usr/local/nginx/nginx -t
ExecStart=/usr/local/nginx/nginx
ExecReload=/usr/local/nginx/nginx -s reload
ExecStop=/usr/local/nginx/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF
) | tee /usr/lib/systemd/system/nginx.service


# 安装mysql 
groupadd $DB_USER
useradd -r -g $DB_USER -s /sbin/nologin -g $DB_USER -M $DB_USER
mkdir -p $MYSQL_DATA
mkdir -p $MYSQL_LOG
mkdir -p $MYSQL_PID
chown -R $DB_USER:$DB_USER $MYSQL
chown -R $DB_USER:$DB_USER $MYSQL_LOG
chown -R $DB_USER:$DB_USER $MYSQL_PID


cd /usr/local/src || exit 1
# # 下载源码包并解压,这里下载的是带boost的包,MySQL5.7.5之后需要boost库支持,所以这里直接下载带boost的源码包
wget -c https://dev.mysql.com/get/Downloads/MySQL-${MYSQL_MAIN}/mysql-boost-${MYSQL_DETIAL}.tar.gz
tar xzf mysql-boost-${MYSQL_DETIAL}.tar.gz
cd mysql-${MYSQL_DETIAL} || exit 1
rm -f CmakeCache.txt
cmake -DWITH_BOOST=boost -DMYSQL_TCP_PORT=3306 -DCMAKE_INSTALL_PREFIX=${MYSQL}  -DMYSQL_DATADIR=${MYSQL_DATA} -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci
make && make install

(
cat <<'EOF'
[client]
port=3306
default-character-set=utf8
socket=/tmp/mysql.sock 

[mysqld]
basedir=/usr/local/mysql
port=3306
datadir=/usr/local/mysql/mysql_data
socket=/tmp/mysql.sock 
log-error=/usr/local/mysql/log/mysql.log
explicit_defaults_for_timestamp=true
pid-file=/usr/local/mysql/pid/mysqld.pid

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

!includedir /etc/my.cnf.d

EOF
) | tee /etc/my.cnf

# 初始化 mysql 数据库
/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --initialize-insecure --user=${DB_USER} --basedir=${MYSQL}

# 添加环境变量
echo "export PATH=\"\$PATH:/usr/local/mysql/bin:/usr/local/bin:\$PATH\";" >> /etc/profile
source /etc/profile

echo "Creating /usr/lib/systemd/system/mysql.service"
(
cat <<'EOF'

[Unit]
Description=MySQL Server
After=syslog.target network.target

[Install]
WantedBy=multi-user.target

[Service]
User=mysql
Group=mysql

PIDFile=/usr/local/mysql/pid/mysqld.pid
TimeoutSec=0

PermissionsStartOnly=true
ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/usr/local/mysql/pid/mysqld.pid
#注意这里要加上 --daemonize 
LimitNOFILE = 5000
Restart=on-failure
RestartPreventExitStatus=1
PrivateTmp=false
EOF
) | tee /usr/lib/systemd/system/mysql.service

# 安装php
cd /usr/local/src || exit 1
wget https://www.php.net/distributions/php-${PHP}.tar.gz
tar xzf php-${PHP}.tar.gz
cd php-${PHP} || exit 1
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php  --enable-exif --with-curl --with-freetype-dir --with-gd  --with-openssl --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir --with-mysqli --with-pcre-regex --with-pdo-mysql --with-pdo-sqlite --with-pear --with-png-dir --with-jpeg-dir --with-xmlrpc --with-xsl --with-zlib --with-bz2 --with-mhash --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-gd-native-ttf --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-sysvshm --enable-xml --enable-zip
make && make install
cp php.ini-production /usr/local/php/lib/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
ln -s /usr/local/php/sbin/php-fpm /etc/init.d
cd /usr/local/php/etc/php-fpm.d
cp www.conf.default ./www.conf
sed -i 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/' /usr/local/php/lib/php.ini
sed -i 's/user = nobody/user = www/' /usr/local/php/etc/php-fpm.d/www.conf
sed -i 's/group = nobody/group = www/' /usr/local/php/etc/php-fpm.d/www.conf
cd /usr/local/src/php-${PHP}/sapi/fpm
cp php-fpm.service /usr/lib/systemd/system/


# 修改权限
chmod 755 /usr/lib/systemd/system/nginx.service
chmod 755 /usr/lib/systemd/system/mysql.service
chmod 755 /usr/lib/systemd/system/php-fpm.service
chmod 755 /usr/lib/systemd/system/redis.service

# 修改 systemctl命令文件要重启才有效
systemctl daemon-reload

# 设置开机自启动
systemctl enable nginx.service 
systemctl enable mysql.service 
systemctl enable redis.service 
systemctl enable php-fpm.service

# 启动服务
systemctl start nginx.service
systemctl start mysql.service
systemctl start redis.service
systemctl start php-fpm.service


# 以下是 修改账号和密码
# 连接MySql
# mysql -uroot -p
# 密码是空
# 修改数据库登录密码 注意密码字段是authentication_string 不是 password; mysql 5.7.14+ 版本更改密码, 原来user表的password变成了authentication_string
# update user set authentication_string=password('123456') where user='root';
# 刷新权限
# flush privileges;
# 允许远程登录
# GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root'; 
# FLUSH PRIVILEGES;
# 开放3306端口
# systemctl start firewalld
# firewall-cmd --zone=public --add-port=3306/tcp --permanent
# firewall-cmd --reload
# 最后一步
# 云服务器安全组 配置3306端口
# 以上完成成功 之后  Navicat就可以连接数据库  


CentOS7 shell脚本一键安装 php mysql redis nginx 源码安装_第1张图片

你可能感兴趣的:(linux)