(CentOS 6.8 + Nginx 1.10.3 + Mysql 5.7.18 + PHP 5.6.31)
一侈净、安裝Nginx
1、安裝依賴
# yum -y install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
2僧凤、下載 Nginx
# cd /usr/local/src
# wget http://nginx.org/download/nginx-1.10.3.tar.gz
3畜侦、解壓
# tar -zxvf nginx-1.10.3.tar.gz
4、編譯
# cd nginx-1.10.3
# ./configure \
--prefix=/usr \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-pcre
# make && make install
5拼弃、創(chuàng)建啟動腳本
# vim /etc/init.d/nginx
5-1夏伊、腳本內(nèi)容
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: NGINX is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -n "$user" ]; then
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
fi
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
6、修改腳本權(quán)限
# chmod 755 /etc/init.d/nginx
7吻氧、設(shè)置開機(jī)啟動
# chkconfig --add nginx
# service nginx reload
# chkconfig nginx on
8溺忧、啟動 Nginx
# service nginx start
二、安裝 Mysql
1盯孙、安裝依賴
# yum -y install gcc gcc-c++ gcc-g77 make cmake bison ncurses-devel autoconf automake zlib* fiex* libxml* libmcrypt* libtool-ltdl-devel* libaio libaio-devel bzr libtool ncurses5-devel imake libxml2-devel expat-devel
2鲁森、安裝 boost_1_59_0 (必須是該版本)
2-1、下載 boost_1_59_0
# cd /usr/local/src
# wget https://nchc.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz
2-2振惰、解壓
# tar -zxvf boost_1_59_0.tar.gz
2-3歌溉、配置
# cd boost_1_59_0
# ./bootstrap.sh
2-4、編譯
# ./b2
2-5骑晶、安裝
# ./b2 install
3痛垛、安裝 cmake (最新版本)
3-1、下載 cmake
# cd /usr/local/src
# wget https://cmake.org/files/v3.8/cmake-3.8.0.tar.gz
3-2桶蛔、解壓
# tar -xzvf cmake-3.8.0.tar.gz
3-3匙头、編譯安裝
# cd cmake-3.8.0
# ./bootstrap
# gmake
# gmake install
4、安裝 Mysql 5.7.18
4-1仔雷、添加mysql用戶和所屬組
# /usr/sbin/groupadd mysql
# /usr/sbin/useradd -g mysql mysql
4-2蹂析、創(chuàng)建mysql安裝目錄和數(shù)據(jù)目錄
# mkdir /usr/local/mysql
# mkdir /usr/local/mysql/data
4-3、修改mysql目錄所有者
# chown -R mysql:mysql /usr/local/mysql
4-4碟婆、下載mysql源碼包
# cd /usr/local/src
# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.18.tar.gz
4-5电抚、解壓
# tar -xzvf mysql-5.7.18.tar.gz
4-6、cmake編譯配置
# cd mysql-5.7.18
# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DMYSQL_DATADIR=/usr/local/mysql/mydata \
-DSYSCONFDIR=/etc \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DMYSQL_TCP_PORT=3306 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_SSL=yes \
-DWITH_BOOST=/usr/local/src/boost_1_59_0 \
-DMYSQL_USER=mysql
4-7竖共、編譯安裝
# make && make install
【
注意:這里可能會報(bào)錯(cuò)(如果在阿里云內(nèi)存不足編譯失敗)
錯(cuò)誤代碼信息:
c++: Internal error: Killed (program cc1plus)
Please submit a full bug report.
See < http://bugzilla.redhat.com/bugzilla > for instructions.
make[2]: *** [sql/CMakeFiles/sql.dir/item_geofunc.cc.o] Error 1
make[1]: *** [sql/CMakeFiles/sql.dir/all] Error 2
make: *** [all] Error 2
處理方法:用2g分區(qū)交換蝙叛,運(yùn)行下面
# dd if=/dev/zero of=/swapfile bs=1k count=2048000 --獲取要增加的2G的SWAP文件塊
# mkswap /swapfile -- 創(chuàng)建SWAP文件
# swapon /swapfile -- 激活SWAP文件
# swapon -s -- 查看SWAP信息是否正確
# echo "/var/swapfile swap swap defaults 0 0" >> /etc/fstab -- 添加到fstab文件中讓系統(tǒng)引導(dǎo)時(shí)自動啟動
然后需要刪除CMakeCache.txt文件,再重新cmake預(yù)編譯
】
4-8公给、初始化數(shù)據(jù)庫【注意:運(yùn)行后最后一句[note] 生成了一個(gè)mysql默認(rèn)密碼甥温,復(fù)制到一個(gè)地方锻煌,保存下來∫鲵荆】
# cd /usr/local/mysql/bin
# ./mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data/ --basedir=/usr/local/mysql --socket=/usr/local/mysql/mysql.sock
4-9、添加mysql服務(wù)
# cd /usr/local/src/mysql-5.7.18
# cd support-files
# cp -a mysql.server /etc/init.d/mysql
4-10匣沼、編輯mysql配置文件
# cd /etc
# sudo vi my.cnf
【修改配置文件中的
1)狰挡、datadir=/usr/local/mysql/data
2)、socket=/usr/local/mysql/mysql.sock
】
4-11释涛、創(chuàng)建文件夾并給定用戶和權(quán)限
# mkdir /var/lib/mysql
# chown mysql:mysql /var/lib/mysql
# chmod 777 /var/lib/mysql
4-12加叁、啟動mysql
# service mysql start
4-13、設(shè)置開機(jī)啟動
# chkconfig mysql on
4-14唇撬、添加環(huán)境變量
# sudo vi /etc/profile
在文件末尾添加一行: export PATH=$PATH:/usr/local/mysql/bin
# :wq
# source /etc/profile (使修改的profile文件生效)
4-15它匕、登錄mysql
# mysql -u root -p
【注意:輸入 4-8 中保存的密碼后 回車】
4-16、修改root密碼
# SET PASSWORD = PASSWORD('yourpassword');
4-17窖认、刷新mysql的系統(tǒng)權(quán)限相關(guān)表
# flush privileges;
4-18豫柬、退出,并用新密碼重新登錄即可
# quit;
三扑浸、安裝php
1烧给、安裝依賴
# yum install -y gcc gcc-c++ make zlib zlib-devel pcre pcre-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel openssl openssl-devel openldap openldap-devel openldap-clients openldap-servers php-mcrypt libmcrypt libmcrypt-devel
2、下載 php
# cd /usr/local/src
# wget http://cn2.php.net/distributions/php-5.6.31.tar.gz
3喝噪、解壓
# tar -zxvf php-5.6.31.tar.gz
4础嫡、PHP 編譯配置
# cd php-5.6.31
# ./configure \
--prefix=/usr/local/php \
--with-fpm-user=www --with-fpm-group=www \
--with-config-file-path=/usr/local/php/etc \
--with-mhash --with-mcrypt --enable-bcmath \
--enable-mysqlnd --with-mysql --with-mysqli --with-pdo-mysql \
--with-gd --enable-gd-native-ttf --with-jpeg-dir --with-png-dir --with-freetype-dir \
--enable-fpm \
--enable-mbstring \
--enable-pcntl \
--enable-sockets \
--enable-opcache \
--with-openssl \
--with-zlib \
--with-curl \
--with-libxml-dir \
--with-iconv-dir
5、編譯安裝
# make && make install
【
注意:
此處可能會出現(xiàn)一個(gè)error:make: *** [sapi/cli/php] Error 1
解決辦法:編輯文件 Makefile
# vim Makefile
# /EXTRA_LIBS (查找EXTRA_LIBS的位置)
然后在末尾添加 -liconv
然后保存酝惧,再重新編譯
】
6榴鼎、復(fù)制生成php-fpm配置文件
# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
7、復(fù)制生成一份php配置文件
# cp php.ini-production /usr/local/php/etc/php.ini
8晚唇、將php-fpm加入系統(tǒng)服務(wù)
# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
9巫财、賦予執(zhí)行權(quán)限
# chmod +x /etc/init.d/php-fpm
10、設(shè)置開機(jī)啟動
# chkconfig php-fpm on
11缺亮、創(chuàng)建www用戶
# groupadd www && useradd -d /home/www -g www www
12翁涤、啟動php-fpm
# service php-fpm start
13、添加環(huán)境變量
# sudo vi /etc/profile
修改行: export PATH=$PATH:/usr/local/mysql/bin:/usr/local/php/bin
# :wq
# source /etc/profile (使修改的profile文件生效)
四萌踱、配置nginx支持php-fpm(修改nginx.conf配置文件)
1葵礼、編輯nginx配置文件,具體路徑根據(jù)實(shí)際的nginx.conf配置文件位置編輯并鸵,下面主要修改nginx的server {}配置塊中的內(nèi)容鸳粉,修改location塊,追加index.php讓nginx服務(wù)器默認(rèn)支持index.php為首頁
# sudo vi /etc/nginx/nginx.conf
location / {
root html;
index index.html index.htm index.php;
}
2园担、然后配置.php請求被傳送到后端的php-fpm模塊届谈,默認(rèn)情況下php配置塊是被注釋的枯夜,此時(shí)去掉注釋并修改為以下內(nèi)容,這里面很多都是默認(rèn)的艰山,root是配置php程序放置的根目錄湖雹,主要修改的就是fastcgi_param中的/scripts為$document_root
location ~ \.php\$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
\$document_root$fastcgi_script_name;
include fastcgi_params;
}
3、保存退出并重啟nginx服務(wù)
# :wq
# service nginx restart
至此曙搬,LNMP環(huán)境已搭建完畢摔吏。
【如若文檔有錯(cuò)誤,歡迎大家不吝賜教纵装。本文檔是集網(wǎng)上各位大神的資源進(jìn)行整合的征讲,具體資源來源已經(jīng)忘記了,如果發(fā)現(xiàn)有侵權(quán)等行為橡娄,請聯(lián)系我诗箍,我將對應(yīng)處理,謝謝~~~】