#!/bin/bash
# 2020-01-01
# 源碼編譯LNMP(CentOS7.6+Nginx1.15+MySQL5.7+PHP7.3)
function install_nginx(){
# 更新epel源
yum install -y epel-release
# 安裝依賴包
yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre*
# 創建nginx運行用戶
# -M(不創建主目錄) -s(不允許登錄)
[ ! $(grep nginx /etc/passwd) ] && useradd -M -s /sbin/nologin nginx
cd /usr/local/src/
# 下載pcre源碼包
wget -c https://jaist.dl.sourceforge.net/project/pcre/pcre/8.42/pcre-8.42.tar.gz
tar -zxvf pcre-8.42.tar.gz
# 下載nginx源碼包
wget -c http://nginx.org/download/nginx-1.15.0.tar.gz
tar -zxvf nginx-1.15.0.tar.gz
cd /usr/local/src/nginx-1.15.0/
# 編譯安裝
./configure --prefix=/usr/local/nginx --with-pcre=/usr/local/src/pcre-8.42 --with-http_ssl_module --user=nginx --group=nginx
make && make install
# 啓動
/usr/local/nginx/sbin/nginx
# 輸出版本
/usr/local/nginx/sbin/nginx -v
[ $? -eq 0 ] && echo -e "\033[31mNginx安裝成功\033[0m"
sleep 10
}
function install_mysql57(){
# 更新源
yum install -y epel-release
# 安裝依賴包
yum install -y gcc gcc-c++ cmake ncurses ncurses-devel bison
# axel:多線程下載工具,下載文件時可以替代curl、wget。(人家分享的命令,試試看好不好用)
yum install -y axel
# axel -n 20 下載鏈接
cd /usr/local/src
# 好像有個bug,如果文件遇到特許情況沒有下載完成,文件名還是存在的,所以它不會繼續下載
# wget -c 應該可以解決(-c 斷點續傳)
# wget -c https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-boost-5.7.25.tar.gz
[ ! -f mysql-boost-5.7.25.tar.gz ] && axel -n 20 https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-boost-5.7.25.tar.gz
# 添加用戶
[ ! $(grep mysql /etc/passwd) ] && useradd -s /sbin/nologin mysql
# 建立所需目錄並更改所有者爲mysql
[ ! -d /data/mysql/data ] && mkdir -p /data/mysql/data
chown -R mysql:mysql /data/mysql
# 將下載好的mysql 解壓到/usr/local/mysql 目錄下
[ ! -d /usr/local/mysql/ ] && mkdir -p /usr/local/mysql/
tar -zxvf mysql-boost-5.7.25.tar.gz -C /usr/local/mysql/
# 編譯安裝
cd /usr/local/mysql/mysql-5.7.25/
# cmake安裝MySQL默認安裝在/usr/local/mysql,如果要指定目錄需要加參數:-DCMAKE_INSTALL_PREFIX=
cmake -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_BOOST=boost
make -j 2 && make install
# 配置文件
cat > /etc/my.cnf << \EOF
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
port = 3306
socket = /tmp/mysql.sock
user = mysql
basedir = /usr/local/mysql
datadir = /data/mysql/data
pid-file = /data/mysql/mysql.pid
log_error = /data/mysql/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /data/mysql/mysql-slow.log
skip-external-locking
key_buffer_size = 32M
max_allowed_packet = 1024M
table_open_cache = 128
sort_buffer_size = 768K
net_buffer_length = 8K
read_buffer_size = 768K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
thread_cache_size = 16
query_cache_size = 16M
tmp_table_size = 32M
performance_schema_max_table_instances = 1000
explicit_defaults_for_timestamp = true
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535
log_bin=mysql-bin
binlog_format=mixed
server_id = 232
expire_logs_days = 10
early-plugin-load = ""
default_storage_engine = InnoDB
innodb_file_per_table = 1
innodb_buffer_pool_size = 128M
innodb_log_file_size = 32M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 32M
sort_buffer_size = 768K
read_buffer = 2M
write_buffer = 2M
EOF
# 修改文件目錄屬主屬組
chown -R mysql:mysql /usr/local/mysql
# 初始化mysql
cd /usr/local/mysql/bin
./mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/data
# 拷貝可執行配置文件
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
# 啓動MySQL
service mysqld start
# 軟連接
ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
# 設置開機自啓動
systemctl enable mysqld
# 修改密碼
mysql -uroot -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '123';"
[ $? -eq 0 ] && echo -e "\033[31mMySQL安裝成功\033[0m" && echo -e "\033[31mMySQL的初始密碼爲:123\033[0m"
sleep 10
# 授權遠程登錄
# GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123' WITH GRANT OPTION;
# mysql -uroot -p123 -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123' WITH GRANT OPTION;"
# FLUSH PRIVILEGES;
# mysql -uroot -p123 -e "FLUSH PRIVILEGES;"
}
function install_php(){
# 安裝依賴
yum install -y gcc gcc-c++ php-mcrypt libmcrypt libmcrypt-devel autoconf freetype gd libmcrypt \
libpng libpng-devel libjpeg libxml2 libxml2-devel zlib curl curl-devel re2c net-snmp-devel \
libjpeg-devel php-ldap openldap-devel openldap-servers openldap-clients freetype-devel gmp-devel
# 下載php源碼包
cd /usr/local/src/
wget -c https://www.php.net/distributions/php-7.3.8.tar.gz
tar -zxvf php-7.3.8.tar.gz
# 編譯安裝
# 提前解決報錯
cp -frp /usr/lib64/libldap* /usr/lib/
cd /usr/local/src/
wget -c https://nih.at/libzip/libzip-1.2.0.tar.gz
tar -zxvf libzip-1.2.0.tar.gz
cd libzip-1.2.0
./configure
make && make install
# /etc/ld.so.conf 此文件記錄了編譯時使用的動態庫的路徑,也就是加載so庫的路徑。
cat >> /etc/ld.so.conf << \EOF
/usr/local/lib64
/usr/local/lib
/usr/lib
/usr/lib64
EOF
# ldconfig -v的作用是將文件/etc/ld.so.conf列出的路徑下的庫文件緩存到/etc/ld.so.cache以供使用
ldconfig -v
cd /usr/local/src/php-7.3.8
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysqli --with-pdo-mysql \
--with-mysql-sock=/usr/local/mysql/mysql.sock --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir \
--with-curl --with-gd --with-gmp --with-zlib --with-xmlrpc --with-openssl --without-pear --with-snmp --with-gettext \
--with-mhash --with-libxml-dir=/usr --with-ldap --with-ldap-sasl --with-fpm-user=nginx --with-fpm-group=nginx \
--enable-xml --enable-fpm --enable-ftp --enable-bcmath --enable-soap --enable-shmop --enable-sysvsem --enable-sockets \
--enable-inline-optimization --enable-maintainer-zts --enable-mbregex --enable-mbstring --enable-pcntl --enable-zip \
--disable-fileinfo --disable-rpath --enable-libxml --enable-opcache --enable-mysqlnd
# 提前解決報錯
cp /usr/local/lib/libzip/include/zipconf.h /usr/local/include/zipconf.h
sed -i 's/-lcrypto -lcrypt/-lcrypto -lcrypt -llber/' /usr/local/src/php-7.3.8/Makefile
make && make install
# 配置文件
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.conf
# 複製php.ini文件
cp /usr/local/src/php-7.3.8/php.ini-production /usr/local/php/etc/php.ini
# 啓動
cp /usr/local/src/php-7.3.8/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
chkconfig --add php-fpm
chkconfig php-fpm on
[ ! $(grep nginx /etc/passwd) ] && useradd -M -s /sbin/nologin nginx
service php-fpm start
[ $? -eq 0 ] && echo -e "\033[31mPHP安裝成功\033[0m"
sleep 10
# 報錯:configure: error: Cannot find ldap libraries in /usr/lib.
# 解決:cp -frp /usr/lib64/libldap* /usr/lib/
# 然後再次:./configure ...
# 報錯:configure: error: Please reinstall the libzip distributions
# 解決:yum install -y libzip-devel
# 然後再次:./configure ...
# 報錯:checking for libzip... configure: error: system libzip must be upgraded to version >= 0.11
#先刪除舊版本:yum remove -y libzip
#下載編譯安裝
# cd /usr/local/src/
# wget -c https://nih.at/libzip/libzip-1.2.0.tar.gz
# tar -zxvf libzip-1.2.0.tar.gz
# cd libzip-1.2.0
# ./configure
# make && make install
# 然後再次:./configure ...
# 報錯:configure: error: off_t undefined; check your library configuration
# 解決:
#添加搜索路徑到配置文件
# echo '/usr/local/lib64
# /usr/local/lib
# /usr/lib
# /usr/lib64'>>/etc/ld.so.conf
#然後 更新配置
# ldconfig -v
# 然後再次:./configure ...
# 報錯:/usr/local/include/zip.h:59:21: 致命錯誤:zipconf.h:沒有那個文件或目錄
# cp /usr/local/lib/libzip/include/zipconf.h /usr/local/include/zipconf.h
# 然後再次:make && make install
# 報錯:
# /usr/bin/ld: ext/ldap/.libs/ldap.o: undefined reference to symbol 'ber_strdup'
# //usr/lib64/liblber-2.4.so.2: error adding symbols: DSO missing from command line
# collect2: error: ld returned 1 exit status
# make: *** [sapi/cli/php] 錯誤 1
# 解決:在Makefile文件EXTRA_LIBS後面添加 -llber
# EXTRA_LIBS = -lcrypt -lzip -lzip -lz -lresolv -lcrypt -lrt -lldap -lgmp -lpng -lz -ljpeg
# -lz -lrt -lm -ldl -lnsl -lpthread -lxml2 -lz -lm -ldl -lssl -lcrypto -lcurl -lxml2 -lz -lm
# -ldl -lssl -lcrypto -lfreetype -lxml2 -lz -lm -ldl -lnetsnmp -lssl -lssl -lcrypto -lm -lxml2
# -lz -lm -ldl -lcrypt -lxml2 -lz -lm -ldl -lxml2 -lz -lm -ldl -lxml2 -lz -lm -ldl -lxml2 -lz
# -lm -ldl -lssl -lcrypto -lcrypt -llber
# 然後再次:make && make install
}
function modify_configuration_files(){
# 備份nginx配置文件
mv /usr/local/nginx/conf/nginx.conf{,_`date +%F`.bak}
cat > /usr/local/nginx/conf/nginx.conf << \EOF
user nginx;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server_tokens off;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
}
}
EOF
# 測試界面
cat > /usr/local/nginx/html/index.php << \EOF
<?php
phpinfo();
?>
EOF
# 檢測配置文件的正確性
/usr/local/nginx/sbin/nginx -t
# 重新加載配置文件
/usr/local/nginx/sbin/nginx -s reload
# 瀏覽器訪問:本地IP:端口/index.php
IP=$(ip addr | grep -w inet | grep -v "127.0.0.1" | awk '{print $2}' | awk -F'/' '{print $1}')
[ $? -eq 0 ] && echo -e "\033[31m瀏覽器訪問:本地IP:端口/index.php\033[0m" && echo -e "\033[31m例如:${IP}/index.php\033[0m" \
&& echo -e "\033[31m出現php信息,成功!\033[0m"
sleep 10
}
function main(){
# 安裝服務(如果有某個模塊安裝成功了,可以註釋該模塊,執行其他的模塊)
install_nginx
install_mysql57
install_php
# 可選配置文件
modify_configuration_files
}
main
源碼編譯LNMP腳本
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.