CentOS6.8系統源碼部署lnmp環境

搭建LNMP環境(CentOS 6.8)
本文檔介紹如何使用一臺普通配置的雲服務器ECS實例或聯網的虛擬主機搭建LNMP平臺的web環境。php

Linux:自由和開放源碼的類UNIX操做系統。
Nginx:輕量級網頁服務器、反向代理服務器。
MySQL:關係型數據庫管理系統。
PHP:主要適用於Web開發領域的一種腳本語言。html

基本流程
1.準備編譯環境
2.安裝nginx
3.安裝mysql
4.安裝php-fpm
5.測試訪問mysql


步驟一:準備編譯環境linux

本文主要說明手動安裝LNMP平臺的操做步驟,您也能夠在雲市場購買LNMP鏡像直接啓動ECS,以便快速建站。nginx

系統版本說明c++

cat /etc/redhat-release
CentOS release 6.8 (Final)web

uname -r
2.6.32-696.6.3.el6.x86_64sql

注:這是本文檔實施時參考的系統版本。您的實際使用版本可能與此不一樣,下文中的nginx,mysql,及php版本,您也能夠根據實際狀況選擇相應版本。
關閉SELINUX數據庫

修改配置文件,重啓服務後永久生效。vim

sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config

命令行設置當即生效。

setenforce 0



步驟二:安裝nginx

Nginx是一個小巧而高效的Linux下的Web服務器軟件,是由 Igor Sysoev 爲俄羅斯訪問量第二的 Rambler.ru 站點開發的,已經在一些俄羅斯的大型網站上運行多年,目前不少國內外的門戶網站、行業網站也都在是使用Nginx,至關穩定。

添加運行nginx服務進程的用戶

groupadd -r nginx
useradd -r -g nginx nginx
下載源碼包解壓編譯。

wget http://nginx.org/download/nginx-1.10.2.tar.gz
tar xvf nginx-1.10.2.tar.gz -C /usr/local/src
yum groupinstall "Development tools"
yum -y install gcc wget gcc-c++ automake autoconf libtool libxml2-devel libxslt-devel perl-devel perl-ExtUtils-Embed pcre-devel openssl-devel (安裝依賴包)

cd /usr/local/src/nginx-1.10.2
./configure \
--prefix=/usr/local/nginx \
--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.pid \
--lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/var/tmp/nginx/client \
--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 \
--user=nginx \
--group=nginx \
--with-pcre \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-mail \
--with-mail_ssl_module \
--with-file-aio \
--with-ipv6 \
--with-http_v2_module \
--with-threads \
--with-stream \
--with-stream_ssl_module

echo $? (記得檢查命令是否正常執行)
make && make install
mkdir -pv /var/tmp/nginx/client

添加nginx啓動腳本。
vim /etc/init.d/nginx
#!/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
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
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
killall -9 nginx
}
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

賦予腳本執行權限。

chmod +x /etc/init.d/nginx

添加至服務管理列表,設置開機自啓。

chkconfig --add nginx
chkconfig nginx on
chkconfig --list | grep nginx

啓動並檢查服務。

service nginx start 或 /etc/init.d/nginx start
netstat -tnlp | grep nginx
ps -ef | grep nginx | grep -v "grep"

瀏覽器訪問可看到默認歡迎頁面。圖片1CentOS6.8系統源碼部署lnmp環境



步驟三:安裝mysql

準備編譯環境。

yum groupinstall "Server Platform Development" "Development tools" -y
yum install cmake -y

準備mysql數據存放目錄。

mkdir /mnt/data
groupadd -r mysql
useradd -r -g mysql -s /sbin/nologin mysql
id mysql
uid=497(mysql) gid=498(mysql) groups=498(mysql)

更改數據目錄屬主屬組。

chown -R mysql:mysql /mnt/data

解壓編譯在MySQL官網下載的穩定版源碼包,這裏使用的是5.6.24版本
wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.24.tar.gz
tar xvf mysql-5.6.24.tar.gz -C /usr/local/src

cd /usr/local/src/mysql-5.6.24
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/mnt/data \
-DSYSCONFDIR=/etc \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DWITH_SSL=system \
-DWITH_ZLIB=system \
-DWITH_LIBWRAP=0 \
-DMYSQL_TCP_PORT=3306 \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci

echo $?
make && make install

修改安裝目錄的屬組爲mysql。

chown -R mysql:mysql /usr/local/mysql/

初始化數據庫。(這步很關鍵,出錯必定要解決)

/usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/mnt/data/ --basedir=/usr/local/mysql
echo $?

注:在CentOS 6.5之後版操做系統的最小安裝完成後,在/etc目錄下會存在一個my.cnf,須要將此文件改名爲其餘的名字,如:/etc/my.cnf.bak,不然,該文件會干擾源碼安裝的MySQL的正確配置,形成沒法啓動。

拷貝配置文件和啓動腳本。

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
cp support-files/my-default.cnf /etc/my.cnf

設置開機自動啓動。

chkconfig mysqld on
chkconfig --add mysqld

修改配置文件中的安裝路徑及數據目錄存放路徑。

echo -e "basedir = /usr/local/mysql\ndatadir = /mnt/data\n" >> /etc/my.cnf

設置PATH環境變量。

echo "export PATH=$PATH:/usr/local/mysql/bin" > /etc/profile.d/mysql.sh
source /etc/profile.d/mysql.sh

啓動並檢查服務。

service mysqld start 或 /etc/init.d/mysqld start
netstat -tnlp | grep 3306
ps -ef | grep "mysqld"|grep -v "grep"
mysql -h 127.0.0.1



步驟四:安裝php-fpm

Nginx自己不能處理PHP,做爲web服務器,當它接收到請求後,不支持對外部程序的直接調用或者解析,必須經過FastCGI進行調用。若是是PHP請求,則交給PHP解釋器處理,並把結果返回給客戶端。PHP-FPM是支持解析php的一個FastCGI進程管理器。提供了更好管理PHP進程的方式,能夠有效控制內存和進程、能夠平滑重載PHP配置。

安裝依賴包。

yum install libmcrypt libmcrypt-devel mhash mhash-devel libxml2 libxml2-devel bzip2 bzip2-devel
解壓官網下載的源碼包,編譯安裝。

注:在官網下載對應版本號,先找到版本系列,後面修改版本號便可
wget http://cn2.php.net/distributions/php-5.6.23.tar.gz
tar xvf php-5.6.23.tar.gz -C /usr/local/src

cd /usr/local/src/php-5.6.23
./configure --prefix=/usr/local/php \
--with-config-file-scan-dir=/etc/php.d \
--with-config-file-path=/etc \
--with-mysql=/usr/local/mysql \
--with-mysqli=/usr/local/mysql/bin/mysql_config \
--enable-mbstring \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--with-openssl \
--enable-xml \
--enable-sockets \
--enable-fpm \
--with-mcrypt

echo $?
make && make install

添加php和php-fpm配置文件。

cp /usr/local/src/php-5.6.23/php.ini-production /etc/php.ini
cd /usr/local/php/etc/
cp php-fpm.conf.default php-fpm.conf
sed -i 's@;pid = run/php-fpm.pid@pid = /usr/local/php/var/run/php-fpm.pid@' php-fpm.conf

添加php-fpm啓動腳本。

cp /usr/local/src/php-5.6.23/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm

添加php-fpm至服務列表並設置開機自啓。

chkconfig --add php-fpm
chkconfig --list php-fpm
chkconfig php-fpm on

啓動服務。

service php-fpm start 或 /etc/init.d/php-fpm start
netstat -tnlp | grep 9000
ps -ef | grep php-fpm | grep -v "grep"

檢查服務是否正常啓動。

netstat -tnlp | grep 9000
ps -ef | grep php-fpm | grep -v "grep"

添加nginx對fastcgi的支持,首先備份默認的配置文件。

cp /etc/nginx/nginx.conf /etc/nginx/nginx.confbak
cp /etc/nginx/nginx.conf.default /etc/nginx/nginx.conf

編輯/etc/nginx/nginx.conf,在所支持的主頁面格式中添加php格式的主頁,相似以下:

location / {
root /usr/local/nginx/html;
index index.php index.html index.htm;
}

取消如下內容前面的註釋:

location ~ .php$ {
root /usr/local/nginx/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;
}

從新載入nginx的配置文件。

service nginx reload 或 nginx -s reload

在/usr/local/nginx/html/新建index.php的測試頁面,內容以下。

cat index.php
<?php
$conn=mysql_connect('127.0.0.1','root','');
if ($conn){
echo "LNMP platform connect to mysql is successful!";
}else{
echo "LNMP platform connect to mysql is failed!";
}
phpinfo();
?>


步驟五:測試訪問
瀏覽器訪問測試,如看到如下內容則表示LNMP平臺構建完成。圖片2
CentOS6.8系統源碼部署lnmp環境

相關文章
相關標籤/搜索