centosLAMP環境搭建

技術小胖子發表於2017-11-15

說明:

1.官網採用LAMP框架,nginx做前端輪詢代理

2、移除系統自帶的rpm包的http mysql php

yum remove httpd mysql mysql-server php php-cli php-common php-devel php-gd  -y

3 安裝必備的開發包

/usr/bin/yum -y install ntp vim-enhanced gcc gcc-c++ flex bison autoconf automake bzip2-devel

ncurses-devel zlib-devel libjpeg-devel libpng-devel libtiff-devel freetype-devel libXpm-devel

gettext-devel  pam-devel libtool libtool-ltdl openssl openssl-devel fontconfig-devel

libxml2-devel curl-devel  libicu libicu-devel libmcrypt libmcrypt-devel libmhash  libxml2

libmhash-devel  mysql-devel   libxslt libxslt-devel curl tcl expect

4.同步系統時間:

vim /etc/ntp.conf

server 3.cn.pool.ntp.org

server 3.asia.pool.ntp.org

server 0.asia.pool.ntp.org

chkconfig ntpd on

service ntpd restart

5.IP分配

    nginx:192.168.201.4  192.168.201.5  192.168.201.6  192.168.201.7  

    apache:192.168.201.123—192.168.201.138

  1. 一.nginx搭建

  2. 1.下載原始碼包nginx-1.4.2

  3. 2.安裝nginx

  4. ./configuer  –prefix=/usr/local/nginx –with-http_stub_status_module –with-http_ssl_module & make & make install

  5. 3.配置檔案如下

  6. user  nobody;

  7. worker_processes  8;

  8. pid               logs/nginx.pid;


  9. events {

  10.         use epoll;

  11.         worker_connections  65535;

  12.         }


  13. http {


  14. #       設定一個共享記憶體區域reqnum,以用於限制每個客戶端IP可以併發連線的請求數

  15. #        limit_conn_zone $binary_remote_addr zone=reqnum:10m;

  16. #       每個客戶端IP每分鐘只允許5個連線

  17. #        limit_req_zone $binary_remote_addr zone=reqone:10m rate=10r/m;

  18.         include       mime.types;

  19.         default_type  application/octet-stream;


  20.         sendfile              on;  

  21.         tcp_nopush            on;   

  22.         tcp_nodelay           on; 

  23.         keepalive_timeout     60; 

  24.         server_tokens         off;


  25.         log_format    wwwlog  `$remote_addr – $remote_user [$time_local] “$request” `              

  26.                               `$status $body_bytes_sent “$http_referer” `             

  27.                               `”$http_user_agent” $http_x_forwarded_for`;    

  28.         access_log    /usr/local/nginx/logs/access.log wwwlog;


  29.         gzip                    on; 

  30.         gzip_min_length         1k; 

  31.         gzip_buffers            4 16k; 

  32.         gzip_http_version       1.1; 

  33.         gzip_comp_level         3; 

  34.         gzip_types              text/plain application/x-javascript text/css applicati

  35. on/xml; 

  36.         gzip_vary on;


  37.         server_names_hash_bucket_size   128;   

  38.         client_header_buffer_size       32k;  

  39.         client_max_body_size            300m; 

  40.         client_body_buffer_size         512k; 

  41.         large_client_header_buffers     4   32k;  


  42.         proxy_connect_timeout           60; 

  43.         proxy_send_timeout              120; 

  44.         proxy_read_timeout              120; 

  45.         proxy_buffer_size               16k; 

  46.         proxy_buffers                   4   64k; 

  47.         proxy_busy_buffers_size         128k; 

  48.         proxy_temp_file_write_size      128k; 


  49.         index           index.php  index.htm index.html default.php default.htm defaul

  50. t.html index.php3;


  51. upstream os_server {

  52. #                       ip_hash;

  53. #                       server 192.168.201.122:80 weight=1;

  54.                         server 192.168.201.123:80 weight=1;

  55.                         server 192.168.201.124:80 weight=1;

  56.                         server 192.168.201.125:80 weight=1;

  57.                         server 192.168.201.126:80 weight=1;

  58.                         server 192.168.201.127:80 weight=1;

  59.                         server 192.168.201.128:80 weight=1;

  60.                         server 192.168.201.129:80 weight=1;

  61.                         server 192.168.201.130:80 weight=1;

  62.                         server 192.168.201.131:80 weight=1;

  63.                         server 192.168.201.132:80 weight=1;

  64.                         server 192.168.201.133:80 weight=1;

  65.                         server 192.168.201.134:80 weight=1;

  66.                         server 192.168.201.136:80 weight=1;

  67.                         server 192.168.201.137:80 weight=1;

  68.                         server 192.168.201.138:80 weight=1;

  69.                                      }  

  70. upstream m_server {

  71. #                       ip_hash;

  72. #                       server 192.168.201.122:80 weight=1;

  73.                         server 192.168.201.123:80 weight=1;

  74.                         server 192.168.201.124:80 weight=1;

  75.                         server 192.168.201.125:80 weight=1;

  76.                         server 192.168.201.126:80 weight=1;

  77.                         server 192.168.201.127:80 weight=1;

  78.                         server 192.168.201.128:80 weight=1;

  79.                         server 192.168.201.129:80 weight=1;

  80.                         server 192.168.201.130:80 weight=1;

  81.                         server 192.168.201.131:80 weight=1;

  82.                         server 192.168.201.132:80 weight=1;

  83.                         server 192.168.201.133:80 weight=1;

  84.                         server 192.168.201.134:80 weight=1;

  85.                         server 192.168.201.136:80 weight=1;

  86.                         server 192.168.201.137:80 weight=1;

  87.                         server 192.168.201.138:80 weight=1;

  88.                         }

  89. upstream weixin_server {

  90. #                       ip_hash;

  91.                         server 192.168.201.35:80 weight=1;

  92.                         server 192.168.201.34:80 weight=1;

  93.                         }


  94. upstream op_server {    

  95. #                       ip_hash;

  96.                         server 192.168.201.35:80 weight=1;

  97. #                       server 192.168.201.34:80 weight=1;

  98.                         }

  99. #—————————www.lqxshop.com———————–

  100. server {

  101.          listen 80;

  102.          server_name    www.shop.com www.ilqx.cn www.chenggongfang.com wx.lqxshop.c

  103. om;

  104.          #限制每個連線的頻寬大小

  105.          #limit_rate 40k;

  106.          #限制每個客戶端ip在指定時間內容允許連線的請求數,且超過限制連線頻率的連線數不

  107. 得超過burst指定

  108.          #limit_req zone=reqone burst=50;

  109.          #使用前面定義的共享記憶體區域,限制每個客戶端IP併發連線的最大數

  110.          #limit_conn    reqnum   20;

  111. location / {

  112.              proxy_set_header HTTP_REFERER  $http_referer;

  113.              proxy_pass http://os_server;

  114.              proxy_set_header Host   $host;

  115.              proxy_set_header X-Real-IP      $remote_addr;

  116.              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

  117.              index  index.html index.php index.htm index.jsp;

  118.            }

  119. }


  120. #——————————–m.lqxshop.com———————-

  121. server {

  122.          listen 80;

  123.          #限制每個連線的頻寬大小

  124.          #limit_rate 40k;

  125.          #限制每個客戶端ip在指定時間內容允許連線的請求數,且超過限制連線頻率的連線數不

  126. 得超過burst指定

  127.          #limit_req zone=reqone burst=50;

  128.          #使用前面定義的共享記憶體區域,限制每個客戶端IP併發連線的最大數

  129.          #limit_conn    reqnum   20;

  130.          server_name    m.shop.com m1.shop.com m2.shop.com;

  131. location / {

  132.              proxy_pass http://m_server;

  133.              proxy_set_header HTTP_REFERER  $http_referer;

  134.              proxy_set_header Host   $host;

  135.              proxy_set_header X-Real-IP      $remote_addr;

  136.              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

  137.              index  index.html index.htm index.jsp;

  138.   }

  139. }


  140. #——————————–weixin.lqxshop.com———————-

  141. server {

  142.          listen 80;

  143.           #限制每個連線的頻寬大小

  144.           #limit_rate 40k;

  145.           #限制每個客戶端ip在指定時間內容允許連線的請求數,且超過限制連線頻率的連線數

  146. 得超過burst指定

  147.           #limit_req zone=reqone burst=50;

  148.           #使用前面定義的共享記憶體區域,限制每個客戶端IP併發連線的最大數

  149.          #limit_conn    reqnum   20;

  150.          server_name    weixin.shop.com;

  151. location / {

  152.              proxy_pass http://weixin_server;

  153.              proxy_set_header HTTP_REFERER  $http_referer;

  154.              proxy_set_header Host   $host;

  155.              proxy_set_header X-Real-IP      $remote_addr;

  156.              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

  157.              index  index.html index.htm index.jsp;

  158.   }

  159. }



  160. #——————————–op.lqxshop.com———————-

  161. server {

  162.          listen 80;

  163.           #限制每個連線的頻寬大小

  164.          #limit_rate 30k;

  165.           #限制每個客戶端ip在指定時間內容允許連線的請求數,且超過限制連線頻率的連線數

  166. 得超過burst指定

  167.           #limit_req zone=reqone burst=50;

  168.            #使用前面定義的共享記憶體區域,限制每個客戶端IP併發連線的最大數

  169.          #limit_conn    reqnum   20;

  170.          server_name    op.shop.com;

  171. location / {

  172.              proxy_pass http://op_server;

  173.              proxy_set_header HTTP_REFERER  $http_referer;

  174.              proxy_set_header Host   $host;

  175.              proxy_set_header X-Real-IP      $remote_addr;

  176.              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

  177.              index  index.html index.htm index.jsp;

  178.   }

  179. }


  180. }



  181. 二.APP(Apache+php)搭建

  182. 1、移除系統自帶的rpm包的http mysql php

  183. yum remove httpd mysql mysql-server php php-cli php-common php-devel php-gd  -y

  184. 2 安裝必備的開發包

  185. /usr/bin/yum -y install ntp vim-enhanced gcc gcc-c++ flex bison autoconf automake bzip2-devel

  186. ncurses-devel zlib-devel libjpeg-devel libpng-devel libtiff-devel freetype-devel libXpm-devel

  187. gettext-devel  pam-devel libtool libtool-ltdl openssl openssl-devel fontconfig-devel

  188. libxml2-devel curl-devel  libicu libicu-devel libmcrypt libmcrypt-devel libmhash  libxml2

  189. libmhash-devel  mysql-devel   libxslt libxslt-devel curl tcl expect

    3.同步系統時間:

  190. vim /etc/ntp.conf

  191. server 3.cn.pool.ntp.org

  192. server 3.asia.pool.ntp.org

  193. server 0.asia.pool.ntp.org

  194. chkconfig ntpd on

  195. service ntpd restart

  196. 4.安裝Apache:

  197. ##############安裝需要的外掛:apr、apr-util、pcre、libmcrypt######################################

  198. cd /usr/local/src/

  199. cd apr-1.4.8################(rm: cannot remove `libtoolT`: No such file or directory///////修改configure檔案,+++++把RM=`$RM`改為RM=`$RM  -f`+++++)

  200. ./configure –prefix=/usr/local/apr && make && make install

  201. cd ../apr-util-1.5.2/

  202. ./configure –prefix=/usr/local/apr-util –with-apr=/usr/local/apr/ && make && make install

  203. cd ../pcre-8.33/

  204. ./configure –prefix=/usr/local/pcre –enable-utf8 && make && make install

  205. cd ../libmcrypt-2.5.8/

  206. ./configure –prefix=/usr/local/libmcrypt && make && make install

  207. ###################開始安裝Apache,並修改開啟啟動##################################################

  208. cd ../httpd-2.4.6/

  209. ./configure –prefix=/usr/local/apache –enable-so –enable-rewrite –enable-mods-shared=most –with-mpm=worker –with-apr=/usr/local/apr –with-apr-util=/usr/local/apr-util/ –with-pcre=/usr/local/pcre/

  210. make && make install 

  211. /usr/local/apache/bin/apachectl start

  212. cp /usr/local/apache/bin/apachectl  /etc/init.d/httpd

  213. vim /etc/init.d/httpd(第二行新增註釋資訊,新增為系統服務)

  214. # chkconfig: 35 61 61

  215. # description: Apache

  216. chkconfig –add httpd

  217. chkconfig httpd on

  218. 5.安裝php:

  219. cd ../php-5.5.3/

  220. ./configure –prefix=/usr/local/php –mandir=/usr/local/share/man –infodir=/usr/local/share/info –with-apxs2=/usr/local/apache/bin/apxs –enable-cgi –with-mysql –with-config-file-path=/usr/local/php/etc –with-pdo-mysql –with-mysqli –enable-zip  -enable-sockets –enable-soap –enable-pcntl –enable-mbstring –enable-intl –enable-calendar –enable-bcmath –enable-exif –with-mcrypt –with-mhash –with-gd –with-png-dir –with-jpeg-dir –with-freetype-dir –with-libxml-dir –with-curl –with-zlib   –with-openssl –with-kerberos=shared –with-gettext=shared –with-xmlrpc=shared –with-mcrypt=/usr/local/libmcrypt 

  221. make && make install


  222. 裝載XXX模組 



  223. cd /usr/local/httpd-2.2.22/modules/generators/

  224. /usr/local/apache/bin/apxs -i -a -c mod_cgi.c


  225. 6.apache和php結合:

  226. vim /usr/local/apache/conf/httpd.conf

  227. 查詢AddType application/x-gzip .gz .tgz,在該行下面新增

  228. AddType application/x-httpd-php .php

  229. 查詢DirectoryIndex index.html 把該行修改成

  230. DirectoryIndex index.html index.htm index.php

  231. 測試apache和php是否整合成功,下面我們測試apache和php是否整合成功,在apache文件跟目錄下新建一個小小的php程式

  232. cd /usr/local/apache/htdocs

  233. vi index.php  //在index.php檔案中寫入下面三行

  234. <?php

  235. phpinfo();

  236. ?>

  237. 重啟apache服務

  238. /sbin/service  httpd restart

  239. 訪問,在瀏覽器中輸入http://localhost(ip地址)/index.php 出現php資訊介面則說明整合成功

  240. elinks http://localhost/index.php

  241. 成功的頁面



  1. 三.mysql搭建


  2. 10    11

  3. 軟體原始碼包存放位置               /opt

  4. 軟體解壓位置   /usr/local/src

  5. 原始碼包編譯安裝位置(prefix)         /usr/local/

  6. 指令碼以及維護程式存放位置           /script

  7. 資料庫檔案位置:           /data/mysql/

  8. 資料庫日誌位置:   /data/log

  9. 資料庫備份目錄:   /data/backup


  10. 1.解除安裝預設的mysql及apache的rpm包

  11. yum remove httpd mysql mysql-server php php-cli php-common php-devel php-gd  -y

  12. yum install ntp

  13. service ntpd stop

  14. ntpdate cn.pool.ntp.org

  15. service ntpd start

  16. chkconfig –level 2345 ntpd on


  17. 2.安裝依賴包

  18. yum install gcc gcc-c++ make cmake ncurses-devel vim -y

  19. cd /opt

  20.  tar zxvf cmake-2.8.5.tar.gz -C /usr/local/src

  21.  cd /usr/local/src

  22. cd cmake-2.8.5/

  23. ./configure

  24. make && make install

  25. cd /opt


  26. 3.編譯安裝mysql

  27. tar zxvf mysql-5.5.23.tar.gz -C /usr/local/src/

  28. cd /usr/local/src/mysql-5.5.23

  29.      cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql

  30.          -DMYSQL_DATADIR=/data/mysql

  31.          -DWITH_MYISAM_STORAGE_ENGINE=1

  32.          -DWITH_INNOBASE_STORAGE_ENGINE=1

  33.          -DWITH_ARCHIVE_STORAGE_ENGINE=1

  34.          -DWITH_BLACKHOLE_STORAGE_ENGINE=1

  35.          -DENABLED_LOCAL_INFILE=1

  36.          -DDEFAULT_CHARSET=utf8

  37.          -DDEFAULT_COLLATION=utf8_general_ci

  38.          -DEXTRA_CHARSETS=all  

  39.   make && make install

  40. groupadd -r -g 3306 mysql

  41. useradd -u 3306 -g mysql -r -M -s /sbin/nologin mysql

  42. mkdir -p /data/mysql/

  43. mkdir -p /data/log/

  44. chown -R mysql:mysql /data/

  45. chown -R mysql:mysql /usr/local/mysql/*

  46. cp support-files/my-huge.cnf /etc/my.cnf 


  47. 4.初始化

  48. /usr/local/mysql/scripts/mysql_install_db –user=mysql –basedir=/usr/local/mysql –datadir=/data/mysql &   //初始化資料庫


  49. 5.啟動項

  50. cp support-files/mysql.server /etc/init.d/mysqld

  51. chmod +x /etc/init.d/mysqld

  52. /chkconfig –add mysqld

  53. chkconfig  –level 2345 mysqld on

  54. service  mysqld start

  55. netstat -tnlp |grep 3306


  56. 6.相關命令

  57. # cd /usr/local/bin //進入使用者的預設搜尋路徑下建立mysql命令的軟連線,可以直接執行mysql命令

  58.  #  ln -s /usr/local/mysql/bin/mysql mysql

  59.  #  ln -s /usr/local/mysql/bin/mysqldump mysqldump

  60.  #  ln -s /usr/local/mysql/bin/mysqladmin  mysqladmin

  61.  #  ln -s /usr/local/mysql/bin/mysqlbinlog

  62. vim /etc/my.cnf  ,在[mysqld]配置引數下面新增下面幾行(大概37行下面),根據服務

  63. skip-external-locking

  64. key_buffer_size = 256M

  65. max_allowed_packet = 500M

  66. table_open_cache = 2048

  67. sort_buffer_size = 8M

  68. read_buffer_size = 4M

  69. read_rnd_buffer_size = 16M

  70. myisam_sort_buffer_size = 128M


  71. ###新增

  72. log-error=/data/log/mysql.err.log 

  73. skip-name-resolv 

  74. max_connections = 3000

  75. max_connect_errors = 2000


  76. slow_query_log

  77. long_query_time = 2

  78. slow_query_log_file = /data/log/slow.log 


  79. bulk_insert_buffer_size = 64M

  80. query_cache_type = 1

  81. query_cache_size =256M

  82. query_cache_limit = 4M

  83. ft_min_word_len = 2

  84. join_buffer_size = 16M

  85. innodb_file_per_table  

  86. log-bin-trust-function-creators=1

  87. myisam_repair_threads = 1

  88. innodb_log_files_in_group = 3

  89. thread_cache_size = 8

  90. # Try number of CPU`s*2 for thread_concurrency

  91. thread_concurrency = 8


  92. innodb_buffer_pool_size = 2G //修改這個引數為實體記憶體的80%


  93. :wq //儲存退出

  94. rm -rvf  /data/mysql/ib* myslq-bin* //刪除原來的預設檔案

  95. service mysqld restart

  96. mysql -u root

  97. Mysql> grant all privileges on *.* to lqx@`%` identified by `shlqx1!`;

  98. Mysql>flush privileges;





     本文轉自yzy121403725 51CTO部落格,原文連結:http://blog.51cto.com/lookingdream/1861511,如需轉載請自行聯絡原作者