使用shell腳本一鍵部署LNMP架構的方法

LNMP架構介紹

LNMP:Linux系統下Nginx+MySQL+PHP這種網站服務器架構。Nginx是一個高性能的HTTP和反向代理服務器,也是一個IMAP/POP3/SMTP代理服務器。Mysql是一個小型關系型數據庫管理系統。PHP是一種在服務器端執行的嵌入HTML文檔的腳本語言。

原理:瀏覽器發送http request請求到服務器(Nginx),服務器響應並處理web請求,將一些靜態資源(CSS,圖片,視頻等)保存服務器上,然後將php腳本通過接口傳輸協議(網關協議)PHP-FCGI(fast-cgi)傳輸給PHP-FPM(進程管理程序),PHP-FPM不做處理,然後PHP-FPM調用PHP解析器進程,PHP解析器解析php腳本信息。PHP解析器進程可以啟動多個,進行並發執行。然後將解析後的腳本返回到PHP-FPM,PHP-FPM再通過fast-cgi的形式將腳本信息傳送給Nginx.服務器再通過Http response的形式傳送給瀏覽器。瀏覽器再進行解析與渲染然後進行呈現。

下面給大傢介紹下使用shell腳本一鍵部署LNMP架構的方法,腳本如下所示:

#/bin/bash

##將需要的安裝包傳到/opt目錄下,並關閉防火墻
systemctl stop firewalld
systemctl disable firewalld &>/dev/null
setenforce 0

###############Nginx###############

##先掛載,再安裝依賴包
df -h | grep /mnt
if [ $? -ne 0 ];then
mount /dev/cdrom /mnt &>/dev/null
fi
yum -y install pcre-devel zlib-devel gcc gcc-c++ make

##創建運行用戶、組(Nginx服務程序默認以nobody身份運行,建議為其創建專門的用戶賬號,以便更準確的控制其訪問權限)
useradd -M -s /sbin/nologin nginx

##編譯安裝
cd /opt
tar zxvf nginx-1.12.0.tar.gz
cd nginx-1.12.0/
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module

make -j 2 && make install

##優化配置文件路徑,並把nginx服務的可執行程序文件放入路徑環境變量的目錄中便於系統識別
ln -s /usr/local/nginx/sbin/* /usr/local/sbin

##添加Nginx系統服務
cat > /lib/systemd/system/nginx.service <<EOF
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -1 $MAINPID
ExecStop=/bin/kill -3 $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF

##修改文件權限
chmod 754 /lib/systemd/system/nginx.service
systemctl start nginx.service
systemctl enable nginx.service &>/dev/null

###############MySQL###############
##安裝MySQL環境依賴包
yum -y install ncurses ncurses-devel bison cmake

##創建運行用戶
useradd -M -s /sbin/nologin mysql

##編譯安裝
cd /opt
tar zxvf mysql-boost-5.7.20.tar.gz
cd /opt/mysql-5.7.20
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1

make -j 2 && make install

##修改mysql配置文件
cat > /etc/my.cnf <<EOF
[client]
port = 3306
socket=/usr/local/mysql/mysql.sock			
[mysqld]
user = mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character-set-server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
max_allowed_packet=16M
server-id = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
EOF

##更改mysql安裝目錄和配置文件的屬主屬組
chown -R mysql:mysql /usr/local/mysql/
chown mysql:mysql /etc/my.cnf

##設置路徑環境變量
echo 'export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile	
source /etc/profile

##初始化數據化
cd /usr/local/mysql/bin/
./mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data

##添加mysqld系統服務
cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
systemctl daemon-reload
systemctl start mysqld.service
systemctl enable mysqld &>/dev/null

##添加mysql的登錄密碼
function mima {
passwd=$1
/usr/bin/expect <<-EOF
spawn mysqladmin -u root -p password $passwd
expect "Enter" {send "\n"}
expect eof
EOF
}
mima "abc123"

##授權遠程登錄
function qwe {
/usr/bin/expect <<-EOF
spawn mysql -u root -p
expect "Enter" {send "abc123\n"}
expect "mysql" {send "grant all privileges on *.* to 'root'@'%' identified by 'abc123';\n"}
expect "mysql" {send "flush privileges;\n"}
expect "mysql" {send "show databases;\n"}
expect "mysql" {send "quit\n"}
expect eof
EOF
}
qwe
###############PHP###############
##安裝環境依賴包
yum -y install gd \
libjpeg libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel

##編譯安裝
cd /opt
tar jxvf php-7.1.10.tar.bz2
cd ./php-7.1.10/
./configure \
--prefix=/usr/local/php \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-fpm \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip

make -j 2 && make install

##路徑優化
ln -s /usr/local/php/bin/* /usr/local/bin/
ln -s /usr/local/php/sbin/* /usr/local/sbin/

##調整PHP配置文件
##PHP有三個配置文件
##php.ini(主配置文件)
cp /opt/php-7.1.10/php.ini-development /usr/local/php/lib/php.ini
sed -i '1170c mysqli.default_socket = /usr/local/mysql/mysql.sock' /usr/local/php/lib/php.ini
sed -i '939c date.timezone = Asia/Shanghai' /usr/local/php/lib/php.ini

php -m
##php-fpm.conf(進程服務配置文件)
cd /usr/local/php/etc
cp php-fpm.conf.default php-fpm.conf
sed -i '17c pid = run/php-fpm.pid' /usr/local/php/etc/php-fpm.conf
##www.conf(擴展配置文件)
cd /usr/local/php/etc/php-fpm.d
cp www.conf.default www.conf

##啟動php-fpm
cd /usr/local/php/sbin/
php-fpm -c /usr/local/php/lib/php.ini

##配置Nginx支持PHP解析
sed -i '65c location ~ \.php$ {' /usr/local/nginx/conf/nginx.conf
sed -i '66c root html;' /usr/local/nginx/conf/nginx.conf
sed -i '67c fastcgi_pass 127.0.0.1:9000;' /usr/local/nginx/conf/nginx.conf
sed -i '68c fastcgi_index index.php;' /usr/local/nginx/conf/nginx.conf
sed -i '69c fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;' /usr/local/nginx/conf/nginx.conf
sed -i '70c include fastcgi_params;' /usr/local/nginx/conf/nginx.conf
sed -i '71c }' /usr/local/nginx/conf/nginx.conf

##驗證PHP測試頁和驗證數據庫工作是否正常

cat > /usr/local/nginx/html/index.php <<EOF
<?php
phpinfo();
?>
EOF

到此這篇關於使用shell腳本一鍵部署LNMP架構的方法的文章就介紹到這瞭,更多相關shell腳本部署LNMP架構內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: