天天看點

LNAMP源碼安裝整合加論壇及動靜分離

LNAMP(Linux+Nginx+Apache+Mysql+PHP)架構受到很多IT企業的青睐,取代了原來認為很好的LNMP(Linux+Nginx+Mysql+PHP)架構,那我們說LNAMP到底有什麼優點呢,還得從Nginx和apache的優缺點說起。

Nginx處理靜态檔案能力很強,Apache處理動态檔案很強而且很穩定,把二者綜合在一塊,性能提升很多倍。可能很多Linux SA在從事LNMP運維中,會發現PHP(FastCGI)模式會出現一些502錯誤的現象,這是因為Nginx+PHP(FastCGI)組合不穩定的原因造成的。

  實驗機:

<code>[root@192_168_77_189 ~]</code><code># uname -a</code>

<code>Linux 192_168_77_189 2.6.32-504.el6.x86_64 </code><code>#1 SMP Wed Oct 15 04:27:16 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux</code>

<code>[root@192_168_77_189 ~]</code><code># cat /etc/issue</code>

<code>CentOS release 6.6 (Final)</code>

<code>Kernel \r on an \m</code>

源碼安裝LNAMP之Nginx

<code>[root@192_168_77_189 ~]</code><code># yum install pcre pcre-devel -y      #依賴包</code>

<code>[root@192_168_77_189 autolamp]</code><code># tar -xzvf nginx-1.8.0.tar.gz </code>

<code>[root@192_168_77_189 nginx-1.8.0]</code><code># ./configure  --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module</code>

<code>[root@192_168_77_189 nginx-1.8.0]</code><code># make &amp;&amp; make install</code>

<code>[root@192_168_77_189 nginx-1.8.0]</code><code># /usr/local/nginx/sbin/nginx</code>

<code>[root@192_168_77_189 nginx-1.8.0]</code><code># ps -ef | grep nginx    </code>

<code>root      9982     1  0 19:38 ?        00:00:00 nginx: master process </code><code>/usr/local/nginx/sbin/nginx</code>

<code>nobody    9983  9982  0 19:38 ?        00:00:00 nginx: worker process      </code>

<code>root      9987  7577  0 19:38 pts</code><code>/1</code>    <code>00:00:00 </code><code>grep</code> <code>nginx</code>

源碼安裝LNAMP之Apache

<code>[root@192_168_77_189 autolamp]</code><code># yum -y install epel-release </code>

<code>[root@192_168_77_189 apr-1.5.2]</code><code># ./configure --prefix=/usr/local/apr</code>

<code>[root@192_168_77_189 apr-1.5.2]</code><code># make &amp;&amp; make install</code>

<code>[root@192_168_77_189 apr-util-1.5.2]</code><code># ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr</code>

<code>[root@192_168_77_189 apr-util-1.5.2]</code><code># make &amp;&amp; make install</code>

<code>[root@192_168_77_189 httpd-2.4.16]</code><code># ./configure --prefix=/usr/local/apache  --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --enable-modules=most --enable-mpms-shared=all --with-mpm=event</code>

<code>[root@192_168_77_189 httpd-2.4.16]</code><code># make &amp;&amp; make install</code>

源碼安裝LNAMP之MySQL

<code> </code><code>yum </code><code>install</code> <code>gcc gcc-c++ </code><code>make</code> <code>cmake  ncurses-devel bison perl</code>

<code> </code> 

<code> </code><code>[root@192_168_77_189 mysql-5.5.44]</code><code># cmake -DCMAKE_INSTALL_PREFIX=/data/app/mysql3306 -DMYSQL_DATADIR=/data/mysql3306  -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_DEBUG=0 -DMYSQL_USER=mysql -DMYSQL_UNIX_ADDR=/data/mysql3306/mysql3306.sock</code>

<code> </code><code>[root@192_168_77_189 mysql-5.5.44]</code><code>#  make &amp;&amp; make install</code>

<code> </code><code>[root@192_168_77_189 mysql-5.5.44]</code><code># cp -a support-files/my-large.cnf /data/mysql3306/my.cnf</code>

<code> </code><code>[root@192_168_77_189 data]</code><code># chown mysql.mysql app/ -R</code>

<code>[root@192_168_77_189 data]</code><code># chown mysql.mysql mysql3306/ -R </code>

<code>[root@192_168_77_189 mysql3306]</code><code># cp -a support-files/mysql.server /etc/init.d/mysqld</code>

<code>[root@192_168_77_189 mysql3306]</code><code># cd /data/app/mysql3306/ </code>

<code>[root@192_168_77_189 mysql3306]</code><code># ./scripts/mysql_install_db --basedir=/data/app/mysql3306/ --datadir=/data/mysql3306/ --user=mysql</code>

<code>[root@192_168_77_189 mysql3306]</code><code>#  echo "export PATH="\$PATH":/data/app/mysql3306/bin/" &gt;&gt;/root/.bash_profile &amp;&amp; source /root/.bash_profile </code>

<code>[root@192_168_77_189 mysql3306]</code><code># /etc/init.d/mysqld restart</code>

<code>Shutting down MySQL. SUCCESS! </code>

<code>Starting MySQL.. SUCCESS!</code>

源碼安裝LNAMP之PHP

<code>yum </code><code>install</code> <code>libxml2-devel  libmcrypt-devel </code><code>bzip2</code><code>-devel </code>

<code>libxml2-devel openssl-devel  </code><code>bzip2</code> <code>bzip2</code><code>-devel</code>

<code>[root@192_168_77_189 php-5.4.42]</code><code># ./configure --prefix=/usr/local/php --with-mysql=/data/app/mysql3306 --with-openssl --with-mysqli=/data/app/mysql3306/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --enable-fpm --with-mcrypt --with-bz2 --with-config-file-path=/etc/php/php.ini --with-config-file-scan-dir=/etc/php/php.d/ --with-apxs2=/usr/local/apache/bin/apxs</code>

<code>[root@192_168_77_189 php-5.4.42]</code><code># make &amp;&amp; make install</code>

源碼安裝Apache+PHP整合

<code>[root@192_168_77_189 php-5.4.42]</code><code># echo "Addtype application/x-httpd-php  .php" &gt;&gt; /usr/local/apache/conf/httpd.conf</code>

<code>[root@192_168_77_189 php-5.4.42]</code><code># sed -i 's/DirectoryIndex index.html/DirectoryIndex index.php index.html/g' /usr/local/apache/conf/httpd.conf</code>

<code>[root@192_168_77_189 php-5.4.42]</code><code># /usr/local/apache/bin/apachectl -t</code>

<code>  </code><code>00557: httpd: apr_sockaddr_info_get() failed </code><code>for</code> <code>192_168_77_189</code>

<code>AH00558: httpd: Could not reliably determine the server</code><code>'s fully qualified domain name, using 127.0.0.1. Set the '</code><code>ServerName' directive globally to suppress this message</code>

<code>Syntax OK</code>

<code>ServerName www.example.com:80改成ServerName localhost:80即可</code>

<code>[root@192_168_77_189 conf]</code><code># /usr/local/apache/bin/apachectl -t</code>

<code>[root@192_168_77_189 conf]</code><code># ps -ef | grep http</code>

<code>root     23847     1  0 21:05 ?        00:00:00 </code><code>/usr/local/apache/bin/httpd</code> <code>-k restart</code>

<code>daemon   23849 23847  0 21:05 ?        00:00:00 </code><code>/usr/local/apache/bin/httpd</code> <code>-k restart</code>

<code>daemon   23850 23847  0 21:05 ?        00:00:00 </code><code>/usr/local/apache/bin/httpd</code> <code>-k restart</code>

<code>daemon   23851 23847  0 21:05 ?        00:00:00 </code><code>/usr/local/apache/bin/httpd</code> <code>-k restart</code>

<code>daemon   23937 23847  0 21:06 ?        00:00:00 </code><code>/usr/local/apache/bin/httpd</code> <code>-k restart</code>

<code>root     23973 22611  0 21:10 pts</code><code>/1</code>    <code>00:00:00 </code><code>grep</code> <code>http</code>

<code>[root@192_168_77_189 ~]</code><code># cd /usr/local/apache/htdocs/</code>

<code>在index.php裡添加</code>

<code>&lt;?php</code>

<code>phpinfo();</code>

<code>?&gt;</code>

<code>~  在用戶端輸入http:</code><code>//192</code><code>.168.77.189/ 測試得到以下頁面</code>

<a href="http://s3.51cto.com/wyfs02/M02/71/61/wKiom1XMm_Lg5430AAMujtnCFLI513.jpg" target="_blank"></a>

源碼安裝DISCUZ論壇

下載下傳discuz源碼封包件

<code>[root@192_168_77_189 ~]</code><code># wget </code>

<code>[root@192_168_77_189 autolamp]</code><code># unzip Discuz_X3.1_SC_UTF8.zip -d /usr/local/apache/htdocs/</code>

<code>[root@192_168_77_189 autolamp]</code><code># cd /usr/local/apache/htdocs/;mv upload/* .</code>

<code>[root@192_168_77_189 htdocs]</code><code># chmod 777 -R data/ uc_server/ config/ uc_client/</code>

<code>然後通路http:</code><code>//192</code><code>.168.77.189/安裝discuz論壇,如下圖,選擇“我同意”</code>

<a href="http://s3.51cto.com/wyfs02/M02/71/5D/wKioL1XMn4yQ0ngfAAQc0AQNcxo275.jpg" target="_blank"></a>

<a href="http://s3.51cto.com/wyfs02/M02/71/61/wKiom1XMnfyywpiqAAH_gr_v1HU013.jpg" target="_blank"></a>

資料庫授權:

<code>[root@192_168_77_189 ~]</code><code># /data/app/mysql3306/bin/mysql   登入,這裡資料庫沒有設定密碼</code>

<code>mysql&gt; create database discuz charset=utf8;</code>

<code>grant all on discuz.* to  identified by </code><code>"123456"</code><code>;   授權</code>

然後在論壇頁面輸入賬号資訊一路下一步就安裝成功了。。。。。。。。。。

<a href="http://s3.51cto.com/wyfs02/M00/71/5D/wKioL1XMoeziRHm9AAJw-arhG04871.jpg" target="_blank"></a>

自此LAMP環境整合并搭建成功,那如何使用Nginx來整合LAMP呢?

源碼安裝Nginx+LAMP整合

先修改apache通路端口為8080,Nginx端口預設為80

然後修改nginx配置檔案: vi/usr/local/nginx/conf/nginx.conf,server配置段内容如下:

(定義upstream均衡子產品,配置動靜分離,動态轉發至apache,靜态檔案直接本地響應),這裡配置的有動靜分離,,動态走app_lamp;子產品靜态走本地目錄,,由于一台機器上實作,有點局限性,了解:

<code>upstreamapp_lamp {</code>

<code>    </code><code>server  127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=30s;</code>

<code>    </code><code>}</code>

<code>    </code><code>server {</code>

<code>        </code><code>listen       80;</code>

<code>        </code><code>server_name  localhost;</code>

<code>        </code><code>location / {</code>

<code>            </code><code>root   </code><code>/usr/local/apache/htdocs</code><code>;</code>

<code>            </code><code>index index.php index.html index.htm;</code>

<code>        </code><code>}</code>

<code>        </code><code>location ~ .*\.(php|jsp|cgi)?$</code>

<code>    </code><code>{</code>

<code>         </code><code>proxy_set_header Host  $host;</code>

<code>         </code><code>proxy_set_header X-Real-IP$remote_addr;</code>

<code>         </code><code>proxy_set_header X-Forwarded-For$proxy_add_x_forwarded_for;</code>

<code>         </code><code>proxy_pass http:</code><code>//app_lamp</code><code>;</code>

<code>        </code><code>location ~.*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$</code>

<code>       </code><code>{</code>

<code>        </code><code>root </code><code>/usr/local/apache/htdocs</code><code>;</code>

<code>        </code><code>expires      3d;</code>

<code>      </code><code>}</code>

自此,LNAMP源碼全部整合完畢

軟體包太大無法上傳。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

本文轉自 Anonymous123 51CTO部落格,原文連結:http://blog.51cto.com/woshitieren/1684485