LNMP論壇搭建

2020-08-12 18:47:42

搭建LAMP

LAMP的四個組成

瞭解LAMP堆疊的四個軟體層有助於故障排除過程,它允許我們看到每個層如何相互依賴。下面 下麪我們來具體瞭解一下LAMP的四個組成部分:

Linux:操作系統(OS)構成了我們的第一層。Linux爲堆疊模型奠定了基礎。所有其他圖層都在此圖層的頂部執行。

Apache:第二層由Web伺服器軟體組成,通常是Apache Web Server。該層位於Linux層之上。Web伺服器負責從Web瀏覽器轉換到正確的網站。

MySQL:我們的第三層是MySQL數據庫所在的位置。MySQL儲存可以通過指令碼查詢來構建網站的詳細資訊。MySQL通常與Apache /第2層一起位於Linux層之上。在高階設定中,MySQL可以解除安裝到單獨的主機伺服器。

PHP(Perl或Python):在它們之上的就是我們的第四層也是最後一層。指令碼層由PHP和/或其他類似的Web程式語言組成。網站和Web應用程式在此層中執行。

搭建Nginx

#安裝環境

[root@localhost opt]# yum -y install gcc gcc-c++ pcre pcre-devel zlib-devel

#解壓縮原始碼包

[root@localhost opt]# tar zxvf nginx-1.12.2.tar.gz

#建立nginx使用者

[root@localhost nginx-1.12.2]# useradd -M -s /sbin/nologin nginx

#編譯安裝

[root@localhost opt]# cd nginx-1.12.2/

[root@localhost nginx-1.12.2]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module

[root@localhost nginx-1.12.2]# make && make install

#路徑優化

[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin

#校驗組態檔語法錯誤並開啓服務檢視

[root@localhost nginx-1.12.2]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[root@localhost nginx-1.12.2]# nginx 

[root@localhost nginx-1.12.2]# netstat -antp | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      22287/nginx: master 

#新增Nginx系統服務

爲了使用Nginx服務啓動,停止,過載方便,可以編寫基於Nginx服務控制檔案使用systemctl

[root@localhost nginx-1.12.2]# 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 = /usr/bin/kill -s HUP $MAINPID
ExecStop = /usr/bin/kill -s QUIT $MAINPID
PrivateTmp = true
[Install]
WantedBy = multi-user.target

[root@localhost nginx-1.12.2]# chmod 754 /lib/systemd/system/nginx.service

[root@localhost nginx-1.12.2]# systemctl enable nginx.service 

[root@localhost nginx-1.12.2]# systemctl start nginx.service 

安裝MySQL

#安裝環境

[root@localhost opt]# yum -y install \
ncurses \
ncurses-devel \
bison \
cmake

#解壓原始碼包

[root@localhost opt]# tar zxvf mysql-boost-5.7.20.tar.gz 

#編譯安裝

[root@localhost opt]# cd mysql-5.7.20/

[root@localhost 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

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

建立執行使用者

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

數據庫目錄進行許可權調整

[root@localhost mysql-5.7.20]# chown -R mysql.mysql /usr/local/mysql

#調整組態檔

[root@localhost opt]# vim /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 = mysqlbasedir = /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

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

#設定環境變數

[root@localhost opt]# echo 'PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH'>>/etc/profile

[root@localhost opt]# echo 'export PATH'>>/etc/profile

[root@localhost opt]# source /etc/profile

#初始化數據庫

[root@localhost opt]# cd /usr/local/mysql/

[root@localhost mysql]# bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data

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

#數據庫開啓並檢視狀態

[root@localhost system]# systemctl start mysqld

[root@localhost system]# systemctl enable mysqld

[root@localhost system]# systemctl status mysqld

[root@localhost system]# netstat -antp | grep mysqld
tcp6       0      0 :::3306                 :::*                    LISTEN      21684/mysqld 

#新增數據庫的管理員密碼;密碼爲"abc123"

[root@localhost etc]# mysqladmin -u root -p password "abc123"
Enter password: 
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

安裝PHP

#安裝環境

[root@localhost opt]# yum -y install \
libjpeg \
libjpeg-devel \
libpng \
libpng-devel \
freetype \
freetype-devel \
libxml2 \
libxml2-devel \
zlib \
zlib-devel \
curl \
curl-devel \
openssl \
openssl-devel

#解壓原始碼包

[root@localhost opt]# tar jxvf php-7.1.10.tar.bz2

#編譯安裝

[root@localhost opt]# cd php-7.1.10/

[root@localhost 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

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

[root@localhost php-7.1.10]# cp php.ini-development /usr/local/php/lib/php.ini

[root@localhost php-7.1.10]# vim /usr/local/php/lib/php.ini 
......
date.timezone = Asia/Shanghai
......
mysqli.default_socket = /usr/local/mysql/mysql.sock

#驗證安裝的模組

[root@localhost php-7.1.10]# /usr/local/php/bin/php -m

#設定優化fpm模組

[root@localhost php-7.1.10]# cd /usr/local/php/etc/

[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf

[root@localhost etc]# cd /usr/local/php/etc/php-fpm.d/

[root@localhost php-fpm.d]# cp www.conf.default www.conf

[root@localhost php-fpm.d]# cd /usr/local/php/etc/

[root@localhost etc]# vim php-fpm.conf
pid = run/php-fpm.pid

[root@localhost etc]# /usr/local/php/sbin/php-fpm -c /usr/local/php/lib/php.ini 

[root@localhost etc]# netstat -antp | grep php-fpm

[root@localhost etc]# ln -s /usr/local/php/bin/* /usr/local/bin/
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      45498/php-fpm: mast 

[root@localhost etc]# ps aux | grep -c "php-fpm"
4

讓nginx支援php

[root@localhost etc]# vim /usr/local/nginx/conf/nginx.conf
        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;
        }

#設定測試網頁

[root@localhost etc]# vim /usr/local/nginx/html/index.php
<?php
phpinfo();
?>

[root@localhost etc]# systemctl restart nginx.service 

在瀏覽器上輸入"20.0.0.10/index.php"

image-20200808121913839

#測試數據庫

[root@localhost etc]# mysql -u root -p
Enter password:    輸入之前的abc123

mysql> create database bbs;

mysql> grant all on bbs.* to 'bbsuser'@'%' identified by 'admin123';

mysql> grant all on bbs.* to 'bbsuser'@'localhost' identified by 'admin123';

mysql> flush privileges;

[root@localhost etc]# vim /usr/local/nginx/html/index.php
<?php
$link=mysqli_connect('192.168.136.50','bbsuser','admin123');
if($link) echo "<h1>Success!!</h1>";
else echo "Fail!!";
?>

[root@localhost etc]# systemctl restart nginx

在瀏覽器上輸入"20.0.0.10/index.php"

image-20200808123251250

部署社羣論壇

#解壓原始碼包

[root@localhost opt]# unzip Discuz_X3.4_SC_UTF8.zip

[root@localhost opt]# cd dir_SC_UTF8/

[root@localhost dir_SC_UTF8]# cp -r upload/ /usr/local/nginx/html/bbs/

[root@localhost dir_SC_UTF8]# cd /usr/local/nginx/html/bbs/

#設定屬主屬組,許可權

[root@localhost bbs]# chown -R root:nginx ./config/
[root@localhost bbs]# chown -R root:nginx ./data/
[root@localhost bbs]# chown -R root:nginx ./uc_client/
[root@localhost bbs]# chown -R root:nginx ./uc_server/

[root@localhost bbs]# chmod -R 777 ./config/
[root@localhost bbs]# chmod -R 777 ./data/
[root@localhost bbs]# chmod -R 777 ./uc_client/
[root@localhost bbs]# chmod -R 777 ./uc_server/

#安裝社羣動力論壇

在瀏覽器輸入"http://192.168.136.40/bbs/install/index.php"

image-20200808123828703

image-20200812183831065

image-20200812183719745