天天看點

centos LAMP環境搭建

說明:

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

一.nginx搭建

1.下載下傳源碼包nginx-1.4.2

2.安裝nginx

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

3.配置檔案如下

user  nobody;

worker_processes  8;

pid               logs/nginx.pid;

events {

        use epoll;

        worker_connections  65535;

        }

http {

#       設定一個共享記憶體區域reqnum,以用于限制每個用戶端IP可以并發連接配接的請求數

#        limit_conn_zone $binary_remote_addr zone=reqnum:10m;

#       每個用戶端IP每分鐘隻允許5個連接配接

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

        include       mime.types;

        default_type  application/octet-stream;

        sendfile              on;  

        tcp_nopush            on;   

        tcp_nodelay           on; 

        keepalive_timeout     60; 

        server_tokens         off;

        log_format    wwwlog  '$remote_addr - $remote_user [$time_local] "$request" '              

                              '$status $body_bytes_sent "$http_referer" '             

                              '"$http_user_agent" $http_x_forwarded_for';    

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

        gzip                    on; 

        gzip_min_length         1k; 

        gzip_buffers            4 16k; 

        gzip_http_version       1.1; 

        gzip_comp_level         3; 

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

on/xml; 

        gzip_vary on;

        server_names_hash_bucket_size   128;   

        client_header_buffer_size       32k;  

        client_max_body_size            300m; 

        client_body_buffer_size         512k; 

        large_client_header_buffers     4   32k;  

        proxy_connect_timeout           60; 

        proxy_send_timeout              120; 

        proxy_read_timeout              120; 

        proxy_buffer_size               16k; 

        proxy_buffers                   4   64k; 

        proxy_busy_buffers_size         128k; 

        proxy_temp_file_write_size      128k; 

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

t.html index.php3;

upstream os_server {

#                       ip_hash;

#                       server 192.168.201.122:80 weight=1;

                        server 192.168.201.123:80 weight=1;

                        server 192.168.201.124:80 weight=1;

                        server 192.168.201.125:80 weight=1;

                        server 192.168.201.126:80 weight=1;

                        server 192.168.201.127:80 weight=1;

                        server 192.168.201.128:80 weight=1;

                        server 192.168.201.129:80 weight=1;

                        server 192.168.201.130:80 weight=1;

                        server 192.168.201.131:80 weight=1;

                        server 192.168.201.132:80 weight=1;

                        server 192.168.201.133:80 weight=1;

                        server 192.168.201.134:80 weight=1;

                        server 192.168.201.136:80 weight=1;

                        server 192.168.201.137:80 weight=1;

                        server 192.168.201.138:80 weight=1;

                                     }  

upstream m_server {

                        }

upstream weixin_server {

                        server 192.168.201.35:80 weight=1;

                        server 192.168.201.34:80 weight=1;

upstream op_server {    

#                       server 192.168.201.34:80 weight=1;

#---------------------------www.lqxshop.com-----------------------

server {

         listen 80;

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

om;

         #限制每個連接配接的帶寬大小

         #limit_rate 40k;

         #限制每個用戶端ip在指定時間内容允許連接配接的請求數,且超過限制連接配接頻率的連接配接數不

得超過burst指定

         #limit_req zone=reqone burst=50;

         #使用前面定義的共享記憶體區域,限制每個用戶端IP并發連接配接的最大數

         #limit_conn    reqnum   20;

location / {

             proxy_set_header HTTP_REFERER  $http_referer;

             proxy_pass http://os_server;

             proxy_set_header Host   $host;

             proxy_set_header X-Real-IP      $remote_addr;

             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

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

           }

}

#--------------------------------m.lqxshop.com----------------------

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

             proxy_pass http://m_server;

             index  index.html index.htm index.jsp;

  }

#--------------------------------weixin.lqxshop.com----------------------

          #限制每個連接配接的帶寬大小

          #limit_rate 40k;

          #限制每個用戶端ip在指定時間内容允許連接配接的請求數,且超過限制連接配接頻率的連接配接數

          #limit_req zone=reqone burst=50;

          #使用前面定義的共享記憶體區域,限制每個用戶端IP并發連接配接的最大數

         server_name    weixin.shop.com;

             proxy_pass http://weixin_server;

#--------------------------------op.lqxshop.com----------------------

         #limit_rate 30k;

           #使用前面定義的共享記憶體區域,限制每個用戶端IP并發連接配接的最大數

         server_name    op.shop.com;

             proxy_pass http://op_server;

二.APP(Apache+php)搭建

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

2 安裝必備的開發包

3.同步系統時間:

4.安裝Apache:

##############安裝需要的插件:apr、apr-util、pcre、libmcrypt######################################

cd /usr/local/src/

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

./configure --prefix=/usr/local/apr && make && make install

cd ../apr-util-1.5.2/

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

cd ../pcre-8.33/

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

cd ../libmcrypt-2.5.8/

./configure --prefix=/usr/local/libmcrypt && make && make install

###################開始安裝Apache,并修改開啟啟動##################################################

cd ../httpd-2.4.6/

./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/

make && make install 

/usr/local/apache/bin/apachectl start

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

vim /etc/init.d/httpd(第二行添加注釋資訊,添加為系統服務)

# chkconfig: 35 61 61

# description: Apache

chkconfig --add httpd

chkconfig httpd on

5.安裝php:

cd ../php-5.5.3/

./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 

make && make install

裝載XXX子產品 

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

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

6.apache和php結合:

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

查找AddType application/x-gzip .gz .tgz,在該行下面添加

AddType application/x-httpd-php .php

查找DirectoryIndex index.html 把該行修改成

DirectoryIndex index.html index.htm index.php

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

cd /usr/local/apache/htdocs

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

<?php

phpinfo();

?>

重新開機apache服務

/sbin/service  httpd restart

通路,在浏覽器中輸入http://localhost(ip位址)/index.php 出現php資訊界面則說明整合成功

elinks http://localhost/index.php

成功的頁面

三.mysql搭建

10    11

軟體源代碼包存放位置               /opt

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

源碼包編譯安裝位置(prefix)         /usr/local/

腳本以及維護程式存放位置           /script

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

資料庫日志位置:   /data/log

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

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

yum install ntp

service ntpd stop

ntpdate cn.pool.ntp.org

service ntpd start

chkconfig --level 2345 ntpd on

2.安裝依賴包

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

cd /opt

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

 cd /usr/local/src

cd cmake-2.8.5/

./configure

3.編譯安裝mysql

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

cd /usr/local/src/mysql-5.5.23

     cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \

         -DMYSQL_DATADIR=/data/mysql \

         -DWITH_MYISAM_STORAGE_ENGINE=1 \

         -DWITH_INNOBASE_STORAGE_ENGINE=1 \

         -DWITH_ARCHIVE_STORAGE_ENGINE=1 \

         -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \

         -DENABLED_LOCAL_INFILE=1 \

         -DDEFAULT_CHARSET=utf8 \

         -DDEFAULT_COLLATION=utf8_general_ci \

         -DEXTRA_CHARSETS=all  

  make && make install

groupadd -r -g 3306 mysql

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

mkdir -p /data/mysql/

mkdir -p /data/log/

chown -R mysql:mysql /data/

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

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

4.初始化

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

5.啟動項

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

chmod +x /etc/init.d/mysqld

/chkconfig --add mysqld

chkconfig  --level 2345 mysqld on

service  mysqld start

netstat -tnlp |grep 3306

6.相關指令

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

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

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

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

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

vim /etc/my.cnf  ,在[mysqld]配置參數下面添加下面幾行(大概37行下面),根據服務

skip-external-locking

key_buffer_size = 256M

max_allowed_packet = 500M

table_open_cache = 2048

sort_buffer_size = 8M

read_buffer_size = 4M

read_rnd_buffer_size = 16M

myisam_sort_buffer_size = 128M

###添加

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

skip-name-resolv 

max_connections = 3000

max_connect_errors = 2000

slow_query_log

long_query_time = 2

slow_query_log_file = /data/log/slow.log 

bulk_insert_buffer_size = 64M

query_cache_type = 1

query_cache_size =256M

query_cache_limit = 4M

ft_min_word_len = 2

join_buffer_size = 16M

innodb_file_per_table  

log-bin-trust-function-creators=1

myisam_repair_threads = 1

innodb_log_files_in_group = 3

thread_cache_size = 8

# Try number of CPU's*2 for thread_concurrency

thread_concurrency = 8

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

:wq //儲存退出

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

service mysqld restart

mysql -u root

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

Mysql>flush privileges;

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