搭建LNMPR環境(CentOS 6)

搭建LNMPR環境(CentOS 6)

文檔提供方:杭州玳數科技有限公司    更新時間:2017-04-19 11:02:51

   

本文檔介紹如何使用一臺普通配置的雲服務器ECS實例搭建LNMP平臺的web環境。

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

適用對象

適用於熟悉Linux操作系統,剛開始使用阿里雲進行建站的個人用戶。

基本流程

使用雲服務器 ECS 搭建LNMP平臺的操作步驟如下:

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

步驟一:準備編譯環境

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

1、系統版本說明


 
  1. # cat /etc/redhat-release
  2. CentOS release 6.5 (Final)

注:這是本文檔實施時參考的系統版本。您的實際使用版本可能與此不同,下文中的nginx,mysql,及php版本,您也可以根據實際情況選擇相應版本。

2、關閉SELINUX

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

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

命令行設置立即生效。

# setenforce 0

3、安全組設置

在ECS安全組放行需訪問的端口和訪問白名單,下面的示例表示允許所有IP訪問服務器的80端口。您可以根據實際情況放行允許訪問的客戶端IP。

圖片1

步驟二:安裝nginx

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

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


 
  1. # groupadd -r nginx
  2. # useradd -r -g nginx nginx

2、下載源碼包解壓編譯。


 
  1. # wget http://nginx.org/download/nginx-1.10.2.tar.gz
  2. # tar xvf nginx-1.10.2.tar.gz -C /usr/local/src
  3. # yum groupinstall "Development tools"
  4. # yum -y install gcc wget gcc-c++ automake autoconf libtool libxml2-devel libxslt-devel perl-devel perl-ExtUtils-Embed pcre-devel openssl-devel
  5. # cd /usr/local/src/nginx-1.10.2
  6. # ./configure \
  7. --prefix=/usr/local/nginx \
  8. --sbin-path=/usr/sbin/nginx \
  9. --conf-path=/etc/nginx/nginx.conf \
  10. --error-log-path=/var/log/nginx/error.log \
  11. --http-log-path=/var/log/nginx/access.log \
  12. --pid-path=/var/run/nginx.pid \
  13. --lock-path=/var/run/nginx.lock \
  14. --http-client-body-temp-path=/var/tmp/nginx/client \
  15. --http-proxy-temp-path=/var/tmp/nginx/proxy \
  16. --http-fastcgi-temp-path=/var/tmp/nginx/fcgi \
  17. --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
  18. --http-scgi-temp-path=/var/tmp/nginx/scgi \
  19. --user=nginx \
  20. --group=nginx \
  21. --with-pcre \
  22. --with-http_v2_module \
  23. --with-http_ssl_module \
  24. --with-http_realip_module \
  25. --with-http_addition_module \
  26. --with-http_sub_module \
  27. --with-http_dav_module \
  28. --with-http_flv_module \
  29. --with-http_mp4_module \
  30. --with-http_gunzip_module \
  31. --with-http_gzip_static_module \
  32. --with-http_random_index_module \
  33. --with-http_secure_link_module \
  34. --with-http_stub_status_module \
  35. --with-http_auth_request_module \
  36. --with-mail \
  37. --with-mail_ssl_module \
  38. --with-file-aio \
  39. --with-ipv6 \
  40. --with-http_v2_module \
  41. --with-threads \
  42. --with-stream \
  43. --with-stream_ssl_module
  44. # make && make install
  45. # mkdir -pv /var/tmp/nginx/client

3、添加SysV啓動腳本。


 
  1. # vim /etc/init.d/nginx
  2. #!/bin/sh
  3. #
  4. # nginx - this script starts and stops the nginx daemon
  5. #
  6. # chkconfig: - 85 15
  7. # description: Nginx is an HTTP(S) server, HTTP(S) reverse \
  8. # proxy and IMAP/POP3 proxy server
  9. # processname: nginx
  10. # config: /etc/nginx/nginx.conf
  11. # config: /etc/sysconfig/nginx
  12. # pidfile: /var/run/nginx.pid
  13.  
  14. # Source function library.
  15. . /etc/rc.d/init.d/functions
  16.  
  17. # Source networking configuration.
  18. . /etc/sysconfig/network
  19.  
  20. # Check that networking is up.
  21. [ "$NETWORKING" = "no" ] && exit 0
  22.  
  23. nginx="/usr/sbin/nginx"
  24. prog=$(basename $nginx)
  25.  
  26. NGINX_CONF_FILE="/etc/nginx/nginx.conf"
  27.  
  28. [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
  29.  
  30. lockfile=/var/lock/subsys/nginx
  31.  
  32. start() {
  33. [ -x $nginx ] || exit 5
  34. [ -f $NGINX_CONF_FILE ] || exit 6
  35. echo -n $"Starting $prog: "
  36. daemon $nginx -c $NGINX_CONF_FILE
  37. retval=$?
  38. echo
  39. [ $retval -eq 0 ] && touch $lockfile
  40. return $retval
  41. }
  42.  
  43. stop() {
  44. echo -n $"Stopping $prog: "
  45. killproc $prog -QUIT
  46. retval=$?
  47. echo
  48. [ $retval -eq 0 ] && rm -f $lockfile
  49. return $retval
  50. killall -9 nginx
  51. }
  52.  
  53. restart() {
  54. configtest || return $?
  55. stop
  56. sleep 1
  57. start
  58. }
  59.  
  60. reload() {
  61. configtest || return $?
  62. echo -n $"Reloading $prog: "
  63. killproc $nginx -HUP
  64. RETVAL=$?
  65. echo
  66. }
  67.  
  68. force_reload() {
  69. restart
  70. }
  71.  
  72. configtest() {
  73. $nginx -t -c $NGINX_CONF_FILE
  74. }
  75.  
  76. rh_status() {
  77. status $prog
  78. }
  79.  
  80. rh_status_q() {
  81. rh_status >/dev/null 2>&1
  82. }
  83.  
  84. case "$1" in
  85. start)
  86. rh_status_q && exit 0
  87. $1
  88. ;;
  89. stop)
  90. rh_status_q || exit 0
  91. $1
  92. ;;
  93. restart|configtest)
  94. $1
  95. ;;
  96. reload)
  97. rh_status_q || exit 7
  98. $1
  99. ;;
  100. force-reload)
  101. force_reload
  102. ;;
  103. status)
  104. rh_status
  105. ;;
  106. condrestart|try-restart)
  107. rh_status_q || exit 0
  108. ;;
  109. *)
  110. echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
  111. exit 2
  112. esac

4、賦予腳本執行權限。

# chmod +x /etc/init.d/nginx

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


 
  1. # chkconfig --add nginx
  2. # chkconfig nginx on

6、啓動服務。

# service nginx start

7、瀏覽器訪問可看到默認歡迎頁面。圖片2

步驟三:安裝mysql

1、準備編譯環境。


 
  1. # yum groupinstall "Server Platform Development" "Development tools" -y
  2. # yum install cmake -y

2、準備mysql數據存放目錄。


 
  1. # mkdir /mnt/data
  2. # groupadd -r mysql
  3. # useradd -r -g mysql -s /sbin/nologin mysql
  4. # id mysql
  5. uid=497(mysql) gid=498(mysql) groups=498(mysql)

3、更改數據目錄屬主屬組。

# chown -R mysql:mysql /mnt/data

4、解壓編譯在MySQL官網下載的穩定版源碼包,這裏使用的是5.6.24版本


 
  1. # tar xvf mysql-5.6.24.tar.gz -C /usr/local/src
  2. # cd /usr/local/src/mysql-5.6.24
  3. # cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
  4. -DMYSQL_DATADIR=/mnt/data \
  5. -DSYSCONFDIR=/etc \
  6. -DWITH_INNOBASE_STORAGE_ENGINE=1 \
  7. -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
  8. -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
  9. -DWITH_READLINE=1 \
  10. -DWITH_SSL=system \
  11. -DWITH_ZLIB=system \
  12. -DWITH_LIBWRAP=0 \
  13. -DMYSQL_TCP_PORT=3306 \
  14. -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
  15. -DDEFAULT_CHARSET=utf8 \
  16. -DDEFAULT_COLLATION=utf8_general_ci
  17. # make && make install

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

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

6、初始化數據庫。

# /usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/mnt/data/

注:在CentOS 6.5版操作系統的最小安裝完成後,在/etc目錄下會存在一個my.cnf,需要將此文件更名爲其他的名字,如:/etc/my.cnf.bak,否則,該文件會干擾源碼安裝的MySQL的正確配置,造成無法啓動。

注:如果出現“FATAL ERROR: Could not find ./share/fill_help_tables.sql”的異常,可使用該命令:/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql/ --user=mysql --datadir=/mnt/data/

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


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

8、設置開機自動啓動。


 
  1. # chkconfig mysqld on
  2. # chkconfig --add mysqld

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

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

10、設置PATH環境變量。


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

11、啓動服務。


 
  1. # service mysqld start
  2. # mysql -h 127.0.0.1

步驟四:安裝php-fpm

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

1、安裝依賴包。


 
  1. # yum install libmcrypt libmcrypt-devel mhash mhash-devel libxml2 libxml2-devel bzip2 bzip2-devel

2、解壓官網下載的源碼包,編譯安裝。


 
  1. # tar xvf php-5.6.23.tar.bz2 -C /usr/local/src
  2. # cd /usr/local/src/php-5.6.23
  3. # ./configure --prefix=/usr/local/php \
  4. --with-config-file-scan-dir=/etc/php.d \
  5. --with-config-file-path=/etc \
  6. --with-mysql=/usr/local/mysql \
  7. --with-mysqli=/usr/local/mysql/bin/mysql_config \
  8. --enable-mbstring \
  9. --with-freetype-dir \
  10. --with-jpeg-dir \
  11. --with-png-dir \
  12. --with-zlib \
  13. --with-libxml-dir=/usr \
  14. --with-openssl \
  15. -enable-xml \
  16. --enable-sockets \
  17. --enable-fpm \
  18. --with-mcrypt \
  19. --with-bz2
  20. # make && make install

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


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

4、添加php-fpm啓動腳本。


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

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


 
  1. # chkconfig --add php-fpm
  2. # chkconfig --list php-fpm
  3. # chkconfig php-fpm on

6、啓動服務。

# service php-fpm start

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


 
  1. # cp /etc/nginx/nginx.conf /etc/nginx/nginx.confbak
  2. # cp /etc/nginx/nginx.conf.default /etc/nginx/nginx.conf

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


 
  1. location / {
  2. root /usr/local/nginx/html;
  3. index index.php index.html index.htm;
  4. }

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


 
  1. location ~ \.php$ {
  2. root /usr/local/nginx/html;
  3. fastcgi_pass 127.0.0.1:9000;
  4. fastcgi_index index.php;
  5. fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/$fastcgi_script_name;
  6. include fastcgi_params;
  7. }

重新載入nginx的配置文件。

# service nginx reload

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


 
  1. # cat index.php
  2. <?php
  3. $conn=mysql_connect('127.0.0.1','root','');
  4. if ($conn){
  5. echo "LNMP platform connect to mysql is successful!";
  6. }else{
  7. echo "LNMP platform connect to mysql is failed!";
  8. }
  9. phpinfo();
  10. ?>

瀏覽器訪問測試,如看到以下內容則表示LNMP平臺構建完成。圖片3

步驟五:安裝redis

1、檢查是否有redis yum 源

1

yum install redis

2、下載fedora的epel倉庫

1

yum install epel-release

3、安裝redis數據庫

1

yum install redis

4、安裝完畢後,使用下面的命令啓動redis服務

1

2

3

4

5

6

7

8

# 啓動redis

service redis start

# 停止redis

service redis stop

# 查看redis運行狀態

service redis status

# 查看redis進程

ps -ef | grep redis

5、設置redis爲開機自動啓動

1

chkconfig redis on

6、進入redis服務

1

2

3

4

# 進入本機redis

redis-cli

# 列出所有key

keys *

7、防火牆開放相應端口

1

2

3

4

5

6

7

8

# 開啓6379

/sbin/iptables -I INPUT -p tcp --dport 6379 -j ACCEPT

# 開啓6380

/sbin/iptables -I INPUT -p tcp --dport 6380 -j ACCEPT

# 保存

/etc/rc.d/init.d/iptables save

# centos 7下執行

service iptables save

二、修改redis默認端口和密碼

1、打開配置文件

1

vi /etc/redis.conf

2、修改默認端口,查找 port 6379 修改爲相應端口即可

3、修改默認密碼,查找 requirepass foobared 將 foobared 修改爲你的密碼

 

4、使用配置文件啓動 redis

1

redis-server /etc/redis.conf &

5、使用端口登錄

1

redis-cli -h 127.0.0.1 -p 6179

6、此時再輸入命令則會報錯

7、輸入剛纔輸入的密碼

1

auth 111

 8、停止redis

  命令方式關閉redis

1

2

redis-cli -h 127.0.0.1 -p 6179

shutdown

  進程號殺掉redis

1

2

ps -ef | grep redis

kill -9 XXX

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章