環境:php
系統硬件:vmware vsphere (CPU:2*4核,內存2G,雙網卡)html
系統版本:CentOS-7-x86_64-Minimal-1503-01.isomysql
安裝步驟:linux
1.準備nginx
1.1 設置靜態IP、DNS地址(網絡設備名稱有可能不同,這裏是eno16780032,如使用DHCP獲取動態IP,可忽略)c++
[root@centos ~]# vi /etc/sysconfig/network-scripts/ifcfg-eno16780032git
找到BOOTPROTO,而且修改(設爲靜態網址)github
BOOTPROTO="static"web
在最後添加三行內容(添加IP,子網掩碼,網關)算法
IPADDR="192.168.1.117"
NETMASK="255.255.255.0"
GATEWAY="192.168.1.1"
:wq 保存退出
[root@centos ~]# vi /etc/resolv.conf
添加如下幾個DNS地址
nameserver 114.114.114.114
nameserver 192.168.1.1
nameserver 8.8.8.8
:wq 保存退出
1.2 顯示IP地址
[root@centos ~]# ip addr|grep inet
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
inet 192.168.1.117/24 brd 192.168.1.255 scope global eno16780032
inet6 fe80::250:56ff:feb0:30f2/64 scope link
1.3 更新系統,顯示系統版本
[root@centos ~]# yum update -y
更新完成後重啓,查看內核版本
[root@centos ~]# shutdown -r now
[root@centos ~]# cat /etc/redhat-release
CentOS Linux release 7.1.1503 (Core)
[root@centos ~]# uname -a
Linux centos 3.10.0-229.20.1.el7.x86_64 #1 SMP Tue Nov 3 19:10:07 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
1.4 安裝基本軟件包
[root@centos ~]# yum install vim wget lsof gcc gcc-c++ bzip2 -y
[root@centos ~]# yum install net-tools bind-utils -y
1.5 關閉selinux (如不關閉,有時會添加不了用戶,或者重啓後無法開機)
[root@centos ~]# vim /etc/selinux/config
屏蔽如下兩行
#SELINUX=enforcing
#SELINUXTYPE=targeted
添加如下一行
SELINUX=disabled
保存,退出
重啓後,查詢是否關閉(顯示Disabled則表示關閉)
[root@centos ~]# shutdown -r now
[root@centos ~]# getenforce
Disabled
1.6 設置PUTTY遠程登陸時,不使用密碼,使用密鑰文件登陸(如不須要,可忽略)
1.6.1 在客戶機生成對稱密鑰
ssh-keygen -t rsa
1.6.2 把客戶機上的公鑰複製到服務器(公鑰文件:id_rsa.pub)
建立目錄
[root@centos ~]# mkdir -p /root/.ssh
使用軟件遠程複製id_rsa.pub到服務器/root/.ssh中。
查看服務器上,公鑰是否已經存在
[root@centos ~]# ll /root/.ssh
-rw-r--r-- 1 root root 394 12月 5 09:33 id_rsa.pub
導入密鑰到authorized_keys
[root@centos ~]# cat id_rsa.pub >> authorized_keys
[root@centos ~]# ll /root/.ssh
-rw-r--r-- 1 root root 394 12月 5 09:37 authorized_keys
-rw-r--r-- 1 root root 394 12月 5 09:33 id_rsa.pub
導入後,刪除公鑰文件
[root@centos ~]# rm id_rsa.pub
1.6.3 設置sshd配置文件
[root@centos ~]# vim /etc/ssh/sshd_config
找到GSSAPICleanupCredentials,而且修改成如下內容
GSSAPICleanupCredentials yes
:wq 保存退出
重啓sshd服務,讓其生效
[root@centos ~]# systemctl restart sshd
1.6.4 客戶端設置PUTTY,進行遠程登陸
打開軟件 PuTTYgen
點擊load 選擇以前客戶機生成私鑰文件id_rsa, 點擊save private key 生成 pKey.ppk文件
打開軟件 PuTTY
點擊Session,在HostName(or IP address)輸入服務器地址
點擊Connection下的DATA,在Auto-login username中輸入登陸帳號(當前帳號爲root)
點擊Connection下的SSH下的Auth,點擊Browse 選擇以前生成 pKeyppk文件
點擊Session,在Saved Sessions中,輸入須要保存的Session名稱,點擊保存
1.6.5 設置完成後,便可以遠程鏈接到服務器
打開軟件 PuTTY
點擊Session,在"Default Settings"下,找到以前已經保存的Session,雙擊打開鏈接
若是顯示 Authenticating with public key "xxxxx-xxxx"時,即表未成功
1.7 下載源碼包(或直接經過FTP上傳到指定目錄)
[root@centos ~]# cd /usr/local/src
[root@centos ~]# wget https://cmake.org/files/v3.4/cmake-3.4.1.tar.gz
[root@centos ~]# wget http://ftp.gnu.org/gnu/bison/bison-3.0.4.tar.gz
[root@centos ~]# wget https://github.com/jemalloc/jemalloc/releases/download/4.0.4/jemalloc-4.0.4.tar.bz2
[root@centos ~]# wget https://downloads.mariadb.org/f/mariadb-10.1.9/source/mariadb-10.1.9.tar.gz
[root@centos ~]# wget http://sourceforge.net/projects/pcre/files/pcre/8.37/pcre-8.37.tar.gz
[root@centos ~]# wget https://www.openssl.org/source/openssl-1.0.2e.tar.gz
[root@centos ~]# wget http://zlib.net/zlib-1.2.8.tar.gz
[root@centos ~]# wget http://nginx.org/download/nginx-1.9.9.tar.gz
[root@centos ~]# wget http://am1.php.net/get/php-5.5.30.tar.gz
[root@centos ~]# wget http://ftp.gnu.org/gnu/libiconv/libiconv-1.14.tar.gz
[root@centos ~]# wget http://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
[root@centos ~]# wget http://sourceforge.net/projects/mcrypt/files/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz
[root@centos ~]# wget http://sourceforge.net/projects/mhash/files/mhash/0.9.9.9/mhash-0.9.9.9.tar.gz
[root@centos ~]# wget http://pecl.php.net/get/zendopcache-7.0.5.tgz
[root@centos ~]# wget http://xdebug.org/files/xdebug-2.4.0rc2.tgz
[root@centos ~]# wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz
[root@centos ~]# wget http://pecl.php.net/get/memcached-2.2.0.tgz
[root@centos ~]# wget https://sourceforge.net/projects/levent/files/libevent/libevent-2.0/libevent-2.0.22-stable.tar.gz
[root@centos ~]# wget http://www.memcached.org/files/memcached-1.4.25.tar.gz
查看是否已經須要的包是否下載完成(以下載地址失效,需另外下載並上傳到服務器)
[root@centos ~]# ll /usr/local/src
2.安裝mariadb
2.1 安裝依賴
[root@centos ~]# yum install ncurses-devel openssl* bzip2 m4 -y
2.2 安裝cmake
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# wget https://cmake.org/files/v3.4/cmake-3.4.1.tar.gz
[root@centos ~]# tar zvxf cmake-3.4.1.tar.gz
[root@centos ~]# cd cmake-3.4.1
[root@centos ~]# ./bootstrap
[root@centos ~]# make && make install
2.3 安裝bison(須要 m4 庫)
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# wget http://ftp.gnu.org/gnu/bison/bison-3.0.4.tar.gz
[root@centos ~]# tar zvxf bison-3.0.4.tar.gz
[root@centos ~]# cd bison-3.0.4
[root@centos ~]# ./configure
[root@centos ~]# make && make install
2.4 安裝jemalloc(須要 bzip2 庫解壓)
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# wget https://github.com/jemalloc/jemalloc/releases/download/4.0.4/jemalloc-4.0.4.tar.bz2
[root@centos ~]# tar xjf jemalloc-4.0.4.tar.bz2
[root@centos ~]# cd jemalloc-4.0.4
[root@centos ~]# ./configure
[root@centos ~]# make && make install
[root@centos ~]# echo '/usr/local/lib' > /etc/ld.so.conf.d/local.conf
[root@centos ~]# ldconfig
2.5 安裝libevent
[root@centos ~]# cd /usr/local/src
[root@centos ~]# wget https://sourceforge.net/projects/levent/files/libevent/libevent-2.0/libevent-2.0.22-stable.tar.gz
[root@centos ~]# tar zvxf libevent-2.0.22-stable.tar.gz
[root@centos ~]# cd libevent-2.0.22-stable
[root@centos ~]# ./configure --prefix=/usr
[root@centos ~]# make && make install
若是libevent的lib目錄不在LD_LIBRARY_PATH裏,可使用如下命令加入
[root@centos ~]# export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr
2.6 建立mysql須要的目錄、配置用戶和用戶組
[root@centos ~]# groupadd mysql
[root@centos ~]# useradd -g mysql mysql -s /sbin/nologin
[root@centos ~]# mkdir -p /data/mysql
[root@centos ~]# chown -R mysql:mysql /data/mysql
2.7 編譯mariadb(須要 cmake ncurses-devel bison 庫)
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# wget https://downloads.mariadb.org/f/mariadb-10.1.9/source/mariadb-10.1.9.tar.gz
[root@centos ~]# tar zvxf mariadb-10.1.9.tar.gz
[root@centos ~]# cd mariadb-10.1.9
[root@centos ~]# cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/opt/mysql -DINSTALL_DOCDIR=share/doc/mariadb-10.1.9 -DINSTALL_DOCREADMEDIR=share/doc/mariadb-10.1.9 -DINSTALL_MANDIR=share/man -DINSTALL_MYSQLSHAREDIR=share/mysql -DINSTALL_MYSQLTESTDIR=share/mysql/test -DINSTALL_PLUGINDIR=lib/mysql/plugin -DINSTALL_SBINDIR=sbin -DINSTALL_SCRIPTDIR=bin -DINSTALL_SQLBENCHDIR=share/mysql/bench -DINSTALL_SUPPORTFILESDIR=share/mysql -DMYSQL_DATADIR=/data/mysql -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DWITH_EXTRA_CHARSETS=complex -DWITH_EMBEDDED_SERVER=ON -DTOKUDB_OK=0
[root@centos ~]# make && make install
2.8 建立軟鏈接
[root@centos ~]# ln -s /opt/mysql/lib/lib* /usr/lib/
[root@centos ~]# ln -s /opt/mysql/bin/mysql /bin
2.9 修改配置文件
[root@centos ~]# cp ./support-files/my-large.cnf /etc/my.cnf
[root@centos ~]# vim /etc/my.cnf
在[client]下添加如下內容
default-character-set = utf8
在[mysqld]下添加如下內容
datadir = /data/mysql
character-set-server = utf8
:wq 保存退出
2.10 初始化數據庫
[root@centos ~]# cd /opt/mysql
[root@centos ~]# ./bin/mysql_install_db --basedir=/opt/mysql --datadir=/data/mysql --user=mysql
[root@centos ~]# ./bin/mysqld_safe --datadir=/data/mysql
確認運行後,按能夠按CTRL+Z結束
[root@centos ~]# ps -ef|grep mysqld
[root@centos ~]# lsof -n | grep jemalloc
設置數據庫ROOT密碼,移除刪除臨時用戶,刪除測試數據庫等
[root@centos ~]# ./bin/mysql_secure_installation
2.11 登陸數據庫,查看數據庫狀態
[root@centos ~]# mysql -u root -p
Mysql [(none)]>status;
Mysql [(none)]>show engines;
Mysql [(none)]>exit;
2.12 設置mysql開機自動啓動服務
[root@centos ~]# vim /etc/systemd/system/mysqld.service
錄入如下內容
[Unit]
Description=MySQL Community Server
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
Alias=mysql.service
[Service]
User=mysql
Group=mysql
# Execute pre and post scripts as root
PermissionsStartOnly=true
# Needed to create system tables etc.
# Start main service
ExecStart=/opt/mysql/bin/mysqld_safe
# Don't signal startup success before a ping works
# Give up if ping don't get an answer
TimeoutSec=30
Restart=always
PrivateTmp=false
:wq 保存
[root@centos ~]# systemctl enable mysqld.service
[root@centos ~]# systemctl list-unit-files|grep enabled|grep mysql
[root@centos ~]# systemctl daemon-reload
2.13 重啓,確認是否已自動啓動服務
[root@centos ~]# shutdown -r now
[root@centos ~]# systemctl start mysqld.service
[root@centos ~]# systemctl status mysqld.service -l
[root@centos ~]# ps -ef|grep mysqld
[root@centos ~]# lsof -n | grep jemalloc
2.14 增長遠程訪問用戶,而且打開防火牆3306端口(不遠程鏈接數據,可忽略)
[root@centos ~]# mysql -u root -p
Mysql [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
Mysql [(none)]> FLUSH PRIVILEGES;
Mysql [(none)]> exit;
(root是用戶名,%是主機名或IP地址,這裏的%表明任意主機或IP地址,也可指定惟一的IP地址;密碼是MyPassword )
2.15 防火牆添加3306端口(不遠程鏈接數據,可忽略)
[root@centos ~]# iptables -L|grep ACCEPT
[root@centos ~]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
[root@centos ~]# firewall-cmd --reload
[root@centos ~]# iptables -L|grep ACCEPT
3.編譯安裝Nginx
3.1安裝依賴
[root@centos ~]# yum install zlib-devel openssl-devel -y
3.2 安裝Pcre
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# wget http://sourceforge.net/projects/pcre/files/pcre/8.37/pcre-8.37.tar.gz
[root@centos ~]# tar zvxf pcre-8.37.tar.gz
[root@centos ~]# cd pcre-8.37
[root@centos ~]# ./configure
[root@centos ~]# make && make install
3.3 安裝zlib
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# wget http://zlib.net/zlib-1.2.8.tar.gz
[root@centos ~]# tar zvxf zlib-1.2.8.tar.gz
[root@centos ~]# cd zlib-1.2.8
[root@centos ~]# ./configure
[root@centos ~]# make && make install
3.4 安裝openssl (依賴zlib庫)
[root@centos ~]# openssl version
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# wget https://www.openssl.org/source/openssl-1.0.2e.tar.gz
[root@centos ~]# tar zvxf openssl-1.0.2e.tar.gz
[root@centos ~]# cd openssl-1.0.2e
[root@centos ~]# ./config shared zlib
[root@centos ~]# make && make install
#更新軟鏈接 (若是編譯時./config shared zlib --prefix=/usr,指定/usr目錄時便可忽略如下步驟 )
[root@centos ~]# mv /usr/bin/openssl /usr/bin/openssl_bak
[root@centos ~]# mv /usr/include/openssl/ /usr/include/openssl_bak
[root@centos ~]# ln -s /usr/local/ssl/bin/openssl /usr/bin/openssl
[root@centos ~]# ln -s /usr/local/ssl/include/openssl/ /usr/include/openssl
[root@centos ~]# echo "/usr/local/ssl/lib" >> /etc/ld.so.conf
#查看最新版本
[root@centos ~]# ldconfig -v | grep ssl
[root@centos ~]# openssl version -a
3.6 建立www用戶和組,建立www虛擬主機使用的目錄,以及Nginx使用的日誌目錄,而且賦予他們適當的權限
[root@centos ~]# groupadd www
[root@centos ~]# useradd -g www www -s /sbin/nologin
[root@centos ~]# mkdir -p /data/www/web
[root@centos ~]# chmod +w /data/www/web
[root@centos ~]# chown -R www:www /data/www/web
***若是無法建立用戶,須要檢查SELinux狀態是否關閉
3.7 安裝nginx
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# wget http://nginx.org/download/nginx-1.9.9.tar.gz
[root@centos ~]# tar zvxf nginx-1.9.9.tar.gz
[root@centos ~]# cd nginx-1.9.9
[root@centos ~]# ./configure --prefix=/opt/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-openssl=/usr/local/src/openssl-1.0.2e --with-zlib=/usr/local/src/zlib-1.2.8 --with-pcre=/usr/local/src/pcre-8.37 --with-ld-opt="-ljemalloc" --with-http_v2_module
[root@centos ~]# make && make install
3.8 配置nginx,以支持靜態網頁訪問
修改 nginx.conf
[root@centos ~]# vim /opt/nginx/conf/nginx.conf
修改前面幾行爲:
user www www;
worker_processes auto;
error_log logs/error.log crit;
pid logs/nginx.pid;
events{
use epoll;
worker_connections 65535;
}
找到,並修改 root 行的內容
location / {
root /data/www/web;
index index.html index.htm;
}
:wq 保存退出
* 若是須要支持http2,參考如下設置(須要https證書,而且OpenSSL 1.0.2+)
server {
listen 443 ssl http2;
ssl_certificate server.crt;
ssl_certificate_key server.key;
...
}
* 完成後,能夠在瀏覽器中,打開此網站,查看是否已經支持http2
chrome://net-internals/#http2
3.9 創建測試首頁
[root@centos ~]# vim /data/www/web/index.html
<html>
<head><title>nginx index.html</title></head>
<body>
<h1>index.html</h1>
</body>
</html>
保存,退出
3.10 測試和運行
[root@centos ~]# cd /opt/nginx
[root@centos ~]# ldconfig
[root@centos ~]# ./sbin/nginx -c /opt/nginx/conf/nginx.conf -t
若是顯示下面信息,即表示配置沒問題
nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx/conf/nginx.conf test is successful
查看jemalloc是否生效,須要先啓動nginx
[root@centos ~]# ./sbin/nginx -c /opt/nginx/conf/nginx.conf
[root@centos ~]# lsof -n | grep jemalloc
ginx 2346 root mem REG 253,1 1824470 51571788 /usr/local/lib/libjemalloc.so.1
nginx 2347 www mem REG 253,1 1824470 51571788 /usr/local/lib/libjemalloc.so.1
nginx 2348 www mem REG 253,1 1824470 51571788 /usr/local/lib/libjemalloc.so.1
nginx 2349 www mem REG 253,1 1824470 51571788 /usr/local/lib/libjemalloc.so.1
nginx 2350 www mem REG 253,1 1824470 51571788 /usr/local/lib/libjemalloc.so.1
3.11 防火牆添加80端口
[root@centos ~]# iptables -L|grep ACCEPT
[root@centos ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent
[root@centos ~]# firewall-cmd --reload
[root@centos ~]# iptables -L|grep ACCEPT
3.12 瀏覽器打開
http://192.168.1.117
顯示出歡迎內容,則表示成功
3.13 做爲服務,開機後啓動
[root@centos ~]# vim /etc/systemd/system/nginx.service
增長如下內容
[Unit]
Description=The nginx HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/opt/nginx/logs/nginx.pid
ExecStartPre=/opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf -t
ExecStart=/opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
:wq 保存退出
[root@centos ~]# systemctl enable nginx.service
[root@centos ~]# systemctl list-unit-files|grep enabled|grep nginx
3.14 啓動服務
[root@centos ~]# ./sbin/nginx -s stop
[root@centos ~]# systemctl daemon-reload
[root@centos ~]# systemctl start nginx.service
[root@centos ~]# systemctl status nginx.service -l
[root@centos ~]# ps -ef|grep nginx
[root@centos ~]# lsof -n | grep jemalloc
4 安裝PHP
4.1 更新依賴
[root@centos ~]# yum install autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers libXpm* gcc gcc-c++ -y
4.2 安裝libiconv (增強系統對支持字符編碼轉換的功能)
[root@centos ~]# cd /usr/local/src
[root@centos ~]# wget http://ftp.gnu.org/gnu/libiconv/libiconv-1.14.tar.gz
[root@centos ~]# tar zvxf libiconv-1.14.tar.gz
[root@centos ~]# cd libiconv-1.14
[root@centos ~]# ./configure --prefix=/usr/local
[root@centos ~]# cd srclib
[root@centos ~]# sed -i -e '/gets is a security/d' ./stdio.in.h
[root@centos ~]# cd ..
[root@centos ~]# make && make install
[root@centos ~]# ln -sf /usr/local/lib/libiconv.so.2 /usr/lib64/
[root@centos ~]# ldconfig
4.4 安裝libmcrypt,libltdl(加密算法庫,PHP擴展mcrypt功能對此庫有依耐關係,要使用mcrypt必須先安裝此庫)
[root@centos ~]# cd /usr/local/src
[root@centos ~]# wget http://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
[root@centos ~]# tar zvxf libmcrypt-2.5.8.tar.gz
[root@centos ~]# cd libmcrypt-2.5.8
[root@centos ~]# ./configure
[root@centos ~]# make && make install
[root@centos ~]# cd libltdl/
[root@centos ~]# ./configure --enable-ltdl-install
[root@centos ~]# make && make install
[root@centos ~]# ln -sf /usr/local/lib/libmcrypt.* /usr/lib64/
[root@centos ~]# ln -sf /usr/local/bin/libmcrypt-config /usr/lib64/
[root@centos ~]# ldconfig
4.5 安裝mhash (hash加密算法庫)
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# wget http://sourceforge.net/projects/mhash/files/mhash/0.9.9.9/mhash-0.9.9.9.tar.gz
[root@centos ~]# tar zvxf mhash-0.9.9.9.tar.gz
[root@centos ~]# cd mhash-0.9.9.9
[root@centos ~]# ./configure
[root@centos ~]# make && make install
[root@centos ~]# ln -sf /usr/local/lib/libmhash.* /usr/lib64/
[root@centos ~]# ldconfig
4.6 安裝mcrypt
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# wget http://sourceforge.net/projects/mcrypt/files/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz
[root@centos ~]# tar zvxf mcrypt-2.6.8.tar.gz
[root@centos ~]# cd mcrypt-2.6.8
[root@centos ~]# ./configure
[root@centos ~]# make && make install
4.7 安裝php (已經安裝mariadb,nginx,ldap)
4.7.1 建立mysql軟鏈接、ldap軟鏈接
[root@centos ~]# mkdir -p /opt/mysql/include/mysql
[root@centos ~]# ln -s /opt/mysql/include/* /opt/mysql/include/mysql/
[root@centos ~]# ln -s /usr/lib64/libldap* /usr/lib
[root@centos ~]# ln -s /usr/lib64/liblber* /usr/lib
4.7.2 安裝
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# wget http://am1.php.net/get/php-5.5.30.tar.gz
[root@centos ~]# tar zvxf php-5.5.30.tar.gz
[root@centos ~]# cd php-5.5.30
[root@centos ~]# ./configure \
--prefix=/opt/php \
--with-config-file-path=/opt/php/etc \
--with-mysqli=/opt/mysql/bin/mysql_config \
--with-openssl \
--with-iconv-dir=/usr/local \
--with-libxml-dir=/usr \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-curl \
--with-mhash \
--with-ldap \
--with-ldap-sasl \
--with-mcrypt \
--with-gd \
--with-xmlrpc \
--with-libdir=/lib/ \
--with-kerberos \
--with-pcre-regex \
--with-pdo-mysql \
--with-zlib-dir \
--with-bz2 \
--with-gettext \
--disable-rpath \
--enable-xml \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--enable-mbregex \
--enable-fpm \
--enable-mbstring \
--enable-gd-native-ttf \
--enable-pcntl \
--enable-sockets \
--enable-zip \
--enable-soap \
--enable-opcache \
--enable-calendar \
--enable-ctype \
--enable-exif \
--enable-session \
--enable-ftp \
[root@centos ~]# make ZEND_EXTRA_LIBS='-liconv'
[root@centos ~]# make install
4.7.3 複製配置文件
[root@centos ~]# cp php.ini-production /opt/php/etc/php.ini
4.7.4 修改php配置文件,支持ZendOpcache
[root@centos ~]# ll /opt/php/lib/php/extensions/no-debug-non-zts-20121212
[root@centos ~]# vim /opt/php/etc/php.ini
在文件中搜索; extension_dir = "./" ,並在下面添加如下內容(若是extension_dir已存在,只添加後面的內容)
extension_dir = "/opt/php/lib/php/extensions/no-debug-non-zts-20121212/"
zend_extension="opcache.so"
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable=1
opcache.enable_cli=1
opcache.huge_code_pages=1
opcache.file_cache=/tmp
:wq 保存退出
開啓系統HugePages
[root@centos ~]# sysctl vm.nr_hugepages=512
[root@centos ~]# cat /proc/meminfo | grep Huge
AnonHugePages: 106496 kB
HugePages_Total: 512
HugePages_Free: 504
HugePages_Rsvd: 27
HugePages_Surp: 0
Hugepagesize: 2048 kB
4.7.5 安裝xdebug擴展(調試PHP用,不須要時可忽略)
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# wget http://xdebug.org/files/xdebug-2.4.0rc2.tgz
[root@centos ~]# tar zvxf xdebug-2.4.0rc2.tgz
[root@centos ~]# cd xdebug-2.4.0RC2
[root@centos ~]# /opt/php/bin/phpize
[root@centos ~]# ./configure --enable-xdebug --with-php-config=/opt/php/bin/php-config
[root@centos ~]# make && make install
修改php配置文件,支持xdebug
[root@centos ~]# vim /opt/php/etc/php.ini
在文件中搜索; extension_dir = "./" ,並在下面添加如下內容(若是extension_dir已存在,只添加後面的一行)
extension_dir = "/opt/php/lib/php/extensions/no-debug-non-zts-20121212/"
[xdebug]
zend_extension = "xdebug.so"
xdebug.remote_enable=1
xdebug.remote_connect_back=on
xdebug.remote_port=8080
xdebug.idekey=PHPSTORM
xdebug.remote_autostart=1
:wq 保存退出
4.7.6 安裝pdo_mysql擴展(已經安裝PHP)
[root@centos ~]# cd /usr/local/src/php-5.5.30/ext/pdo_mysql/
[root@centos ~]# /opt/php/bin/phpize
[root@centos ~]# ./configure --with-php-config=/opt/php/bin/php-config --with-pdo-mysql=/opt/mysql
[root@centos ~]# make && make install
修改php配置文件,支持pdo_mysql
[root@centos ~]# vim /opt/php/etc/php.ini
在文件中搜索; extension_dir = "./" ,並在下面添加如下內容(若是extension_dir已存在,只添加後面的一行)
extension_dir = "/opt/php/lib/php/extensions/no-debug-non-zts-20121212/"
extension = "pdo_mysql.so"
:wq 保存退出
4.7.7 安裝memcache擴展(已經安裝PHP,依賴libmemcached)
[root@centos ~]# cd /usr/local/src
[root@centos ~]# wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz
[root@centos ~]# tar zvxf libmemcached-1.0.18.tar.gz
[root@centos ~]# cd libmemcached-1.0.18
[root@centos ~]# ./configure --with-memcached --prefix=/opt/libmemcached
[root@centos ~]# make && make install
[root@centos ~]# cd /usr/local/src
[root@centos ~]# wget http://pecl.php.net/get/memcached-2.2.0.tgz
[root@centos ~]# tar zvxf memcached-2.2.0.tgz
[root@centos ~]# cd memcached-2.2.0
[root@centos ~]# /opt/php/bin/phpize
[root@centos ~]# ./configure --with-php-config=/opt/php/bin/php-config --with-libmemcached-dir=/opt/libmemcached
[root@centos ~]# make && make install
[root@centos ~]# ll /opt/php/lib/php/extensions/no-debug-non-zts-20121212/
修改php配置文件,支持memcache
[root@centos ~]# vim /opt/php/etc/php.ini
在文件中搜索; extension_dir = "./" ,並在下面添加如下內容(若是extension_dir已存在,只添加後面的一行)
extension_dir = "/opt/php/lib/php/extensions/no-debug-non-zts-20121212/"
extension = "memcached.so"
4.7.8 安裝php-fpm
[root@centos ~]# cp /opt/php/etc/php-fpm.conf.default /opt/php/etc/php-fpm.conf
[root@centos ~]# vim /opt/php/etc/php-fpm.conf
修改內容,而且讓其它生效
[global]
pid = run/php-fpm.pid
error_log = log/php-fpm.log
emergency_restart_threshold = 10
emergency_restart_interval = 1m
process_control_timeout = 5s
user = www
group = www
pm.max_children = 35
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
保存,退出
# php-fpm.conf 重要參數詳解
# pid = run/php-fpm.pid
# pid設置,默認在安裝目錄中的var/run/php-fpm.pid,建議開啓
# error_log = log/php-fpm.log
# 錯誤日誌,默認在安裝目錄中的var/log/php-fpm.log
# log_level = notice
# 錯誤級別. 可用級別爲: alert(必須當即處理), error(錯誤狀況), warning(警告狀況), notice(通常重要信息), debug(調試信息). 默認: notice.
# emergency_restart_threshold = 60
# emergency_restart_interval = 60s
# 表示在emergency_restart_interval所設值內出現SIGSEGV或者SIGBUS錯誤的php-cgi進程數若是超過 emergency_restart_threshold個,php-fpm就會優雅重啓。這兩個選項通常保持默認值。
# process_control_timeout = 0
# 設置子進程接受主進程複用信號的超時時間. 可用單位: s(秒), m(分), h(小時), 或者 d(天) 默認單位: s(秒). 默認值: 0.
# daemonize = yes
# 後臺執行fpm,默認值爲yes,若是爲了調試能夠改成no。在FPM中,可使用不一樣的設置來運行多個進程池。 這些設置能夠針對每一個進程池單獨設置。
# listen = 127.0.0.1:9000
# fpm監聽端口,即nginx中php處理的地址,通常默認值便可。可用格式爲: 'ip:port', 'port', '/path/to/unix/socket'. 每一個進程池都須要設置.
# listen.backlog = -1
# backlog數,-1表示無限制,由操做系統決定,此行註釋掉就行。backlog含義參考:http://www.3gyou.cc/?p=41
# listen.allowed_clients = 127.0.0.1
# 容許訪問FastCGI進程的IP,設置any爲不限制IP,若是要設置其餘主機的nginx也能訪問這臺FPM進程,listen處要設置成本地可被訪問的IP。默認值是any。每一個地址是用逗號分隔. 若是沒有設置或者爲空,則容許任何服務器請求鏈接
# listen.owner = www
# listen.group = www
# listen.mode = 0666
# unix socket設置選項,若是使用tcp方式訪問,這裏註釋便可。
# user = www
# group = www
# 啓動進程的賬戶和組
# pm = dynamic #對於專用服務器,pm能夠設置爲static。
# 如何控制子進程,選項有static和dynamic。若是選擇static,則由pm.max_children指定固定的子進程數。若是選擇dynamic,則由下開參數決定:
# pm.max_children #,子進程最大數
# pm.start_servers #,啓動時的進程數
# pm.min_spare_servers #,保證空閒進程數最小值,若是空閒進程小於此值,則建立新的子進程
# pm.max_spare_servers #,保證空閒進程數最大值,若是空閒進程大於此值,此進行清理
# pm.max_requests = 1000
# 設置每一個子進程重生以前服務的請求數. 對於可能存在內存泄漏的第三方模塊來講是很是有用的. 若是設置爲 '0' 則一直接受請求. 等同於 PHP_FCGI_MAX_REQUESTS 環境變量. 默認值: 0.
# pm.status_path = /status
# FPM狀態頁面的網址. 若是沒有設置, 則沒法訪問狀態頁面. 默認值: none. munin監控會使用到
# ping.path = /ping
# FPM監控頁面的ping網址. 若是沒有設置, 則沒法訪問ping頁面. 該頁面用於外部檢測FPM是否存活而且能夠響應請求. 請注意必須以斜線開頭 (/)。
# ping.response = pong
# 用於定義ping請求的返回相應. 返回爲 HTTP 200 的 text/plain 格式文本. 默認值: pong.
# request_terminate_timeout = 0
# 設置單個請求的超時停止時間. 該選項可能會對php.ini設置中的'max_execution_time'由於某些特殊緣由沒有停止運行的腳本有用. 設置爲 '0' 表示 'Off'.當常常出現502錯誤時能夠嘗試更改此選項。
# request_slowlog_timeout = 10s
# 當一個請求該設置的超時時間後,就會將對應的PHP調用堆棧信息完整寫入到慢日誌中. 設置爲 '0' 表示 'Off'
# slowlog = log/$pool.log.slow
# 慢請求的記錄日誌,配合request_slowlog_timeout使用
# rlimit_files = 1024
# 設置文件打開描述符的rlimit限制. 默認值: 系統定義值默承認打開句柄是1024,可以使用 ulimit -n查看,ulimit -n 2048修改。
# rlimit_core = 0
# 設置核心rlimit最大限制值. 可用值: 'unlimited' 、0或者正整數. 默認值: 系統定義值.
# chroot =
# 啓動時的Chroot目錄. 所定義的目錄須要是絕對路徑. 若是沒有設置, 則chroot不被使用.
# chdir =
# 設置啓動目錄,啓動時會自動Chdir到該目錄. 所定義的目錄須要是絕對路徑. 默認值: 當前目錄,或者/目錄(chroot時)
# catch_workers_output = yes
# 重定向運行過程當中的stdout和stderr到主要的錯誤日誌文件中. 若是沒有設置, stdout 和 stderr 將會根據FastCGI的規則被重定向到 /dev/null . 默認值: 空.
4.7.8 配置nginx,支持php
[root@centos ~]# mkdir -p /opt/nginx/conf/vhosts
[root@centos ~]# vim /opt/nginx/conf/nginx.conf
刪除原來內容,替換成如下內容:
user www www;
worker_processes auto;
error_log logs/error.log crit;
pid logs/nginx.pid;
events {
use epoll;
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
# log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
# access_log logs/access.log main;
sendfile on;
# tcp_nopush on;
# keepalive_timeout 0;
keepalive_timeout 65;
# gzip on;
include /opt/nginx/conf/vhosts/*.conf;
}
:wq 保存退出
建立網站配置文件(網站thinkphp,網站目錄/data/www/web)
[root@centos ~]# vim /opt/nginx/conf/vhosts/web.conf
添加如下內容
server {
listen 80;
server_name 192.168.1.117;
root /data/www/web;
location / {
index index.html index.php;
# for bowers thinkphp without /index.php path
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php/$1 last;
break;
}
}
location ~ \.php {
root /data/www/web;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www/web$fastcgi_script_name;
include fastcgi_params;
# for thinkphp pathinfo mode
set $path_info "";
set $real_script_name $fastcgi_script_name;
if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
set $real_script_name $1;
set $path_info $2;
}
fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
fastcgi_param SCRIPT_NAME $real_script_name;
fastcgi_param PATH_INFO $path_info;
}
}
:wq 保存退出
4.7.9 將php-fpm服務加到開機啓動服務
[root@centos ~]# cp /usr/local/src/php-5.5.30/sapi/fpm/php-fpm.service /etc/systemd/system/
[root@centos ~]# vim /etc/systemd/system/php-fpm.service
刪除原來內容,替換成如下內容
[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target
[Service]
Type=simple
PIDFile=/opt/php/var/run/php-fpm.pid
ExecStart=/opt/php/sbin/php-fpm --nodaemonize --fpm-config /opt/php/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
[Install]
WantedBy=multi-user.target
保存,退出
[root@centos ~]# systemctl enable php-fpm.service
[root@centos ~]# systemctl list-unit-files|grep enabled|grep php-fpm
[root@centos ~]# systemctl daemon-reload
[root@centos ~]# systemctl start php-fpm.service
[root@centos ~]# systemctl status php-fpm.service -l
4.7.10 編寫測試頁面
[root@centos ~]# mkdir -p /data/www/web
[root@centos ~]# vim /data/www/web/index.php
輸入如下內容
<html>
<head><title>hello php</title></head>
<body>
<?php phpinfo();?>
</body>
</html>
:wq 保存退出
[root@centos ~]# systemctl restart nginx
[root@centos ~]# systemctl restart php-fpm
瀏覽器訪問:http://192.168.1.117/index.php
5 安裝memcached服務
5.1 安裝memcached (須要libevent庫)
[root@centos ~]# cd /usr/local/src
[root@centos ~]# wget http://www.memcached.org/files/memcached-1.4.25.tar.gz
[root@centos ~]# tar zvxf memcached-1.4.25.tar.gz
[root@centos ~]# cd memcached-1.4.25
[root@centos ~]# ./configure --prefix=/opt/memcached --with-libevent=/usr
[root@centos ~]# make && make install
5.2 設置用戶、運行參數,以及開機啓動
[root@centos ~]# groupadd memcached
[root@centos ~]# useradd -g memcached memcached -s /sbin/nologin
[root@centos ~]# vim /etc/sysconfig/memcached
添加如下內容
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""
:wq 保存退出
[root@centos ~]# vim /etc/systemd/system/memcached.service
添加如下內容
[Unit]
Description=Memcached
Before=httpd.service
After=network.target
[Service]
Type=simple
EnvironmentFile=-/etc/sysconfig/memcached
ExecStart=/opt/memcached/bin/memcached -u $USER -p $PORT -m $CACHESIZE -c $MAXCONN $OPTIONS
[Install]
WantedBy=multi-user.target
:wq 保存退出
[root@centos ~]# systemctl enable memcached.service
[root@centos ~]# systemctl list-unit-files|grep enabled|grep memcached
[root@centos ~]# systemctl daemon-reload
[root@centos ~]# systemctl start memcached.service
[root@centos ~]# systemctl status memcached.service -l
5.3 防火牆添加12121端口(若是不須要外部訪問,可忽略)
[root@centos ~]# iptables -L|grep ACCEPT
[root@centos ~]# firewall-cmd --zone=public --add-port=11211/tcp --permanent
[root@centos ~]# firewall-cmd --reload
[root@centos ~]# iptables -L|grep ACCEPT
5.4 測試PHP支持(PHP須要安裝memcached擴展)
[root@centos ~]# vim /data/www/web/m.php
添加如下內容
<?php
$mem = new Memcached;
$mem->addServer('127.0.0.1',11211);
$mem->set('test',"hello memcached");
$val=$mem->get('test');
echo $val;
?>
:wq 保存退出
瀏覽器訪問:http://192.168.1.117/m.php