地址:https://help.aliyun.com/document_detail/50700.html?spm=5176.10695662.1996646101.searchclickresult.670c8bddOSvv6h
搭建LNMP環(huán)境(CentOS 6)
文檔提供方:杭州玳數(shù)科技有限公司更新時間:2017-04-19 11:02:51
本文檔介紹如何使用一臺普通配置的云服務(wù)器ECS實例搭建LNMP平臺的web環(huán)境撬碟。
Linux:自由和開放源碼的類UNIX操作系統(tǒng)邓萨。
Nginx:輕量級網(wǎng)頁服務(wù)器展氓、反向代理服務(wù)器巷送。
MySQL:關(guān)系型數(shù)據(jù)庫管理系統(tǒng)。
PHP:主要適用于Web開發(fā)領(lǐng)域的一種腳本語言。
適用于熟悉Linux操作系統(tǒng),剛開始使用阿里云進(jìn)行建站的個人用戶颤绕。
使用云服務(wù)器 ECS 搭建LNMP平臺的操作步驟如下:
準(zhǔn)備編譯環(huán)境
安裝nginx
安裝mysql
安裝php-fpm
測試訪問
本文主要說明手動安裝LNMP平臺的操作步驟,您也可以在云市場購買LNMP鏡像直接啟動ECS祟身,以便快速建站奥务。
1、系統(tǒng)版本說明
# cat /etc/redhat-release
CentOSrelease6.5(Final)
注:這是本文檔實施時參考的系統(tǒng)版本袜硫。您的實際使用版本可能與此不同氯葬,下文中的nginx,mysql婉陷,及php版本帚称,您也可以根據(jù)實際情況選擇相應(yīng)版本。
2秽澳、關(guān)閉SELINUX
修改配置文件闯睹,重啟服務(wù)后永久生效。
# sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
命令行設(shè)置立即生效担神。
# setenforce 0
3楼吃、安全組設(shè)置
在ECS安全組放行需訪問的端口和訪問白名單,下面的示例表示允許所有IP訪問服務(wù)器的80端口妄讯。您可以根據(jù)實際情況放行允許訪問的客戶端IP孩锡。
Nginx是一個小巧而高效的Linux下的Web服務(wù)器軟件,是由 Igor Sysoev 為俄羅斯訪問量第二的 Rambler.ru 站點開發(fā)的捞挥,已經(jīng)在一些俄羅斯的大型網(wǎng)站上運行多年浮创,目前很多國內(nèi)外的門戶網(wǎng)站、行業(yè)網(wǎng)站也都在是使用Nginx砌函,相當(dāng)穩(wěn)定。
1、添加運行nginx服務(wù)進(jìn)程的用戶
# groupadd -r nginx
# useradd -r -g nginx? nginx
2讹俊、下載源碼包解壓編譯垦沉。
# 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
# make && make install
# mkdir -pv /var/tmp/nginx/client
3、添加SysV啟動腳本仍劈。
# 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"]&&exit0
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]||exit5
[-f $NGINX_CONF_FILE]||exit6
echo-n $"Starting $prog: "
daemon $nginx-c $NGINX_CONF_FILE
retval=$?
echo
[$retval-eq0]&&touch $lockfile
return$retval
}
stop(){
echo-n $"Stopping $prog: "
killproc $prog-QUIT
retval=$?
echo
[$retval-eq0]&&rm-f $lockfile
return$retval
killall-9nginx
}
restart(){
configtest||return$?
stop
sleep1
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/null2>&1
}
case"$1"in
start)
rh_status_q&&exit0
$1
;;
stop)
rh_status_q||exit0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q||exit7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q||exit0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit2
esac
4厕倍、賦予腳本執(zhí)行權(quán)限。
# chmod +x /etc/init.d/nginx
5贩疙、添加至服務(wù)管理列表讹弯,設(shè)置開機自啟。
# chkconfig --add nginx
# chkconfig? nginx on
6这溅、啟動服務(wù)组民。
# service nginx start
7、瀏覽器訪問可看到默認(rèn)歡迎頁面悲靴。
1臭胜、準(zhǔn)備編譯環(huán)境。
# yum groupinstall "Server Platform Development"? "Development tools" -y
# yum install cmake -y
2癞尚、準(zhǔn)備mysql數(shù)據(jù)存放目錄耸三。
# 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)
3、更改數(shù)據(jù)目錄屬主屬組浇揩。
# chown -R mysql:mysql /mnt/data
4仪壮、解壓編譯在MySQL官網(wǎng)下載的穩(wěn)定版源碼包,這里使用的是5.6.24版本
# 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
# make && make install
5胳徽、修改安裝目錄的屬組為mysql睛驳。
# chown -R mysql:mysql /usr/local/mysql/
6、初始化數(shù)據(jù)庫膜廊。
# /usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/mnt/data/
注:在CentOS 6.5版操作系統(tǒng)的最小安裝完成后乏沸,在/etc目錄下會存在一個my.cnf,需要將此文件更名為其他的名字爪瓜,如:/etc/my.cnf.bak蹬跃,否則,該文件會干擾源碼安裝的MySQL的正確配置铆铆,造成無法啟動蝶缀。
7、拷貝配置文件和啟動腳本薄货。
# 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
8翁都、設(shè)置開機自動啟動。
# chkconfig mysqld? on
# chkconfig --add mysqld
9谅猾、修改配置文件中的安裝路徑及數(shù)據(jù)目錄存放路徑柄慰。
# echo -e "basedir = /usr/local/mysql\ndatadir = /mnt/data\n" >> /etc/my.cnf
10鳍悠、設(shè)置PATH環(huán)境變量。
# echo "export PATH=$PATH:/usr/local/mysql/bin" > /etc/profile.d/mysql.sh
# source /etc/profile.d/mysql.sh
11坐搔、啟動服務(wù)藏研。
# service mysqld start
# mysql -h 127.0.0.1
Nginx本身不能處理PHP,作為web服務(wù)器概行,當(dāng)它接收到請求后蠢挡,不支持對外部程序的直接調(diào)用或者解析,必須通過FastCGI進(jìn)行調(diào)用凳忙。如果是PHP請求业踏,則交給PHP解釋器處理,并把結(jié)果返回給客戶端涧卵。PHP-FPM是支持解析php的一個FastCGI進(jìn)程管理器勤家。提供了更好管理PHP進(jìn)程的方式,可以有效控制內(nèi)存和進(jìn)程艺演、可以平滑重載PHP配置却紧。
1、安裝依賴包胎撤。
# yum install libmcrypt libmcrypt-devel mhash mhash-devel libxml2 libxml2-devel bzip2 bzip2-devel
2晓殊、解壓官網(wǎng)下載的源碼包,編譯安裝伤提。
# tar xvf php-5.6.23.tar.bz2 -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 \
--with-bz2
# make && make install
3巫俺、添加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
4肿男、添加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
5、添加php-fpm至服務(wù)列表并設(shè)置開機自啟舶沛。
# chkconfig --add php-fpm
# chkconfig --list php-fpm
# chkconfig php-fpm on
6嘹承、啟動服務(wù)。
# service php-fpm start
7如庭、添加nginx對fastcgi的支持叹卷,首先備份默認(rèn)的配置文件。
# 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;
}
取消以下內(nèi)容前面的注釋:
location~\.php${
root/usr/local/nginx/html;
fastcgi_pass127.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
在/usr/local/nginx/html/新建index.php的測試頁面往毡,內(nèi)容如下蒙揣。
# cat index.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();
?>
瀏覽器訪問測試,如看到以下內(nèi)容則表示LNMP平臺構(gòu)建完成开瞭。