1. 程式人生 > >基於(linux + nginx + mysql + php)原始碼包安裝及搭建

基於(linux + nginx + mysql + php)原始碼包安裝及搭建

lnmp是什麼?

LNMP指的是一個基於CentOS/Debian編寫的Nginx、PHP、MySQL、phpMyAdmin、eAccelerator一鍵安裝包。可以在VPS、獨立主機上輕鬆的安裝LNMP生產環境。
簡介:
LNMP代表的就是:Linux系統下Nginx+MySQL+PHP這種網站伺服器架構。
Linux是一類Unix計算機作業系統的統稱,是目前最流行的免費作業系統。代表版本有:debian、centos、ubuntu、fedora、gentoo等。
Nginx是一個高效能的HTTP和反向代理伺服器,也是一個IMAP/POP3/SMTP代理伺服器。
Mysql是一個小型關係型資料庫管理系統。
PHP是一種在伺服器端執行的嵌入HTML文件的指令碼語言。
這四種軟體均為免費開源軟體,組合到一起,成為一個免費、高效、擴充套件性強的網站服務系統。

特點:

作為 Web 伺服器:相比 Apache,Nginx 使用更少的資源,支援更多的併發連線,體現更高的效率。
作為負載均衡伺服器:Nginx 既可以在內部直接支援Rails和PHP,也可以支援作為 HTTP代理伺服器對外進行服務。Nginx 用C編寫,不論是系統資源開銷還是CPU使用效率都比Perlbal要好的多。
作為郵件代理伺服器:Nginx同時也是一個非常優秀的郵件代理伺服器(最早開發這個產品的目的之一也是作為郵件代理伺服器),Last/fm 描述了成功並且美妙的使用經驗。
Nginx 安裝非常的簡單,配置檔案非常簡潔(還能夠支援perl語法)。Nginx支援平滑載入新的配置,還能夠在不間斷服務的情況下進行軟體版本的升級。

1. Mqsql原始碼包安裝部署:

tar zxf mysql-boost-5.7.17.tar.gz #解壓mysql
rpm -ivh cmake-2.8.12.2-4.el6.x86_64.rpm #cmake 有依賴包
yum install libarchive.so.2 -y
yum install cmake-2.8.12.2-4.el6.x86_64.rpm
yum install -y gcc gcc-c++ ncurses-devel bison#gcc-c++編譯cmake
cd mysql-5.7.17/
rm -f CMakeCache.txt #cmake執行時會先讀快取檔案
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/lnmp/mysql #安裝目錄 tar zxf mysql-boost-5.7.17.tar.gz #解壓mysql rpm -ivh cmake-2.8.12.2-4.el6.x86_64.rpm #cmake 有依賴包 yum install libarchive.so.2 -y yum install cmake-2.8.12.2-4.el6.x86_64.rpm yum install -y gcc gcc-c++ #gcc-c++編譯cmake
cd mysql-5.7.17/
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/lnmp/mysql #安裝目錄
-DMYSQL_DATADIR=/usr/local/lnmp/mysql/data #存放資料預設路徑在
-DMYSQL_UNIX_ADDR=/usr/local/lnmp/mysql/data/mysql.sock 
-DWITH_INNOBASE_STORAGE_ENGINE=1 
-DDEFAULT_CHARSET=utf8 #字元和校驗字元必須保持一致
-DDEFAULT_COLLATION=utf8_general_ci 
-DEXTRA_CHARSETS=all
預設埠3306如果不更改可以不寫

DCMAKE_INSTALL_PREFIX:指定MySQL程式的安裝目錄,預設/usr/local/mysql
DEFAULT_CHARSET:指定伺服器預設字符集,預設latin1
DEFAULT_COLLATION:指定伺服器預設的校對規則,預設latin1_general_ci
ENABLED_LOCAL_INFILE:指定是否允許本地執行LOAD DATA INFILE,預設OFF
WITH_COMMENT:指定編譯備註資訊
WITH_xxx_STORAGE_ENGINE:指定靜態編譯到mysql的儲存引擎,MyISAM,MERGE,MEMBER以及CSV四種引擎預設即被編譯至伺服器,不需要特別指定。
WITHOUT_xxx_STORAGE_ENGINE:指定不編譯的儲存引擎
SYSCONFDIR:初始化引數檔案目錄
MYSQL_DATADIR:資料檔案目錄
MYSQL_TCP_PORT:服務埠號,預設3306
MYSQL_UNIX_ADDR:socket檔案路徑,預設/tmp/mysql.sock

yum install ncurses-devel bison -y
rm -f CMakeCache.txt #cmake執行時會先讀快取檔案

cmake -DCMAKE_INSTALL_PREFIX=/usr/local/lnmp/mysql #安裝目錄
-DMYSQL_DATADIR=/usr/local/lnmp/mysql/data #存放資料預設路徑在
-DMYSQL_UNIX_ADDR=/usr/local/lnmp/mysql/data/mysql.sock 
-DWITH_INNOBASE_STORAGE_ENGINE=1 
-DDEFAULT_CHARSET=utf8 #字元和校驗字元必須保持一致
-DDEFAULT_COLLATION=utf8_general_ci 
-DEXTRA_CHARSETS=all
make
make install

cd /usr/local/
df -h #mysql大概佔用了9G的記憶體
cd /usr/local/lnmp/mysql/
ls
rpm -qa|grep mysql #檢視系統的mysql安裝包不能解除安裝因為他的解除安裝存在一定的依賴性會
對系統的其他服務產生影響
cd support-files/
ls #/usr/local/lnmp/mysql/support-files目錄下有mysql的預設配置檔案指令碼my-default.cnf
pwd #/usr/local/lnmp/mysql/support-files
vim my-default.cnf #檢視指令碼檔案覆蓋系統中mysql的配置檔案
cp my-default.cnf /etc/my.cnf
vim /etc/my.cnf
 basedir = /usr/local/lnmp/mysql
 datadir = /usr/local/lnmp/mysql/data
 port = 3306
 socket = /usr/local/lnmp/mysql/data/mysql.sock
 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

file mysql.server #檢視mysql.server檔案型別
 mysql.server: POSIX shell script text executable
vim mysql.server #檢視檔案中的mysql路徑
cp mysql.server /etc/init.d/mysqld #將其複製到/etc/init.d/下方便用指令碼啟動服務
ll /etc/init.d/mysqld #許可權為root
   切換到/usr/local/lnmp/mysql/
groupadd -g 27 mysql #建立mysql使用者
useradd -u 27 -g 27 -M -d /usr/local/lnmp/mysql/data -s /sbin/nologin mysql
chown -R mysql.mysql . #修改/usr/local/lnmp/mysql/目錄下的許可權
ll
cd
vim .bash_profile #指令碼路徑---->PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin
source .bash_profile #使更改生效

cd /usr/local/lnmp/mysql/
mysqld --initialize --user=mysql #初始化服務生成data檔案

vim /etc/init.d/mysqld 檢視指令碼檔案
/etc/init.d/mysqld start #開啟mysqld服務如果成功則證明沒有問題但是不夠安全mysql使用者的許可權太大我們只需要給mysql使用者使用data的許可權
chgrp root . -R #修改/usr/local/lnmp/mysql/下檔案的組
chown root . -R #修改/usr/local/lnmp/mysql/下檔案的owner
chown mysql data/ -R
ll

/etc/init.d/mysqld start
mysql_secure_installation #安裝加密安全認證修改使用者密碼初始密碼在初始化過程中產
生可以複製下來然後修改mysql密碼
 Would you like to setup VALIDATE PASSWORD plugin?
Press y|Y for Yes, any other key for No: N #不需要密碼安全檢測該檢測會檢測設定的密碼強度如果低於8位不能通過
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) :#要修改之前設定的密碼麼 
Remove anonymous users? (Press y|Y for Yes, any other key for No) : 
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : 
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : 
/etc/init.d/mysqld start
mysql -p #登陸mysql

php原始碼安裝:

解壓php壓縮包:
1 tar jxf php-5.6.35.tar.bz2
2 cd php-5.6.35
#新增功能模組進行編譯
3  ./configure --prefix=/usr/local/lnmp/php  --with-config-file-path=/usr/local/lnmp/php/etc  --with-mysql=mysqlnd  --enable-mysqlnd  --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd  --with-openssl  --with-snmp --with-gd --with-zlib --with-curl --with-libxml-dir  --with-png-dir  --with-jpeg-dir  --with-freetype-dir --with-pear --with-gettext --with-gmp  --enable-inline-optimization --enable-soap --enable-ftp  --enable-sockets --enable-mbstring --enable-fpm  --with-fpm-user=nginx --with-fpm-group=nginx --with-mhash
4 cd
# 安裝功能模組需要的包
5 yum install  libxml2-devel openssl-devel curl-devel gd-devel-2.0.35-11.el6.x86_64.rpm  re2c-0.13.5-1.el6.x86_64.rpm gmp-devel libmcrypt-devel-2.5.8-9.el6.x86_64.rpm libmcrypt-2.5.8-9.el6.x86_64.rpm net-snmp-devel -y
# 需要的安裝包:       
gd-devel-2.0.35-11.el6.x86_64.rpm      libmcrypt-2.5.8-9.el6.x86_64.rpm        php-5.6.35.tar.bz2              libmcrypt-devel-2.5.8-9.el6.x86_64.rpm  re2c-0.13.5-1.el6.x86_64.rpm

編譯安裝:

6 cd  php-5.6.35
7 ./configure --prefix=/usr/local/lnmp/php \
 --with-config-file-path=/usr/local/lnmp/php/etc \
 --with-mysql=mysqlnd  --enable-mysqlnd \
 --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd  --with-openssl \
 --with-snmp --with-gd --with-zlib --with-curl --with-libxml-dir \
 --with-png-dir  --with-jpeg-dir \
 --with-freetype-dir --with-pear --with-gettext --with-gmp 
 8 make && make install

2.1>配置php:

cd /usr/local/lnmp/php/etc/
cp php-fpm.conf.default php-fpm.conf
cd /root/php-5.6.35
cp /root/php-5.6.35/php.ini-production   /usr/local/lnmp/php/etc/php.ini
vim php.ini #修改時區 
vim php-fpm.conf #pid = run/php-fpm.pid  去掉註釋因為預設值時none

useradd -M -d /usr/local/lnmp/nginx -s /sbin/nologin  nginx
cd /root/php-5.6.35/sapi/fpm/
cp init.d.php-fpm /etc/init.d/php-fpm #在原始碼包複製啟動指令碼
chmod +x /etc/init.d/php-fpm #指令碼新增可執行許可權
/etc/init.d/php-fpm start #啟動服務
netstat -antlp #檢視監聽埠9000

3. 安裝nginx原始碼包佈署nginx

tar zxf nginx-sticky-module-ng.tar.gz #解壓nginx原始碼包
tar zxf nginx-1.10.1.tar.gz #解壓nginx演算法包
vim /nginx-1.10.1/src/core/nginx.h #取消版本顯示
vim auto/cc/gcc #取消debug編譯
cd nginx-1.10.1
./configure --prefix=/usr/local/lnmp/nginx --with-http_ssl_module --with-http_stub_status_module  --user=nginx  --group=nginx --with-threads  --with-file-aio #報錯需要一個pcre-devel包出什麼錯我們解決什麼錯
yum install -y pcre-devel

繼續編譯:
./configure --prefix=/usr/local/lnmp/nginx --with-http_ssl_module --with-http_stub_status_module  --user=nginx  --group=nginx --with-threads  --with-file-aio
make && make install
vim nginx.conf #worker_connections  65535
vim /etc/security/limits.conf #修改系統引數
# End of file
nginx   -       nofile  65536
sysctl -a | grep file #檢視核心引數
fs.file-nr = 480    0   291272
fs.file-max = 291272

useradd -M -d /usr/local/lnmp/nginx -s /sbin/nologin  nginx
cd
vim .bash_profile #新增指令碼路徑 
source .bash_profile #重新整理更改
cd /usr/local/lnmp/nginx/conf/

2.1在nginx環境部署php

vim nginx.conf #修改nginx配置檔案使nginx環境支援php

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name
            include        fastcgi.conf;
            fastcgi.conf這個檔案中預設有該指令碼路徑
fastcgi_param  SCRIPT_FILENAME      /scripts$fastcgi_script_name;
我們也可以指定:astcgi_param  $ SCRIPT_FILENAME /scripts/usr/local/lnmp/nginx/html/$fastcgi_script_name;}

ln -s /usr/local/lnmp/nginx/sbin/nginx /sbin/nginx #建立軟連結
vim /usr/local/lnmp/nginx/html/index.php #編寫php檔案在nginx釋出家目錄
nginx -s reload #重新載入nginx



在瀏覽器訪問172.25.30.5為nginx—> /usr/local/lnmp/nginx/html/index.html

在瀏覽器訪問172.25.30.5/index.php為編寫的php介面如果想直接訪問可以更改預設釋出次序 index index.php index.html index.htm;

2.2. 論壇部署:

yum install -y unzip
unzip Discuz_X3.2_SC_UTF8.zip -d /usr/local/lnmp/nginx/html/
cd /usr/local/lnmp/nginx/html/#該目錄下生成了upload目錄upload中有data檔案我們想要對他進行操作並將data和之前的mysql結合起來因此我們需要給mysql使用者操作upload中一些目錄可操作的權力:
mv  upload bbs


登陸安裝論壇:



 chmod 777 config/ data/ uc_server/ uc_client/ -R
/etc/init.d/mysqld start
此時我們可以登陸我們的數路庫:會發現我們找不到路徑:因此我們得在php動態配置檔案中告訴他們mysql路徑
cd /usr/local/lnmp/php/etc/
vim php.ini
/etc/init.d/php-fpm reload

再次登陸會告訴我們許可權不夠那我們就給一個777的許可權:
cd /usr/local/lnmp/mysql
chown mysql. data -R
ll




此時就可以訪問我們登陸之後可以刪除rm -rf bbs/index.php

4.1 php新增memcache服務加速

MEMCACHE:

MemCache的工作流程如下:先檢查客戶端的請求資料是否在memcached中,如有,直接把請求資料返回,不再對資料庫進行任何操作;如果請求的資料不在memcached中,就去查資料庫,把從資料庫中獲取的資料返回給客戶端,同時把資料快取一份到memcached中(memcached客戶端不負責,需要程式明確實現);每次更新資料庫的同時更新memcached中的資料,保證一致性;當分配給memcached記憶體空間用完之後,會使用LRU(Least Recently Used,最近最少使用)策略加上到期失效策略,失效資料首先被替換,然後再替換掉最近未使用的資料。

Memcache面向物件的常用介面包括:
Memcache::connect -- 開啟一個到Memcache的連線
Memcache::pconnect -- 開啟一個到Memcache的長連線
Memcache::close -- 關閉一個Memcache的連線
Memcache::set -- 儲存資料到Memcache伺服器上
Memcache::get --提取一個儲存在Memcache伺服器上的資料
Memcache::replace --替換一個已經存在Memcache伺服器上的專案(功能類似Memcache::set)
Memcache::delete -- 從Memcache伺服器上刪除一個儲存的專案
Memcache::flush -- 重新整理所有Memcache伺服器上儲存的專案(類似於刪除所有的儲存的專案)
Memcache::getStats -- 獲取當前Memcache伺服器執行的狀態

PHP資料快取
PHP的資料快取包括針對資料庫資料進行快取和針對PHP模板資料進行快取。針對資料庫資料進行快取的工具有memcache等。針對PHP模板資料進行快取的工具主要有smarty等。

給php新增memcache快取
tar zxf memcache-2.2.5.tgz 
cd /usr/local/lnmp/php/bin/
vim .bash_profile 
source .bash_profile  #新增php指令碼執行環境
cd memcache-2.2.5
phpize #監控
./configure #直接生成/usr/local/lnmp/php/lib/php/extensions/no-debug-non-zts-20131226/memcache.so
make 
make install 

cd  /usr/local/lnmp/php/lib/php/extensions/no-debug-non-zts-20131226/
ls
yum install -y memcached 
php -m | grep memcache
cd /usr/local/lnmp/php/etc
vim php.ini 
extension = memcache.so
/etc/init.d/php-fpm restart
php -m | grep memcache
/etc/init.d/memcached start
netstat -antlp | grep :11211
cd memcache-2.2.5
cp memcache.php example.php  /usr/local/lnmp/nginx/html/
rpm -q memcached
cd /usr/local/lnmp/nginx/html/
ls
vim example.php 
/etc/init.d/php-fpm start


給php新增快取模組配置檔案:

客戶端訪問example.php不斷重新整理會發現命中快取越高:


我們不能把頁面暴露在外面因此與要加密:
vim example.php


看看加入快取之後有沒有起到加速的作用:
客戶端測試:

4.2 Nginx 新增快取

OpenResty執行原理:
Nginx 採用的是 master-worker 模型,一個 master 程序管理多個 worker 程序,基本的事件處理都是放在woker 中,master 負責一些全域性初始化,以及對 worker 的管理。在OpenResty中,每個 woker 使用一個LuaVM,當請求被分配到 woker 時,將在這個 LuaVM 裡建立一個 coroutine(協程)。協程之間資料隔離,每個協程具有獨立的全域性變數_G。ps. 協程和多執行緒下的執行緒類似:有自己的堆疊,自己的區域性變數,有自己的指令指標,但是和其他協程程式共享
全域性變數等資訊。執行緒和協程的主要不同在於:多處理器的情況下,概念上來說多執行緒是同時執行多個執行緒,而協程是通過程式碼來完成協程的切換,任何時刻只有一個協程程式在執行。並且這個在執行的協程只有明確被要求掛起
時才會被掛起。
安裝openstry

tar zxf openresty-1.13.6.1.tar.gz 
nginx -s stop #我們可以安裝一個新的nginx給nginx新增快取;也可以用之前安裝的nginx給其新增快取安裝過程和之前一樣
cd openresty-1.13.6.1
./configure --prefix=/usr/local/lnmp/openresty --with-http_ssl_module --with-http_stub_status_module  --user=nginx  --group=nginx --with-threads  --with-file-aio
make 
make install

配置nginx:

cd /usr/local/lnmp/openresty/nginx/conf/
vim nginx.conf
/usr/local/lnmp/openresty/nginx/sbin/nginx -t
/usr/local/lnmp/openresty/nginx/sbin/nginx 
cd /usr/local/lnmp/openresty/nginx/html/
cp /usr/local/lnmp/nginx/html/example.php .


nginx.conf詳情引數:

events {
    worker_connections  65535;
}
模組一:訪問本機的11211埠即memcache埠:
http {

        upstream memcache {
        server localhost:11211;
        keepalive 512 ;
        }
    include       mime.types;
    default_type  application/octet-stream;

模組二:訪問地址/memc中定義
        #access_log  logs/host.access.log  main;
        location / {
            root   html;
            index  index.html index.htm;
        }
        location /memc {
            internal;
            memc_connect_timeout 100ms;
            memc_send_timeout 100ms;
            memc_read_timeout 100ms;
            set $memc_key $query_string;
            set $memc_exptime 300;
            memc_pass memcache;

        }
模組三: fastcgi通過9000在memc中存取
      location ~ \.php$ {
           set $key $uri$args;
           srcache_fetch GET /memc $key;
           srcache_store PUT /memc $key;
           root           html;
           fastcgi_pass   127.0.0.1:9000;
           fastcgi_index  index.php;
           #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
           include        fastcgi.conf;
        }




訪問nginx頁面:

讓我們來看看給nginx加了快取有沒有起到加速的效果: