簡介: Lnmp 環境的搭建還是非常簡單的,之前由於博客遷移等原因,導致丟失了好多博文,這次重新整理記錄一下。 Lnmp 即:Linux 、Nginx 、Mysql 、PHP Lnmp 是一套 Web 環境,Linux 作為底層操作系統,Nginx 提供 web 服務,Mysql 提供資料庫服務,P ...
簡介:
Lnmp 環境的搭建還是非常簡單的,之前由於博客遷移等原因,導致丟失了好多博文,這次重新整理記錄一下。
Lnmp 即:Linux 、Nginx 、Mysql 、PHP
Lnmp 是一套 Web 環境,Linux 作為底層操作系統,Nginx 提供 web 服務,Mysql 提供資料庫服務,PHP 負責解析 PHP 代碼。
強烈建議宿主機記憶體大於、等於 1G ,否則建議還是安裝低版本的 Mysql 跟 PHP !!!
一、Nginx
下載地址:http://nginx.org/download/nginx-1.8.0.tar.gz # 現在最新穩定版已經發展到了 1.8.0
shell > yum -y install gcc gcc-c++ make wget zlib-devel pcre-devel openssl-devel shell > wget http://nginx.org/download/nginx-1.8.0.tar.gz shell > tar zxf nginx-1.8.0.tar.gz shell > cd nginx-1.8.0 shell > ./configure --prefix=/usr/local/nginx ; make ; make install # --prefix 參數指定 Nginx 安裝路徑 shell > /usr/local/nginx/sbin/nginx # 啟動 Nginx shell > netstat -anpt | grep nginx # 確認是否啟動 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 7520/nginx shell > curl -I http://127.0.0.1 # 訪問成功 HTTP/1.1 200 OK Server: nginx/1.8.0 Date: Fri, 07 Aug 2015 12:08:14 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Fri, 07 Aug 2015 12:04:38 GMT Connection: keep-alive ETag: "55c49ed6-264" Accept-Ranges: bytes shell > iptables -I INPUT -p tcp --dport 80 -j ACCEPT shell > service iptables save
## 至此 Nginx 就可以外網通過瀏覽器訪問了
二、Mysql ( 傳送門:http://www.cnblogs.com/wangxiaoqiangs/p/5336048.html )
三、PHP
下載地址:http://cn2.php.net/distributions/php-5.6.11.tar.gz
shell > yum -y install epel-release shell > yum -y install gd-devel libtool libjpeg-devel libpng-devel freetype-devel libxml2-devel zlib-devel bzip2-devel libcurl-devel libxslt-devel openssl-devel glibc-devel glib2-devel libmcrypt-devel shell > wget http://cn2.php.net/distributions/php-5.6.11.tar.gz shell > tar zxf php-5.6.11.tar.gz shell > cd php-5.6.11 shell > ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php \ --with-mysql=/usr/local/mysql/ --with-mysqli=/usr/local/mysql/bin/mysql_config \ --with-gd --with-xsl --with-bz2 --with-zlib --with-curl --with-pear --without-iconv --with-mcrypt \ --with-gettext --with-openssl --with-libxml-dir --with-png-dir --with-jpeg-dir --with-freetype-dir \ --with-libdir=lib64 --enable-ftp --enable-fpm --enable-exif --enable-soap --enable-bcmath --enable-calendar \ --enable-sockets --enable-mbstring --enable-gd-native-ttf --disable-rpath --disable-debug
# 這堆參數怎麼說呢: 你也不必太在意,能解決大多數需求,有幾個需要解釋一下
# --prefix= 指定安裝路徑
# --with-config-file-path 指定 php.ini 存放位置
# --with-mysql 指定 Mysql 安裝路徑
# --enable-fpm Nginx 連接 php 全靠這個東西,如果沒有它,你的 Nginx 就不能解析 php
shell > make ; make install shell > cp /usr/local/src/php-5.6.11/php.ini-production /usr/local/php/php.ini shell > cp /usr/local/src/php-5.6.11/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm shell > chmod a+x /etc/init.d/php-fpm shell > cat /usr/local/php/etc/php-fpm.conf.default > /usr/local/php/etc/php-fpm.conf shell > chkconfig --add php-fpm # 加入開機啟動 shell > chkconfig --level 35 php-fpm on shell > service php-fpm start # 啟動 php-fpm shell > netstat -anpt | grep php-fpm tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 71992/php-fpm
四、讓 Nginx 支持 PHP
shell > vim /usr/local/nginx/conf/nginx.conf # 修改配置文件,使其可以解析 PHP server { listen 80; server_name localhost; location / { root html; index index.php index.html index.htm; # 加入 index.php } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location ~ \.php$ { # 整段註釋去掉 root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; # 修改為自己的根目錄 include fastcgi_params; } }
# 當然,這隻是讓 Nginx 支持 PHP 而已,實際工作中可以還有更多的配置
## 創建測試頁面 ( echo "<?php phpinfo(); ?>" > /usr/local/nginx/html/info.php ) , 重啟 Nginx 、php-fpm 放問測試!( 好久前測試記得需要 iptables -I INPUT -i io -j ACCEPT )
五、報錯彙總:( 記錄一些常見的錯誤及解決方法 )
1、沒有安裝 gcc 導致報錯 ( yum -y install gcc )
checking for OS + Linux 2.6.32-504.el6.x86_64 x86_64 checking for C compiler ... not found ./configure: error: C compiler cc is not found
2、沒有安裝 pcre-devel 導致報錯 ( yum -y install pcre-devel )
./configure: error: the HTTP rewrite module requires the PCRE library. You can either disable the module by using --without-http_rewrite_module option, or install the PCRE library into the system, or build the PCRE library statically from the source with nginx by using --with-pcre=<path> option.
3、沒有安裝 zlib-devel 導致報錯 ( yum -y install zlib-devel )
./configure: error: the HTTP gzip module requires the zlib library. You can either disable the module by using --without-http_gzip_module option, or install the zlib library into the system, or build the zlib library statically from the source with nginx by using --with-zlib=<path> option.
# 上面是關於 Nginx 的報錯
4、沒有安裝 gcc-c++ 導致報錯 ( yum -y install gcc-c++ )
CMake Error: your CXX compiler: "CMAKE_CXX_COMPILER-NOTFOUND" was not found. Please set CMAKE_CXX_COMPILER to a valid compiler path or name.
# 上面是關於 Mysql 的報錯
5、沒有安裝 libxml2-devel 導致報錯 ( yum -y install libxml2-devel )
configure: error: xml2-config not found. Please check your libxml2 installation.
6、沒有安裝 bzip2-devel 導致報錯 ( yum -y install bzip2-devel )
configure: error: Please reinstall the BZip2 distribution
7、沒有安裝 libcurl-devel 導致報錯 ( yum -y install libcurl-devel )
configure: error: Please reinstall the libcurl distribution - easy.h should be in <curl-dir>/include/curl/
8、沒有安裝 libjpeg-devel 導致報錯 ( yum -y install libjpeg-devel )
configure: error: jpeglib.h not found.
9、沒有安裝 libpng-devel 導致報錯 ( yum -y install libpng-devel )
configure: error: png.h not found.
10、沒有安裝 freetype-devel 導致報錯 ( yum -y install freetype-devel )
configure: error: freetype-config not found.
11、沒有安裝 libmcrypt-devel 導致報錯 ( yum -y install epel-release ; yum -y install libmcrypt-devel )
configure: error: mcrypt.h not found. Please reinstall libmcrypt.
12、找不到 libmysqlclient.so.18 導致報錯 ( ln -s /usr/local/mysql/lib /usr/local/mysql/lib64 )
configure: error: Cannot find libmysqlclient under /usr/local/mysql/. Note that the MySQL client library is not bundled anymore!
13、跟上一個錯誤有連帶性,我猜的 ( ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/ )
configure: error: Don't know how to define struct flock on this system, set --enable-opcache=no
14、沒有安裝 libxslt-devel 導致報錯 ( yum -y install libxslt-devel )
configure: error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution