環境:php
系統硬件:vmware vsphere (CPU:2*4核,內存2G,雙網卡)html
系統版本:CentOS-7-x86_64-Minimal-1611.isopython
安裝步驟:mysql
1.準備linux
1.0 查看硬件信息nginx
查看物理cpu個數
[root@centos ~]# grep 'physical id' /proc/cpuinfo | sort -u | wc -llaravel
查看核心數量
[root@centos ~]# grep 'core id' /proc/cpuinfo | sort -u | wc -lc++
查看線程數
[root@centos ~]# grep 'processor' /proc/cpuinfo | sort -u | wc -lgit
查看硬盤空間佔用github
[root@centos ~]# df -h
查看目錄空間佔用
[root@centos ~]# du -msh /usr/local/src
查看物理內存,以及swap使用狀況
[root@centos ~]# free -h
******************************************
把 /usr/local/src 目錄,轉到 /data 下
[root@centos ~]# mkdir -p /data
[root@centos ~]# mv /usr/local/src /data
[root@centos ~]# ln -s /data/src /usr/local/src
******************************************
1.1 主機名設置
當前主機名查看
[root@centos ~]# hostname
主機名設置
[root@centos ~]# hostnamectl --static set-hostname tCentos
[root@centos ~]# systemctl restart network
[root@centos ~]# hostname
tCentos
1.2 設置靜態IP、DNS地址(網絡設備名稱有可能不同,這裏是eno16780032,如使用DHCP獲取動態IP,可忽略)
[root@centos ~]# vi /etc/sysconfig/network-scripts/ifcfg-eno16780032
找到BOOTPROTO,而且修改(設爲靜態網址)
BOOTPROTO="static"
在最後添加三行內容(添加本機IP,子網掩碼,網關)
IPADDR="192.168.1.10"
NETMASK="255.255.255.0"
GATEWAY="192.168.1.1"
:wq 保存退出
[root@centos ~]# shutdonw -r now
[root@centos ~]# vi /etc/resolv.conf
添加如下幾個DNS地址
nameserver 114.114.114.114
nameserver 192.168.1.1
nameserver 8.8.8.8
:wq 保存退出
[root@centos ~]# systemctl restart network
[root@centos ~]# ip addr|grep inet
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
inet 192.168.1.10/24 brd 192.168.1.255 scope global eno16780032
inet6 fe80::250:56ff:feb0:30f2/64 scope link
1.3 更新時間,設置定時同步時間
[root@centos ~]# yum install -y ntpdate
[root@centos ~]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
手動設置系統時間(測試用)
[root@centos ~]# date -s "2000-1-1"
[root@centos ~]# /usr/sbin/ntpdate us.pool.ntp.org
查看當前系統時間
[root@centos ~]# date
設置定時任務,自動執行
[root@centos ~]# mkdir -p /data/crond
[root@centos ~]# crontab -e
添加如下內容 (天天 02:00同步一次,而且日誌記錄到 /data/crond/ntpdate.log)
00 02 * * * /usr/sbin/ntpdate time.nist.gov 1>>/data/crond/ntpdate.log 2>&1
:wq 保存退出
1.4 安裝基本軟件包
[root@centos ~]# yum install vim wget lsof gcc gcc-c++ bzip2 firewalld openssl-devel mlocate -y
配置Vim顯示格式
[root@centos ~]# vim /etc/vimrc
在末尾添加如下內容
set nocompatible
set number
filetype on
set history=1000
set background=dark
syntax on
set autoindent
set smartindent
set tabstop=2
set shiftwidth=2
set showmatch
set guioptions-=T
set vb t_vb=
set ruler
set nohls
set incsearch
if has("vms")
set nobackup
else
set backup
endif
:wq 保存退出
VIM格式化使用方法(打開文件後,輸入如下命令,第二個G是shift+g)
gg=G
1.5 打開selinux ( centos 7.3某個版本設置爲enforcing會致使無法重啓)
[root@centos ~]# vim /etc/selinux/config
找到這一行,去掉前面的#註釋
SELINUX=enforcing
保存,退出
重啓後,查詢是否關閉(顯示Enforcing表示啓用,Disabled則表示關閉)
[root@centos ~]# shutdown -r now
[root@centos ~]# getenforce
Enforcing
1.6 更新系統,顯示系統版本(使用阿里雲源)
[root@centos ~]# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
[root@centos ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@centos ~]# yum clean all
[root@centos ~]# yum makecache
[root@centos ~]# yum update -y
更新完成後重啓,查看內核版本
[root@centos ~]# shutdown -r now
[root@centos ~]# cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)
[root@centos ~]# uname -r
3.10.0-514.16.1.el7.x86_64
查看當前全部內核版本
[root@centos ~]# rpm -qa | grep kernel-devel
kernel-devel-3.10.0-514.16.1.el7.x86_64
kernel-devel-3.10.0-514.10.2.el7.x86_64
清除多餘內核版本 (有須要能夠刪除)
[root@centos ~]# yum remove kernel-devel-3.10.0-514.10.2.el7.x86_64
更新完成後重啓,在啓動菜單選擇內核版本時能夠查看到刪除結果
[root@centos ~]# shutdown -r now
1.7 設置PUTTY遠程登陸時,不使用密碼,使用密鑰文件登陸(如不須要,可忽略)
1.7.1 服務器上建立目錄
[root@centos ~]# mkdir -p /root/.ssh
1.7.2 在"客戶機"生成對稱密鑰,把客戶機上的公鑰複製到服務器(公鑰文件:id_rsa.pub)
[root@centos ~] ssh-keygen -t rsa
根據提示操做,生成公鑰
上傳到服務器指定目錄
[root@centos ~] scp id_rsa.pub root@192.168.1.10/root/.ssh
*** 或使用軟件遠程複製id_rsa.pub到服務器/root/.ssh中。
1.7.3 查看服務器上,公鑰是否已經存在
[root@centos ~]# cd /root/.ssh
[root@centos ~]# ll
-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
設置目錄和文件讀取權限
[root@centos ~]# chmod 700 /root/.ssh
[root@centos ~]# chmod 600 /root/.ssh/authorized_keys
1.7.4 設置sshd配置文件
[root@centos ~]# vim /etc/ssh/sshd_config
找到GSSAPICleanupCredentials,而且修改成如下內容
GSSAPICleanupCredentials yes
:wq 保存退出
重啓sshd服務,讓其生效
[root@centos ~]# systemctl restart sshd
1.7.5 客戶端設置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.7.6 設置完成後,便可以遠程鏈接到服務器
打開軟件 PuTTY
點擊Session,在"Default Settings"下,找到以前已經保存的Session,雙擊打開鏈接
若是顯示 Authenticating with public key "xxxxx-xxxx"時,即表未成功
1.8 設置新用戶,而且使用密碼和證書雙重認證遠程登陸。同時禁止root遠程登陸 (如不須要,可忽略)
1.8.1 root登陸後,修改root密碼 (安全建議:密碼爲15位,大小字母+數字+特殊字符)
[root@centos ~]# passwd
1.8.2 添加新用戶,而且設置密碼
[root@centos ~]# adduser vicowong
[root@centos ~]# passwd vicowong
1.8.3 建立目錄,複製密鑰相關文件到用戶目錄,而且設置權限
[root@centos ~]# mkdir /home/vicowong/.ssh -p
[root@centos ~]# cp /root/.ssh/authorized_keys /home/vicowong/.ssh
[root@centos ~]# chmod 700 /home/vicowong/.ssh
[root@centos ~]# chmod 600 /home/vicowong/.ssh/authorized_keys
[root@centos ~]# chown vicowong:vicowong /home/vicowong/.ssh
[root@centos ~]# chown vicowong:vicowong /home/vicowong/.ssh/authorized_keys
1.8.4 設置防火牆,設置遠程鏈接端口(這裏是26322)
[root@centos ~]# systemctl enable firewalld
[root@centos ~]# systemctl start firewalld
[root@centos ~]# systemctl status firewalld
[root@centos ~]# firewall-cmd --zone=public --add-port=26322/tcp --permanent
[root@centos ~]# firewall-cmd --reload
[root@centos ~]# iptables -L|grep ACCEPT
1.8.5 安裝semanage(用於設置selinux策略)
[root@centos ~]# yum install -y policycoreutils-python selinux-policy selinux-policy-targeted
查看當前 selinux 是否啓用 即 Enforcing 狀態 (不然有可能設置 selinux 策略不成功)
[root@centos ~]# getenforce
查看當前 selinux 關於遠程ssh鏈接端口的設置
[root@centos ~]# semanage port -l | grep ssh
ssh_port_t tcp 22
添加新端口
[root@centos ~]# semanage port -a -t ssh_port_t -p tcp 26322
1.8.6 設置sshd配置文件
[root@centos ~]# vim /etc/ssh/sshd_config
找到如下內容,而且進行修改
Port 26322
Protocol 2
ServerKeyBits 1024
PermitRootLogin no
AllowUsers vicowong
StrictModes yes
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
PermitEmptyPasswords no
PasswordAuthentication yes
AuthenticationMethods publickey,password
X11Forwarding no
MaxStartups 10:30:60
:wq 保存退出
重啓sshd服務,讓其生效
[root@centos ~]# systemctl restart sshd
1.8.7 使用新用戶登陸(從新打開一個新終端,原來的終端先不關,避免因設置不當致使無法鏈接遠程)
打開軟件 PuTTY,點擊以前保存的Sessions,點擊Load讀取以前的配置
在Port框輸入端口(當前帳號爲26322)
點擊Connection下的DATA,在Auto-login username中輸入登陸帳號(當前帳號爲vicowong)
點擊Session 點擊Save。保存當前修改。
點擊Open,打開終端。
1.8.8 設置後,必須遠程將進行密碼和證書雙重認證。
遠程登陸會以vicowong這個帳號進行登陸。安裝維護須要root權限時,可使用su實現
[root@centos ~]# su root
1.8.9 開機自動禁止被ping (設置爲 1:禁止,0:容許)
[root@centos ~]# chmod +x /etc/rc.d/rc.local
[root@centos ~]# vim /etc/rc.d/rc.local
打開後,在最後增長一行
echo 1 >/proc/sys/net/ipv4/icmp_echo_ignore_all
:wq 保存退出
[root@centos ~]# cat /proc/sys/net/ipv4/icmp_echo_ignore_all
1
1.9 升級 gcc (更新過程時間很長,根據須要更新)
全部源碼包統一放到指定目錄(可事前下載直接經過FTP上傳)
[root@centos ~]# ll /usr/local/src
1.9.1 下載 gcc,解壓
[root@centos ~]# cd /usr/local/src
[root@centos ~]# wget http://gcc.parentingamerica.com/releases/gcc-6.3.0/gcc-6.3.0.tar.gz
[root@centos ~]# tar zvxf gcc-6.3.0.tar.gz
[root@centos ~]# cd gcc-6.3.0
1.9.2 下載依賴文件,而且刷新
[root@centos ~]# ./contrib/download_prerequisites
[root@centos ~]# ldconfig
1.9.3 建立編輯目錄
[root@centos ~]# mkdir gcc-build-6.3.0
[root@centos ~]# cd /usr/local/src/gcc-6.3.0/gcc-build-6.3.0
1.9.4 編譯
[root@centos ~]# ../configure --enable-languages=c,c++ --disable-multilib --enable-checking=release --prefix=/usr
# --prefix=/usr 指定安裝目錄(覆蓋原來的目錄)
# --enable-languages,說明你要讓你的gcc支持那些語言
# --disable-multilib不生成編譯爲其餘平臺可執行代碼的交叉編譯器
# --disable-checking生成的編譯器在編譯過程當中不作額外檢查,也可使用
# --enable-checking=xxx來增長一些檢查
1.9.5 編譯、安裝
[root@centos ~]# make && make install
文件更名,避免運行 ldconfig 命令出現如下錯誤 「
ldconfig: /lib64/libstdc++.so.6.0.22-gdb.py is not an ELF file - it has the wrong magic bytes at the start.」
[root@centos ~]# mv /usr/lib64/libstdc++.so.6.0.22-gdb.py /usr/lib64/bak_libstdc++.so.6.0.22-gdb.py
[root@centos ~]# ldconfig
1.9.6 重啓,查看版本,檢查是否成功更新
[root@centos ~]# shutdown -r now
[root@centos ~]# gcc --version
[root@centos ~]# g++ --version
1.10 安裝cmake
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# wget https://cmake.org/files/v3.8/cmake-3.8.1.tar.gz
[root@centos ~]# tar zvxf cmake-3.8.1.tar.gz
[root@centos ~]# cd cmake-3.8.1
[root@centos ~]# ./bootstrap && make && make install
1.11 更新安裝openssl (依賴zlib庫)
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# wget http://zlib.net/zlib-1.2.11.tar.gz
[root@centos ~]# tar zvxf zlib-1.2.11.tar.gz
[root@centos ~]# cd zlib-1.2.11
[root@centos ~]# ./configure && make && make install
[root@centos ~]# openssl version
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# wget https://www.openssl.org/source/openssl-1.1.0e.tar.gz
[root@centos ~]# tar zvxf openssl-1.1.0e.tar.gz
[root@centos ~]# cd openssl-1.1.0e
[root@centos ~]# ./config shared zlib --prefix=/usr && make && make install
#更新軟鏈接 (若是編譯時沒有指定-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
2.安裝mariadb
2.1 安裝依賴
[root@centos ~]# yum install ncurses-devel zlib-devel openssl-devel bzip2 m4 -y
2.2 安裝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 && make && make install
2.3 安裝jemalloc(須要 bzip2 庫解壓)
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# wget https://github.com/jemalloc/jemalloc/releases/download/4.5.0/jemalloc-4.5.0.tar.bz2
[root@centos ~]# tar xjf jemalloc-4.5.0.tar.bz2
[root@centos ~]# cd jemalloc-4.5.0
[root@centos ~]# ./configure && make && make install
[root@centos ~]# echo '/usr/local/lib' > /etc/ld.so.conf.d/local.conf
[root@centos ~]# ldconfig
2.4 安裝libevent(依賴openssl庫)
***********************************
[root@centos ~]# yum reinstall openssl-devel -y
[root@centos ~]# ldconfig
[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 && make && make install
[root@centos ~]# ll /usr/lib | grep libevent
[root@centos ~]# updatedb
[root@centos ~]# locate libevent
若是libevent的lib目錄不在LD_LIBRARY_PATH裏,可使用如下命令加入
[root@centos ~]# export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr
2.5 建立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.6 編譯mariadb(須要 cmake ncurses-devel bison 庫)
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# wget https://mirrors.tuna.tsinghua.edu.cn/mariadb//mariadb-10.1.23/source/mariadb-10.1.23.tar.gz
[root@centos ~]# tar zvxf mariadb-10.1.23.tar.gz
[root@centos ~]# cd mariadb-10.1.23
[root@centos ~]# cmake \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=/opt/mysql \
-DINSTALL_DOCDIR=share/doc/mariadb-10.1.23 \
-DINSTALL_DOCREADMEDIR=share/doc/mariadb-10.1.23 \
-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.7 建立軟鏈接
[root@centos ~]# ln -s /opt/mysql/lib/lib* /usr/lib/
[root@centos ~]# ln -s /opt/mysql/bin/mysql /bin
[root@centos ~]# ln -s /opt/mysql/bin/mysqldump /bin
[root@centos ~]# ln -s /opt/mysql/bin/mysqlbinlog /bin
2.8 修改配置文件
[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.9 初始化數據庫
[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
2.10 設置數據庫ROOT密碼,移除刪除臨時用戶,刪除測試數據庫等(根據提示操做)
[root@centos ~]# ./bin/mysql_secure_installation
輸入當前密碼。(默認爲空,回車便可)
Enter current password for root (enter for none):
是否設置root密碼,輸入Y,後,輸入兩次密碼
Set root password? [Y/n] y
New password:
Re-enter new password:
是否移除匿名用戶,輸入Y
Remove anonymous users? [Y/n] y
是否禁止root遠程登陸,輸入Y
Disallow root login remotely? [Y/n] y
是否刪除測試數據庫,輸入Y
Remove test database and access to it? [Y/n] y
是否從新讀取權限表數據
Reload privilege tables now? [Y/n] y
2.11 登陸數據庫,查看數據庫狀態
[root@centos ~]# mysql -u root -p
MariaDB [(none)]> status;
MariaDB [(none)]> show engines;
MariaDB [(none)]> SHOW VARIABLES LIKE '%have%ssl%';
MariaDB [(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
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(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 ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz
[root@centos ~]# tar zvxf pcre-8.39.tar.gz
[root@centos ~]# cd pcre-8.39
[root@centos ~]# ./configure && make && make install
3.3 建立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
3.4 安裝nginx
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# wget http://nginx.org/download/nginx-1.12.0.tar.gz
[root@centos ~]# tar zvxf nginx-1.12.0.tar.gz
[root@centos ~]# cd nginx-1.12.0
[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.1.0e \
--with-zlib=/usr/local/src/zlib-1.2.11 \
--with-pcre=/usr/local/src/pcre-8.39 \
--with-ld-opt="-ljemalloc" \
--with-http_v2_module \
[root@centos ~]# make && make install
3.5 配置nginx,以支持靜態網頁訪問
[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 {
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
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 保存退出 ( 保存前先 gg=G 格式化)
* 若是須要支持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
* HTTPS性能評估:https://www.ssllabs.com/ssltest/
建立網站配置文件目錄
[root@centos ~]# mkdir -p /opt/nginx/conf/vhosts
建立網站配置文件
[root@centos ~]# vim /opt/nginx/conf/vhosts/web.conf
添加如下內容
server {
listen 80;
server_name 192.168.1.10;
set $root /data/www/web;
root $root;
location / {
index index.html index.htm;
}
}
:wq 保存退出 ( 保存前先 gg=G 格式化)
3.6 創建測試首頁
[root@centos ~]# vim /data/www/web/index.html
<html>
<head><title>nginx index.html</title></head>
<body>
<h1>index.html</h1>
</body>
</html>
保存,退出
3.7 測試和運行
[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.8 防火牆添加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.9 瀏覽器打開
http://192.168.1.10
顯示出歡迎內容,則表示成功
3.10 做爲服務,開機後啓動
[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.11 啓動服務
[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.3 安裝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 && make && make install
[root@centos ~]# cd libltdl/
[root@centos ~]# ./configure --enable-ltdl-install && 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.4 安裝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 && 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 && make && make install
4.5 安裝re2c
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# wget http://sourceforge.net/projects/re2c/files/0.15.3/re2c-0.15.3.tar.gz
[root@centos ~]# tar zvxf re2c-0.15.3.tar.gz
[root@centos ~]# cd re2c-0.15.3
[root@centos ~]# ./configure && make && make install
4.6 安裝php (已經安裝mariadb,nginx,ldap)
4.6.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.6.2 安裝
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# wget http://am1.php.net/distributions/php-7.1.4.tar.gz
[root@centos ~]# tar zvxf php-7.1.4.tar.gz
[root@centos ~]# cd php-7.1.4
[root@centos ~]# ./configure \
--prefix=/opt/php \
--with-config-file-path=/opt/php/etc \
--with-openssl \
--with-mysqli=shared,mysqlnd \
--with-pdo-mysql=shared,mysqlnd \
--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-zlib-dir \
--with-bz2 \
--with-gettext \
--disable-rpath \
--enable-pdo \
--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' && make install
4.6.3 複製配置文件
[root@centos ~]# cp php.ini-production /opt/php/etc/php.ini
4.6.4 開啓系統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
設置永久有效(重啓後自動開啓HugePages)
[root@centos ~]# vim /etc/sysctl.conf
vm.nr_hugepages=512
:wq 保存退出
4.6.5 修改php配置文件,支持ZendOpcache
[root@centos ~]# ll /opt/php/lib/php/extensions/no-debug-non-zts-20160303
[root@centos ~]# vim /opt/php/etc/php.ini
在文件中搜索; extension_dir = "./" ,並在下面添加如下內容(若是extension_dir已存在,只添加後面的內容)
extension_dir = "/opt/php/lib/php/extensions/no-debug-non-zts-20160303/"
zend_extension="opcache.so"
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=20000
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 保存退出
4.6.6 修改php配置文件,支持pdo_mysql,mysqli
[root@centos ~]# vim /opt/php/etc/php.ini
在文件中搜索; extension_dir = "./" ,並在下面添加如下內容(若是extension_dir已存在,只添加後面的一行)
extension_dir = "/opt/php/lib/php/extensions/no-debug-non-zts-20160303/"
extension = "pdo_mysql.so"
extension = "mysqli.so"
:wq 保存退出
4.6.7 安裝xdebug擴展(調試PHP用,不須要時可忽略)
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# wget http://xdebug.org/files/xdebug-2.5.0.tgz
[root@centos ~]# tar zvxf xdebug-2.5.0.tgz
[root@centos ~]# cd xdebug-2.5.0
[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-20160303/"
[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.6.8 安裝memcahced擴展 (須要 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 && make && make install
[root@centos ~]# cd /usr/local/src
[root@centos ~]# yum install git -y
[root@centos ~]# git clone https://github.com/php-memcached-dev/php-memcached.git
[root@centos ~]# cd php-memcached
[root@centos ~]# git checkout php7
[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-20160303/
修改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-20160303/"
extension = "memcached.so"
4.6.9 安裝redis擴展
[root@centos ~]# cd /usr/local/src
[root@centos ~]# yum install git -y
[root@centos ~]# git clone https://github.com/phpredis/phpredis/
[root@centos ~]# cd phpredis
[root@centos ~]# git checkout php7
[root@centos ~]# /opt/php/bin/phpize
[root@centos ~]# ./configure --with-php-config=/opt/php/bin/php-config
[root@centos ~]# make && make install
[root@centos ~]# ll /opt/php/lib/php/extensions/no-debug-non-zts-20160303/
修改php配置文件,支持redis
[root@centos ~]# vim /opt/php/etc/php.ini
在文件中搜索; extension_dir = "./" ,並在下面添加如下內容(若是extension_dir已存在,只添加後面的幾行)
extension_dir = "/opt/php/lib/php/extensions/no-debug-non-zts-20160303/"
extension = "redis.so"
4.6.10 安裝php-fpm
[root@centos ~]# cp /opt/php/etc/php-fpm.conf.default /opt/php/etc/php-fpm.conf
[root@centos ~]# cp /opt/php/etc/php-fpm.d/www.conf.default /opt/php/etc/php-fpm.d/web.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
:wq 保存退出
[root@centos ~]# vim /opt/php/etc/php-fpm.d/web.conf
修改內容,而且讓其它生效
user = www
group = www
pm.max_children = 35
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
:wq 保存退出
# 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.11 配置nginx,支持php
建立網站配置文件(網站目錄/data/www/web)
[root@centos ~]# vim /opt/nginx/conf/vhosts/web.conf
添加如下內容
server {
listen 80;
server_name 192.168.1.10;
set $root /data/www/web;
root $root;
location / {
index index.html index.php;
}
location ~ \.php {
root /data/www/web;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
:wq 保存退出 ( 保存前先 gg=G 格式化)
4.6.12 將php-fpm服務加到開機啓動服務
[root@centos ~]# cp /usr/local/src/php-7.1.4/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.6.13 編寫測試頁面
[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.10/index.php
5 安裝memcached服務
5.1 安裝memcached (須要libevent庫)
[root@centos ~]# cd /usr/local/src
[root@centos ~]# wget http://memcached.org/files/memcached-1.4.33.tar.gz
[root@centos ~]# tar zvxf memcached-1.4.33.tar.gz
[root@centos ~]# cd memcached-1.4.33
[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"
IP="127.0.0.1"
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 -l $IP -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 防火牆添加11211端口(若是不須要外部訪問,可忽略)
[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/index.php
添加如下內容
<html>
<head><title>hello php</title></head>
<body>
<h1 style="text-align:center;">
<?php
$mem = new Memcached;
$mem->addServer('127.0.0.1',11211);
$mem->set('test',"hello memcached");
$val=$mem->get('test');
echo $val;
?>
</h1>
<?php phpinfo();?>
</body>
</html>
:wq 保存退出
瀏覽器訪問:http://192.168.1.10/index.php
6.配置Laravel
6.1 安裝composer
6.1.1 關掉xdebug擴展 (也能夠不關閉,但會影響composer的速度)
[root@centos ~]# vim /opt/php/etc/php.ini
找到如下內容,註釋掉
;[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 保存退出
[root@centos ~]# systemctl restart php-fpm
6.2 添加php軟鏈接
[root@centos ~]# /opt/php/bin/php --version
[root@centos ~]# ln -s /opt/php/bin/php /usr/local/bin/php
[root@centos ~]# php --version
6.3 下載 composer 腳本文件
[root@centos ~]# cd /usr/local/src
[root@centos ~]# wget https://getcomposer.org/download/1.4.1/composer.phar
[root@centos ~]# chmod +x composer.phar
[root@centos ~]# ll /usr/local/src/composer.phar
-rwxr-xr-x 1 root root 1815925 1月 8 2017 composer.phar
檢查composer版本
[root@centos ~]# /usr/local/src/composer.phar -V
會提示如下內容,能夠忽略無論,主要是查看版本
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Composer version 1.4.1 2017-03-10 09:29:45
建立軟鏈接
[root@centos ~]# ln -s /usr/local/src/composer.phar /usr/local/bin/composer
[root@centos ~]# ll /usr/local/bin/composer
lrwxrwxrwx 1 root root 28 1月 6 07:15 /usr/local/bin/composer -> /usr/local/src/composer.phar
[root@centos ~]# composer -V
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Composer version 1.4.1 2017-03-10 09:29:45
更改composer到國內源(速度快不少)
[root@centos ~]# composer config -g repo.packagist composer https://packagist.phpcomposer.com
composer 自更新 (成功後會出再提示內容)
[root@centos ~]# composer self-update
Updating to version 1.4.1 (stable channel).
重啓
[root@centos ~]# shutdown -r now
6.4 初始化laravel項目
6.4.1 進入項目目錄
[root@centos ~]# cd /data/www/
6.4.2 建立laravel項目
[root@centos ~]# composer create-project laravel/laravel --prefer-dist laravel5
[root@centos ~]# ll /data/www/
6.4.3 修改項目權限,以及storage和vendor文件夾(不然無法正常顯示)
[root@centos ~]# chown -R www:www /data/www/laravel5/
[root@centos ~]# chmod -R 766 /data/www/laravel5/storage
6.4.4.建立配置網站文件
由於以前的網站 web.conf 使用80端口,爲避免IP、域名和端口產生衝突,須要先更名爲 web.conf.bak
[root@centos ~]# mv /opt/nginx/conf/vhosts/web.conf /opt/nginx/conf/vhosts/web.conf.bak
新建 laravel5 網站的配置文件
[root@centos ~]# vim /opt/nginx/conf/vhosts/laravel5.conf
錄入如下內容 (注意端口不要重複。)
server {
listen 80;
server_name 192.168.1.10;
set $root_dir /data/www/laravel5/public/;
root $root_dir;
location / {
index index.html index.php;
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php {
root $root_dir;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $root_dir$fastcgi_script_name;
include fastcgi_params;
}
}
:wq 保存退出
*********************
vhosts目錄下的全部 ***.conf 配置文件,任意兩個配置文件,相同IP或者域名時,端口都不能重複。
如 vhosts下的4個配置文件,不會形成IP、域名 和端口衝突
listen 80;
server_name 192.168.1.10;
listen 8080;
server_name 192.168.1.10;
listen 80;
server_name 192.168.1.11;
listen 80;
server_name www.abc.com;
*********************
6.4.5 修改php.ini 防止攻擊者欺騙PHP去執行一些不該該執行的代碼
[root@centos ~]# vim /opt/php/etc/php.ini
添加如下內容
cgi.fix_pathinfo=0
:wq 保存退出
重啓服務,使用瀏覽器打開
[root@centos ~]# systemctl restart nginx
[root@centos ~]# systemctl restart php-fpm
查看 laravel 版本
[root@centos ~]# cd /data/www/laravel5
[root@centos ~]# php artisan --version
Laravel Framework 5.4.22
用瀏覽器打開 http://192.168.1.10/index.php
顯示出Laravel的LOGO則表示成功