LNMP架構及DISCUZ論壇部署
2)(5分)在Nginx伺服器上搭建LNMP服務,並且能夠對外提供Discuz論壇服務。
3)(5分)為Nginx服務配置虛擬主機,新增兩個域名 www.kgc.com和 www.benet.com,使用http://www.kgc.com/bbs/index.php可訪問Discuz論壇頁面。
使用http://www.benet.com則訪問/var/www/html目錄中的index.html檔案的內容,內容可自定義,要求有圖片。
1.環境
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
2.安裝
yum -y install pcre-devel zlib-devel gcc gcc-c++ make
3.建立執行使用者
useradd -M -s /sbin/nologin nginx
4.編譯安裝
cd /opt
將所需軟體包拖入
解壓:tar zxvf nginx-1.12.0.tar.gz
cd nginx-1.12.0/
編譯:
安裝:
make && make install -j4
5.路徑優化
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin
6.新增 Nginx 系統服務
vim /lib/systemd/system/nginx.service
[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 -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true [Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start nginx
MySQL
1.安裝依賴環境
yum -y install \
ncurses \
ncurses-devel \
bison \
cmake
2.建立執行使用者
useradd -s /sbin/nologin mysql
3.編譯安裝
cd /opt
將mysql軟體包拖入該目錄下進行解壓
tar -zxf mysql-boost-5.7.20.tar.gz
cd 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_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 && make install -j4
4.資料庫目錄進行許可權調整
chown -R mysql:mysql /usr/local/mysql
5.調整配置檔案
vi /etc/my.cnf
將檔案內容刪除重新寫入以下內容:
[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
[mysql]
port = 3306
default-character-set=utf8
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 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
6.設定環境變數
-
echo 'PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
-
echo 'export PATH' >> /etc/profile
7.初始化資料庫
cd /usr/local/mysql
bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
cp usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system
8.資料庫開啟自啟、 關閉、狀態
systemctl enable mysqld
systemctl start mysqld
systemctl status mysqld
netstat -anpt | grep 3306
9.設定Mysql密碼
ln -s /usr/local/mysql/bin/* /usr/local/bin
mysqladmin -u root -p password
10.登入資料庫
mysql -u root -p
php
1.安裝環境依賴包
yum -y install \
libjpeg \
libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 \ libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel
2.編譯安裝
cd /opt
將php軟體包拖入該目錄下進行解壓
tar -jxf 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 && make install -j4
3.修改配置檔案
cp php.ini-development /usr/local/php/lib/php.ini
vim /usr/local/php/lib/php.ini
-
1170 mysqli.default_socket = /usr/local/mysql/mysql.sock
-
939 date.timezone = Asia/Shanghai
驗證安裝
/usr/local/php/bin/php -m
4.配置及優化FPM模組
-
cd /usr/local/php/etc/
-
cp php-fpm.conf.default php-fpm.conf
-
cd /usr/local/php/etc/php-fpm.d/
-
cp www.conf.default www.conf
cd /usr/local/php/etc
-
vi php-fpm.conf
pid = run/php-fpm.pid #將這一行註釋去掉
/usr/local/php/sbin/php-fpm -c /usr/local/php/lib/php.ini
ps aux | grep -c "php-fpm"
5.進入nginx主配置檔案
vi /usr/local/nginx/conf/nginx.conf
--65行--取消註釋,修改
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; #將/scripts修改為nginx的工作目錄
include fastcgi_params;
}
systemctl restart nginx.service
6.建立網頁檔案
vi /usr/local/nginx/html/index.php
<?php
phpinfo();
?>
systemctl restart nginx
http://172.16.10.101/index.php
論壇
登入資料庫
mysql -u root -p
CREATE DATABASE bbs;
GRANT all ON bbs.* TO 'bbsuser'@'%' IDENTIFIED BY 'admin123';
GRANT all ON bbs.* TO 'bbsuser'@'localhost' IDENTIFIED BY 'admin123';
flush privileges;
systemctl restart nginx
cd /opt
將discuz軟體包拖入該目錄下進行解壓
unzip Discuz_X3.4_SC_UTF8.zip
cd dir_SC_UTF8/
cp -r upload/ /usr/local/nginx/html/bbs
cd /usr/local/nginx/html/bbs
chown -R root:nginx ./config/
chown -R root:nginx ./data/
chown -R root:nginx ./uc_client/
chown -R root:nginx ./uc_server/
chmod -R 777 ./config/
chmod -R 777 ./data/
chmod -R 777 ./uc_client/
chmod -R 777 ./uc_server/
http://172.16.10.101/bbs/install/index.php
點選“我同意”
點選“下一步”
點選“下一步”
設定完成後點選下一步
http://172.16.10.101/bbs/index.php
echo "172.16.10.101 www.kgc.com www.benet.com" >>/etc/hosts
mkdir -p /var/www/html
cd /var/www/html
拉入圖片(bs.jpg error.png)
vim index.html
<img src="bs.jpg"/
</body>
</html>
vim /usr/local/nginx/conf/nginx.conf
35行修改:
server {
listen 80;
server_name www.kgc.com;
access_log logs/kgc.com.access.log;
location / {
root /usr/local/nginx/html;
index index.html index.htm;
}
81行新增以下內容:
server {
listen 80;
server_name www.benet.com;
charset utf-8;
access_log logs/benet.com.access.log;
location / {
root /var/www/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
nginx -t
systemctl restart nginx.sevice
http://www.kgc.com/bbs/index.php
4)(5分)對基於www.benet.com域名的虛擬機器主機的nginx服務調優:隱藏nginx版本號,快取靜態圖片網頁時間為1天,設定防盜鏈功能。根據日期對訪問日誌進行日誌分割,要求每天生成一份日誌檔案
http模組下加
server_tokens off;
benet server模組下加
location ~*\.(jpg|jpeg)$ {
root /var/www/html;
expires 1d;
}
配置盜鏈主機
yum install -y httpd
cd /var/www/html
vim index.html
<html>
<body>
<h1>dao lian </h1>
<img src="http://www.benet.com/bs.jpg"/>
</body>
</html>
echo "172.16.10.101 www.benet.com">>/etc/hosts
echo "172.16.10.200 www.aaa.com">>/etc/hosts
systemctl restart httpd
nginx主機下
cd /var/www/html
拉入圖片error.png
vim /usr/local/nginx/conf/nginx.conf
在expires 1d;新增
valid_referers none blocked *.benet.com benet.com;
if ( $invalid_referer ) {
rewrite ^/ http://www.benet.com/error.png;
}
systemctl restart nginx
5)(5分)要求使用location將請求http://www.kgc.com/index.php的訪問跳轉到http://www.kgc.com/bbs/index.php。
要求使用rewrite將使用域名www.benet.com請求以 .php 結尾的訪問都跳轉到域名www.kgc.com上,而且後面的引數保持不變,比如訪問http://www.benet.com/bbs/index.php跳轉到http://www.kgc.com/bbs/index.php。
vim /usr/local/nginx/conf/ngin.conf
kgc server 模組下加
location = /index.php {
rewrite (.+) http://www.kgc.com/bbs/index.php;
}
benet server模組下加
location ~ \.php$ {
rewrite (.+) http://www.kgc.com/$1 permanent;
}
systemctl restart nginx
http://www.benet.com/bbs/index.php
6)(10分)在Web1、Web2伺服器上搭建Tomcat服務;在Nginx伺服器上對基於www.benet.com域名的虛擬機器主機設定動靜分離由nginx提供靜態頁面服務,將對.jsp檔案的動態頁面請求轉發到Tomcat伺服器處理,配合2臺Tomcat節點實現負載均衡
1.環境準備
systemctl stop firewalld
setenforce 0
2.安裝jdk
cd /opt
將jdk和tomcat軟體包拖入當前目錄下進行解壓
rpm -ivh jdk-8u201-linux-x64.rpm
3.修改檔案
vim /etc/profile
export JAVA_HOME=/usr/java/jdk1.8.0_201-amd64
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
export PATH=$JAVA_HOME/bin:$PATH
source /etc/profile
4.安裝tomcat
cd /opt
tar -zxf apache-tomcat-9.0.16.tar.gz
cp -r apache-tomcat-9.0.16 /usr/local/tomcat
5.啟動tomcat
-
cd /usr/local/tomcat/
-
/usr/local/tomcat/bin/startup.sh
-
ss -natp |grep 8080
6.將tomcat新增到服務
/usr/local/tomcat/bin/shutdown.sh
useradd -s /sbin/nologin tomcat
chown tomcat:tomcat /usr/local/tomcat -R
vim /etc/systemd/system/tomcat.service
[Unit]
Description=Tomcat
#After=syslog.target network.target remote-fs.target nss-lookup.target
After=syslog.target network.target
[Service]
Type=forking
ExecStart=/usr/local/tomcat/bin/startup.sh
ExecStop=/usr/local/tomcat/bin/shutdown.sh
RestartSec=3
PrivateTmp=true
User=tomcat
Group=tomcat
[Install]
WantedBy=multi-user.target
重新載入服務,並開啟,檢視是否成功啟動
systemctl daemon-reload
systemctl start tomcat
ss -ntap |grep 8080 (web2)
7.Tomcat服務優化
cd /usr/local/tomcat/webapps
mkdir test
cd test
vim index.jsp(web1)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head> <title>JSP test1page</title> </head>
<body>
<% out.println("動態頁面 1,this is dynamic web1");%>
</body>
</html>
vim index.jsp(web2)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head> <title>JSP test2page</title> </head>
<body>
<% out.println("動態頁面 2,this is dynamic web2");%>
</body>
</html>
vim /usr/local/tomcat/conf/server.xml
<Host name="localhost" appBase="/usr/local/tomcat/webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> <Context docBase="/usr/local/tomcat/webapps/test" path="" reloadable="true" />
/usr/local/tomcat/bin/shutdown.sh
/usr/local/tomcat/bin/startup.sh
nginx 主機配置
cd /usr/local/nginx/html
<html>
<body>
<h1>這是靜態頁面</h1>
</body>
</html>
mkdir /usr/local/nginx/html/img
cd /usr/local/nginx/html
拉入圖片(cf.jpg)
vim /usr/local/nginx/conf/nginx.conf
server模組上面新增
upstream tomcat_server {
server 172.16.10.10:8080 weight=1;
server 172.16.10.20:8080 weight=1;
}
benet模組內新增
location ~ .*.jsp$ {
proxy_pass http://tomcat_server;
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ .*.(gif|jpg|jpeg|png|bmp|swf|css)$ {
root /usr/local/nginx/html/img;
expires 10d;
}
systemctl restart nginx
http://www.benet.com/index.jsp
7)(5分)在閘道器伺服器上配置DNAT和DNS,使client可以通過www.benet.com域名訪問閘道器伺服器的ens36介面的地址即可驗證上題結果。
systemctl stop firewalld
setenforce 0
yum install bind bind-utils -y
vim /etc/named.conf
any any
vim /etc/named.rfc1912.zones
zone "benet.com" IN {
type master;
file "benet.com.zone";
};
cd /var/named
cp -p named.localhost benet.com.zone
vim benet.com.zone
$TTL 1D
@ IN SOA @ rname.invalid. (
0
1D
1H
1W
3H )
NS @
A 172.16.10.101
www A 172.16.10.101
systemctl restart named
host www.benet.com
iptables -t nat -A PREROUTING -i ens37 -d 12.0.0.1 -p tcp --dport 80 -j DANT --to 172.16.10.101