1. 程式人生 > 實用技巧 >部署LNMP架構

部署LNMP架構

一、LNMP架構簡介

LNMP平臺就是Linux、Ngnix、MySQL、PHP的組合架構,需要Linux伺服器、MySQL資料庫、PHP解析環境
Nginx是一個高效能的HTTP和反向代理伺服器,也是一個IMAP/POP3/SMTP代理伺服器。Mysql是一個小型關係型資料庫管理系統。PHP是一種在伺服器端執行的嵌入HTML文件的指令碼語言。

二、Nginx編譯安裝

 1 [root@localhost ~]# yum -y install gcc gcc-c++ make pcre-devel zlib-devel
 2 
 3 [root@localhost ~]# tar zxf nginx-1.12
.2.tar.gz 4 [root@localhost ~]# cd nginx-1.12.2/ 5 [root@localhost nginx-1.12.2]# ./configure \ 6 > --prefix=/usr/local/nginx \ 7 > --user=nginx \ 8 > --group=nginx \ 9 > --with-http_stub_status_module ###統計狀態模組 10 11 [root@localhost nginx-1.12.2]# make && make install 12 13 [root@localhost ~]# useradd -M -s /sbin/nologin nginx ###建立一個不可登入的程式使用者
14 15 [root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/bin ###優化執行路徑 16 [root@localhost ~]# nginx ###啟動服務 17 [root@localhost ~]# netstat -anpt | grep nginx ###檢視nginx服務是否開啟 18 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 16429/nginx: master 19 20 [root@localhost ~]# killall
-s QUIT nginx ###選項-s QUIT等於-3 停止服務 21 [root@localhost ~]# netstat -anpt | grep nginx 22 23 [root@localhost ~]# killall -s HUP nginx ###選項-s HUP等於-1 重新載入 24 [root@localhost ~]# netstat -anpt | grep nginx 25 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 16488/nginx: master 26 27 [root@localhost ~]# vi /etc/init.d/nginx ###製作管理指令碼 28 #!/bin/bash 29 #chkconfig: 35 20 80 30 #description: nginx server 31 PROG="/usr/local/nginx/sbin/nginx" 32 PIDF="/usr/local/nginx/logs/nginx.pid" 33 34 case "$1" in 35 start) 36 $PROG 37 ;; 38 stop) 39 killall -s QUIT $(cat $PIDF) 40 ;; 41 restart) 42 $0 stop 43 $0 start 44 ;; 45 reload) 46 killall -s HUP $(cat $PIDF) 47 ;; 48 *) 49 echo "Usage: $0 {start|stop|reload|status}" 50 exit 1 51 esac 52 exit 0 53 54 [root@localhost ~]# chmod +x /etc/init.d/nginx 55 [root@localhost ~]# chkconfig --add nginx

三、編譯安裝Mysql資料庫

3.1、安裝環境

1 [root@localhost ~]# yum -y install \
2 > ncurses \
3 > ncurses-devel \
4 > bison \
5 > cmake

3.2、建立不可登入的使用者

1 [root@localhost ~]# useradd -s /sbin/nologin mysql

3.3、上傳安裝包到/opt目錄下解壓縮並進行cmake配置

 1 [root@localhost ~]# cd /opt
 2 [root@localhost opt]# tar zxf mysql-boost-5.7.20.tar.gz
 3 
 4 [root@localhost mysql-5.7.20]# cmake \
 5 > -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
 6 > -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
 7 > -DSYSCONFDIR=/etc \
 8 > -DSYSTEMD_PID_DIR=/usr/local/mysql \
 9 > -DDEFAULT_CHARSET=utf8  \
10 > -DDEFAULT_COLLATION=utf8_general_ci \
11 > -DWITH_INNOBASE_STORAGE_ENGINE=1 \
12 > -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
13 > -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
14 > -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
15 > -DMYSQL_DATADIR=/usr/local/mysql/data \
16 > -DWITH_BOOST=boost \
17 > -DWITH_SYSTEMD=1

3.4、編譯安裝

1 [root@localhost mysql-5.7.20]# make && make install

3.5、資料庫目錄進行許可權調整

1 [root@localhost ~]# chown -R mysql:mysql /usr/local/mysql/

3.6、建立調整配置檔案

 1 [root@localhost ~]# vi /etc/my.cnf
 2 [client]
 3 port = 3306
 4 default-character-set=utf8
 5 socket = /usr/local/mysql/mysql.sock
 6 
 7 [mysql]
 8 port = 3306
 9 default-character-set=utf8
10 socket = /usr/local/mysql/mysql.sock
11 
12 [mysqld]
13 user = mysql
14 basedir = /usr/local/mysql
15 datadir = /usr/local/mysql/data
16 port = 3306
17 character_set_server=utf8
18 pid-file = /usr/local/mysql/mysqld.pid
19 socket = /usr/local/mysql/mysql.sock
20 server-id = 1
21 
22 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

3.7、調整配置檔案許可權

1 [root@localhost ~]# chown mysql:mysql /etc/my.cnf

3.8、設定環境變數

1 [root@localhost ~]# echo 'PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >>/etc/profile
2 [root@localhost ~]# echo 'export PATH' >> /etc/profile
3 [root@localhost ~]# source /etc/profile

3.9、初始化資料庫

1 [root@localhost ~]# cd /usr/local/mysql/
2 [root@localhost mysql]# bin/mysqld \
3 > --initialize-insecure \
4 > --user=mysql \
5 > --basedir=/usr/local/mysql \
6 > --datadir=/usr/local/mysql/data

3.10、編輯啟動檔案

1 [root@localhost ~]# cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/

3.11、啟動mysql資料庫並檢視狀態

1 [root@localhost ~]# systemctl start mysqld
2 [root@localhost ~]# netstat -anpt | grep mysqld
3 tcp6       0      0 :::3306                 :::*                    LISTEN      67263/mysqld

3.12、設定root登陸並設定密碼

1 [root@localhost ~]# mysqladmin -uroot -p password 

四、編譯安裝PHP環境解析

4.1、安裝環境依賴包

 1 [root@localhost ~]# yum -y install \
 2 > libjpeg \
 3 > libjpeg-devel \
 4 > libpng libpng-devel \
 5 > freetype freetype-devel \
 6 > libxml2 \
 7 > libxml2-devel \
 8 > zlib zlib-devel \
 9 > curl curl-devel \
10 > openssl openssl-devel

4.2、上傳檔案至opt目錄下並解壓縮

1 [root@localhost ~]# cd /opt
2 [root@localhost opt]# tar jxf php-7.1.10.tar.bz2

4.3、configure配置

 1 [root@localhost opt]# cd php-7.1.10/
 2 [root@localhost php-7.1.10]# ./configure \
 3 > --prefix=/usr/local/php \
 4 > --with-mysql-sock=/usr/lcoal/mysql/mysql.sock \
 5 > --with-mysqli \
 6 > --with-zlib \
 7 > --with-curl \
 8 > --with-gd \
 9 > --with-jpeg-dir \
10 > --with-png-dir \
11 > --with-freetype-dir \
12 > --with-openssl \
13 > --enable-fpm \
14 > --enable-mbstring \
15 > --enable-xml \
16 > --enable-session \
17 > --enable-ftp \
18 > --enable-pdo \
19 > --enable-tokenizer \
20 > --enable-zip

4.4、編譯安裝

1 [root@localhost php-7.1.10]# make && make install

4.5、php有三個配置檔案,php.ini 核心配置檔案,php-fpm.conf 程序服務配置檔案,www.conf擴充套件配置檔案

1 [root@localhost php-7.1.10]# cp php.ini-development /usr/local/php/lib/php.ini
2 [root@localhost php-7.1.10]# vi /usr/local/php/lib/php.ini
3 date.timezone = Asia/Shanghai
4 mysqli.default_socket = /usr/lcoal/mysql/mysql.sock

4.6、配置及優化fpm模組

 1 [root@localhost php-7.1.10]# cd /usr/local/php/etc/
 2 [root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
 3 [root@localhost etc]# cd php-fpm.d/
 4 [root@localhost php-fpm.d]# cp www.conf.default www.conf
 5 [root@localhost etc]# vi php-fpm.conf
 6 pid = run/php-fpm.pid ###去掉;號
 7 
 8 [root@localhost etc]# /usr/local/php/sbin/php-fpm -c /usr/local/php/lib/php.ini
 9 [root@localhost etc]# netstat -ntap | grep 9000
10 tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN       118685/php-fpm: mas
11 
12 [root@localhost etc]# ln -s /usr/local/php/bin/* /usr/local/bin
13 [root@localhost etc]# ps aux | grep -c "php-fpm" ###檢視php-fpm程序數
14 4

4.7、配置Nginx支援PHP功能

 1 [root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
 2  location ~ \.php$ {
 3             root           /var/www/aa;
 4             fastcgi_pass   127.0.0.1:9000;
 5             fastcgi_index  index.php;
 6             include        fastcgi.conf;
 7         }
 8 
 9 [root@localhost ~]# vi /var/www/aa/index.php
10 <?php
11 phpinfo();
12 ?>
13 
14 [root@localhost ~]# systemctl restart nginx

4.8、在客戶端測試 http://192.168.73.40/index.php

4.9、測試資料庫工作是否正常

 1 [root@localhost ~]# mysql -u root -p
 2 Enter password: ###輸入之前設定的密碼
 3 
 4 mysql> create database test;
 5 Query OK, 1 row affected (0.00 sec)
 6 
 7 mysql> grant all on bbs.* to 'bbsuser'@'%' identified by 'admin123';
 8 Query OK, 0 rows affected, 1 warning (0.01 sec)
 9 
10 mysql> grant all on bbs.* to 'bbsuser'@'localhost' identified by 'admin123';
11 Query OK, 0 rows affected, 1 warning (0.00 sec)
12 
13 mysql> flush privileges;
14 Query OK, 0 rows affected (0.01 sec)
15 
16 [root@localhost ~]# vi /usr/local/nginx/html/index.php ###更改測試頁內容
17 <?php
18 $link=mysqli_connect('192.168.73.40','bbsuser','admin123');
19 if($link) echo "<h1>Success!</h1>";
20 else echo "Fail!!";
21 ?>
22 [root@localhost ~]# systemctl restart nginx

4.10、在客戶端輸入 http://192.168.73.40/index.php測試

五、部署Discuz!社群論壇Web應用

5.1、安裝

 1 [root@localhost ~]# cd /opt
 2 [root@localhost opt]# unzip Discuz_X3.4_SC_UTF8.zip ###將安裝包放在/opt目錄下並解壓縮
 3 [root@localhost opt]# cd dir_SC_UTF8/
 4 [root@localhost dir_SC_UTF8]# cp -r upload /var/www/aa/bbs
 5 [root@localhost dir_SC_UTF8]# cd /var/www/aa/bbs/
 6 ###給安裝論壇的檔案許可權
 7 [root@localhost bbs]# chown -R root:nginx ./config
 8 [root@localhost bbs]# chown -R root:nginx ./data
 9 [root@localhost bbs]# chown -R root:nginx ./uc_client
10 [root@localhost bbs]# chown -R root:nginx ./uc_server
11 [root@localhost bbs]# chmod -R 777 ./config
12 [root@localhost bbs]# chmod -R 777 ./data
13 [root@localhost bbs]# chmod -R 777 ./uc_client
14 [root@localhost bbs]# chmod -R 777 ./uc_server
15 [root@localhost bbs]# systemctl restart nginx

5.2、在客戶端輸入 http://192.168.73.40/bbs/install/index 進行論壇安裝

5.3、安裝完畢再用http://14.0.0.40/test/index.php登入論壇

5.4、登入http://14.0.0.40/test/admin.php可以直接進管理中心