centos 源碼編譯安裝lnmp(linux + nginx1.5.5 + mysql5.5.21 + php5.3.27)

來源:互聯網
上載者:User

一:安裝相關相依元件

先yum安裝基本的相依元件:

yum install make apr* autoconf automake gcc gcc-c++ zlib-devel openssl openssl-devel pcre-devel gd kernel keyutils patch perl kernel-headers compat* mpfr cpp glibc libgomp libstdc++-devel ppl cloog-ppl keyutils-libs-devel libcom_err-devel libsepol-devel libselinux-devel krb5-devel zlib-devel libXpm* freetype libjpeg* libpng* php-common php-gd ncurses* libtool* libxml2 libxml2-devel patch

(yum安裝過後基本的組件已安好,以下編譯安裝可以安裝最新版的組件)

1:安裝zlib

zlib 官網:http://zlib.net


wget http://zlib.net/zlib-1.2.8.tar.gz
tar -xvzf zlib-1.2.8.tar.gz
cd zlib-1.2.8.tar.gz
./configure
make
make install

2:安裝freetype

wget http://download.savannah.gnu.org/releases/freetype/freetype-2.5.0.1.tar.gz
cd /usr/local/src
tar -zxvf freetype-2.5.0.1.tar.gz
cd freetype-2.5.0.1
./configure --prefix=/usr/local/freetype
make
make install
make 的時候可能出現錯誤:
/usr/include/libpng12/pngconf.h:336: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
/usr/include/libpng12/pngconf.h:337: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘include’
make: *** [/usr/local/src/freetype-2.5.0.1/objs/sfnt.lo] Error 1
解決辦法:
./configure –prefix=/usr/local/freetype –without-png(加上這個選項就行了)

3:安裝libpng


wget http://sourceforge.net/projects/libpng/files/libpng12/older-releases/1.2.31/libpng-1.2.31.tar.gz/download
cd /usr/local/src
tar -zxvf libpng-1.2.31.tar.gz
cd libpng-1.2.31
./configure
make
make install

4:安裝libxml2


wget http://xmlsoft.org/sources/old/libxml2-2.7.1.tar.gz
cd /usr/local/src
tar -xzvf libxml2-2.7.1.tar.gz
cd libxml2-2.7.1
./configure --prefix=/usr/local/libxml2 --without-zlib (記得加上後面這句,不然會出錯)
make
make install

5:安裝libmcypt


wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz
cd /usr/local/src
tar zxvf libmcrypt-2.5.7.tar.gz
cd libmcrypt-2.5.7
./configure --prefix=/usr/local/libmcrypt
mak
make install

6:安裝gd2(php的gd庫,壓縮和處理圖片會用到)


wget http://ring.u-toyama.ac.jp/archives/graphics/gd/gd-2.0.35.tar.gz
cd /usr/local/src
tar -zxvf gd-2.0.35.tar.gz
mkdir -p /usr/local/gd
cd gd-2.0.35
./configure --prefix=/usr/local/gd --enable-m4_pattern_allow --with-jpeg --with-png --with-zlib --with-freetype=/usr/local/freetype --with-libmcrypt=/usr/local/libmcrypt
make
make instal
(–with-jpeg:使其支援jpeg圖片處理;–with-png:支援png圖片處理)
編譯過程可能出現錯誤:
就先編譯安裝下gettext

wget http://ftp.gnu.org/pub/gnu/gettext/gettext-0.18.3.1.tar.gz
tar xzf gettext-0.18.3.1.tar.gz
cd gettext-0.18.3.1
./configure
make
make install
注意:gd庫版本如果是gd-2.0.35以上,則需要 ./configure –enable-m4_pattern_allow或者編譯安裝 gettext組件後再重新編譯gd

7:安裝openssl

wget https://www.openssl.org/source/openssl-1.0.1c.tar.gz  #openssldir 是設定檔目錄,建議安裝兩次,shared 作用是產生動態串連庫。
tar -zxf openssl-1.0.1c.tar.gz
cd openssl-1.0.1c/
./config --prefix=/usr/local/openssl --openssldir=/usr/local/openssl/ssl
make
make install
./config shared --prefix=/usr/local/openssl --openssldir=/usr/local/openssl/ssl
make clean
make
make install

8:安裝curl


wget http://curl.haxx.se/download/curl-7.17.1.tar.gz
tar -zxvf curl-7.17.1.tar.gz
cd curl-7.17.1
./configure --prefix=/usr/local/curl
make
make install
如果出現錯誤:
curl.so -lz -lrt -Wl,-rpath -Wl,/usr/local/curl/lib
../lib/.libs/libcurl.so: undefined reference to `ERR_remove_thread_state’
collect2: ld returned 1 exit status
make[2]: *** [curl] Error 1
make[2]: Leaving directory `/usr/local/src/curl-7.32.0/src’
make[1]: *** [all] Error 2
make[1]: Leaving directory `/usr/local/src/curl-7.32.0/src’
make: *** [all-recursive] Error 1
建議安裝舊點的版本,特別是如果openssl的版本是1.0以下

二:安裝nginx需要的組件

1:安裝pcre

wget http://sourceforge.net/projects/pcre/files/pcre/8.33/pcre-8.33.tar.gz/download
tar -zxvf pcre-8.33.tar.gz
cd pcre-8.33
./configure --enable-utf8
make
make install

三:安裝nginx


wget http://nginx.org/download/nginx-1.5.5.tar.gz
tar xf nginx-1.5.5.tar.gz
cd nginx-1.5.5
./configure --prefix=/usr/local/nginx --without-http_memcached_module --user=www --group=www --with-http_stub_status_module --with-openssl=/usr/local/openssl --with-pcre=/usr/local/src/pcre-8.33
#注意:--with-pcre=/usr/local/src/pcre-8.31指向的是源碼包解壓的路徑,而不是安裝的路徑,否則會報錯
make
make install

添加nginx開機啟動指令碼:


#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
# By www.osyunwei.com
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}

start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}

stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}

restart() {
#configtest || return $?
stop
sleep 1
start
}

reload() {
#configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}

force_reload() {
restart
}

configtest() {
$nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
status $prog
}

rh_status_q() {
rh_status >/dev/null 2>&1
}

case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
#=======================================================
wq!#儲存退出
chmod 775 /etc/rc.d/init.d/nginx #賦予檔案執行許可權
chkconfig nginx on #設定開機啟動
/etc/rc.d/init.d/nginx restart
service nginx restart

四:安裝mysql

mysql官網:http://dev.mysql.com/downloads/ 下載源碼包

groupadd mysql #添加mysql組
useradd -g mysql mysql -s /bin/false #建立使用者mysql並加入到mysql組,不允許mysql使用者直接登入系統
mkdir -p /usr/local/mysql #建立MySQL安裝目錄
mkdir -p /usr/local/mysql/data #建立MySQL資料存放目錄
tar zxvf mysql-5.5.21.tar.gz
cd mysql-5.5.21
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DSYSCONFDIR=/etc -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_TCP_PORT=3306 -DWITH_EXTRA_CHARSETS:STRING=utf8,gbk -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_DEBUG=0 -DWITH_READLINE=1 -DWITH_SSL=yes
#(有時候會出現找不到/usr/local/mysql/data 等警告,不管他,直接繼續編譯)
make
make install
cd /usr/local/mysql
cp ./support-files/my-huge.cnf /etc/my.cnf #提示檔案存在的話直接覆蓋
vim /etc/my.cnf #在 [mysqld] 部分增加
datadir =/usr/local/mysql/data #資料庫存放路徑
./scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql #產生mysql系統資料庫
cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld #把Mysql加入系統啟動
chmod 755 /etc/init.d/mysqld #增加執行許可權
chkconfig mysqld on #加入開機啟動
vim /etc/rc.d/init.d/mysqld #編輯
basedir = /usr/local/mysql #MySQL程式安裝路徑
datadir = /usr/lcoal/mysql/data #MySQl資料庫存放目錄
service mysqld start #啟動
/usr/local/mysql/bin/mysql -u root –p # 登入mysql

#安裝成功後進行root使用者密碼修改:
/usr/local/mysql/bin/mysqladmin -u root -p password "新密碼" #斷行符號後提示輸入舊密碼,輸入成功後密碼修稿成功
/etc/init.d/mysqld restart #重啟mysql,安裝成功!

五:安裝php


wget http://cn2.php.net/get/php-5.3.27.tar.gz/from/ar2.php.net/mirror
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-mysql-sock=/tmp/mysql.sock --with-pdo-mysql=/usr/local/mysql --with-gd=/usr/local/gd  --with-png-dir=/usr/lib --with-jpeg-dir=/usr/local/jpeg9 --with-freetype-dir=/usr/local/freetype --with-iconv --with-zlib --enable-xml --enable-magic-quotes --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curlwrappers --enable-mbregex --enable-fpm --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl=/usr/local/openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --enable-session --with-mcrypt=/usr/local/libmcrypt --with-curl=/usr/local/curl --enable-ctype
make
make install
(–with-jpeg-dir 使用yum安裝的預設版本)

cp php.ini-production /usr/local/php/etc/php.ini
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
vim /usr/local/php/etc/php-fpm.conf
pid = run/php-fpm.pid  #取消一下行的注釋:
chmod +x /etc/init.d/php-fpm
chkconfig --add php-fpm
chkconfig php-fpm on

service php-fpm start #啟動php-fpn
ps aux|grep php-fpm ##檢查是否已經正常啟動
netstat -ant|grep :9000 #查看連接埠是否監聽
configure配置的時候可能出現的問題:
checking for cURL in default path… not found
configure: error: Please reinstall the libcurl distribution -
因為:curl的dev包沒有安裝
解決辦法:yum -y install curl-devel

make編譯的時候可能出現的問題:
virtual memory exhausted: Cannot allocate memory
原因:編譯過程是一個記憶體消耗較大的動作,記憶體吃緊,無法分配
解決辦法:配置項加上 –disable-fileinfo這個參數,重新編譯

六:配置nginx支援php


vim /usr/local/nginx/conf/nginx.conf
user www www; #首行user去掉注釋,修改Nginx運行組為www www
index index.php index.html index.htm; #找到這行,添加index.php
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
#取消FastCGI server部分location的注釋,注意fastcgi_param行的參數,改為$document_root$fastcgi_script_name,或者使用絕對路徑
vim /usr/local/php/etc/php-fpm.conf #同樣把user ,group 改為www www
#重啟nginx:


/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid` //重啟php,www www的使用者組和使用者才會生效

vim /usr/local/nginx/html/index.php #建立測試的php檔案
<?php php phpinfo();?>
瀏覽器輸入主機ip地址,成功顯示出phpinfo資訊

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.