1. 程式人生 > >CentOS 6.5 編譯安裝 LNMP環境

CentOS 6.5 編譯安裝 LNMP環境

kcon 幹凈 libs prefix bic 基於 nss led dev

建立一個軟件包目錄存放

mkdir -p /usr/local/src/

清理已經安裝包

技術分享
rpm -e httpd
rpm -e mysql
rpm -e php
yum -y remove httpd
yum -y remove mysql
yum -y remove php

#搜索apache包
rpm -qa http*

#強制卸載apache包
rpm -e --nodeps 查詢出來的文件名

#檢查是否卸載幹凈
rpm -qa|grep http*
技術分享

selinux可能會致使編譯安裝失敗,我們先禁用它。永久禁用,需要重啟生效

sed -i ‘s/SELINUX=enforcing/SELINUX=disabled/g‘ /etc/selinux/config

臨時禁用,不需要重啟

setenforce 0

安裝必備工具

yum -y install make gcc gcc-c++ gcc-g77 flex bison file libtool libtool-libs autoconf kernel-devel libjpeg libjpeg-devel libpng libpng-devel libpng10 libpng10-devel gd gd-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glib2 glib2-devel bzip2 bzip2-devel libevent libevent-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel gettext gettext-devel ncurses-devel gmp-devel pspell-devel unzip libcap lsof

安裝mysql5.6.17

按照標準需要給mysql創建所屬用戶和用戶組

技術分享
創建群組
groupadd mysql
創建一個用戶,不允許登陸和不創主目錄 
useradd -s /sbin/nologin -g mysql -M mysql
檢查創建用戶
tail -1 /etc/passwd
技術分享

centos最小化安裝後,會有mysql的庫因此先卸載!

檢查安裝與否
rpm -qa|grep mysql 
強制卸載 
rpm -e mysql-libs-5.1.73-3.el6_5.x86_64 --nodeps

MySQL從5.5版本開始,通過./configure進行編譯配置方式已經被取消,取而代之的是cmake工具。 因此,我們首先要在系統中源碼編譯安裝cmake工具。

技術分享
cd /usr/local/src
wget http://www.cmake.org/files/v2.8/cmake-2.8.12.2.tar.gz tar zxvf cmake-2.8.12.2.tar.gz cd cmake-2.8.12.2 ./configure make && make install
技術分享

使用cmake來編譯安裝mysql5.6.17

技術分享
cd /usr/local/src
wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.17.tar.gz tar zxvf mysql-5.6.17.tar.gz cd mysql-5.6.17 cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DMYSQL_USER=mysql -DWITH_DEBUG=0 -DWITH_SSL=system make && make install
技術分享

修改/usr/local/mysql權限

chmod +w /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql

進入support-files目錄

cd support-files/
如果還有my.cnf請備份
mv /etc/my.cnf /etc/my.cnf.bak
如果願意也可以復制配置文件到etc下
cp my-default.cnf /etc/my.cnf

執行初始化配置腳本,創建系統自帶的數據庫和表,註意配置文件的路徑

/usr/local/mysql/scripts/mysql_install_db --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql

拷貝mysql安裝目錄下support-files服務腳本到init.d目錄

#拷貝腳本
cp mysql.server /etc/init.d/mysqld
#賦予權限
chmod +x /etc/init.d/mysqld

設置開機啟動

chkconfig mysqld on
啟動MySQL
service mysqld start
或者
/etc/init.d/mysql start

修改環境變量

修改/etc/profile文件
vi /etc/profile
在文件末尾添加
PATH=/usr/local/mysql/bin:$PATH
export PATH

讓配置立即生效

source /etc/profile

登陸測試,默認是沒有密碼,直接回車就可進入

mysql -uroot -p

***************************安裝 PHP 的分割線******************************************************

安裝依賴關系

libiconv庫為需要做轉換的應用提供了一個iconv()的函數,以實現一個字符編碼到另一個字符編碼的轉換

技術分享
cd /usr/local/src
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz tar zxvf libiconv-1.14.tar.gz cd libiconv-1.14 ./configure --prefix=/usr/local/libiconv make && make install
技術分享

libmcrypt是加密算法擴展庫。

技術分享
cd /usr/local/src
wget http://iweb.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz tar zxvf libmcrypt-2.5.8.tar.gz cd libmcrypt-2.5.8 ./configure make && make install
技術分享

Mhash是基於離散數學原理的不可逆向的php加密方式擴展庫,其在默認情況下不開啟。

技術分享
cd /usr/local/src
wget https://nchc.dl.sourceforge.net/project/mhash/mhash/0.9.9.9/mhash-0.9.9.9.tar.gz tar zxvf mhash-0.9.9.9.tar.gz cd mhash-0.9.9.9 ./configure make && make install
技術分享

編譯mcrypt可能會報錯:configure: error: *** libmcrypt was not found

vi  /etc/ld.so.conf
最後一行添加
/usr/local/lib/
載入
ldconfig

mcrypt 是 php 裏面重要的加密支持擴展庫,Mcrypt擴展庫可以實現加密解密功能,就是既能將明文加密,也可以密文還原。

技術分享
cd /usr/local/src
wget http://iweb.dl.sourceforge.net/project/mcrypt/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz tar zxvf mcrypt-2.6.8.tar.gz cd mcrypt-2.6.8 ./configure make && make install
技術分享

編譯mcrypt可能會報錯:/bin/rm: cannot remove `libtoolT‘: No such file or directory 如果出現錯誤,用下面的方法

修改 configure 文件,把RM=‘$RM‘改為RM=‘$RM -f‘ 這裏的$RM後面一定有一個空格。 如果後面沒有空格,直接連接減號,就依然會報錯。

正式開始編譯php!

技術分享
cd /usr/local/src
wget http://cn2.php.net/distributions/php-5.6.30.tar.gz tar zxvf php-5.6.30.tar.gz cd php-5.6.30 ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv=/usr/local/libconv-1.14 --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-magic-quotes --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-mbstring --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --disable-fileinfo --enable-maintainer-zts make && make install
技術分享

如果出現 resinstall iconv

技術分享
cd /usr/local/src
cd php-5.6.30
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-magic-quotes --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-mbstring --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --disable-fileinfo --enable-maintainer-zts
make && make install
技術分享

修改fpm配置php-fpm.conf.default文件名稱

mv /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

復制php.ini配置文件

cp php.ini-production /usr/local/php/etc/php.ini

復制php-fpm啟動腳本到init.d

cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

賦予執行權限

chmod +x /etc/init.d/php-fpm

添加為啟動項

chkconfig --add php-fpm

設置開機啟動

chkconfig php-fpm on

按照標準,給php-fpm創建一個指定的用戶和組

創建群組
groupadd www
創建一個用戶,不允許登陸和不創主目錄 
useradd -s /sbin/nologin -g www -M www

將5.6版本的php添加到環境變量(如果不添加,默認是5.3版本)

技術分享
修改/etc/profile文件
vi /etc/profile

在文件末尾的 PATH=/usr/local/mysql/bin:$PATH 後面再加PHP的命令目錄

PATH=/usr/local/mysql/bin:$PATH:/usr/local/php/bin

讓配置生效
source /etc/profile
技術分享

立即啟動php-fpm

service php-fpm start
#或者
/etc/init.d/php-fpm start

php安裝完成

***************nginx安裝分割線*****************

安裝openssl

技術分享
cd /usr/local/src

wget https://www.openssl.org/source/openssl-1.0.2k.tar.gz

tar zxvf openssl-1.0.2k.tar.gz

cd openssl-1.0.2k

./config --prefix=/usr/local/src/openssl --openssldir=/usr/local/src/openssl/conf
make && make install
#檢驗安裝
/usr/local/src/openssl/bin/openssl version -a 
技術分享

安裝pcre

技術分享
cd /usr/local/src

rm -rf pcre
rm -rf pcre-8.37 

wget https://ftp.pcre.org/pub/pcre/pcre-8.37.tar.gz

tar zxvf pcre-8.37.tar.gz

cd pcre-8.37

./configure --prefix=/usr/local/src/pcre/

make && make install
技術分享

安裝zlib軟件

技術分享
cd /usr/local/src

rm -rf zlib
rm -rf zlib-1.2.11

wget https://nchc.dl.sourceforge.net/project/libpng/zlib/1.2.11/zlib-1.2.11.tar.gz

tar zxvf zlib-1.2.11.tar.gz

cd zlib-1.2.11

./configure --prefix=/usr/local/src/zlib/

make && make install
技術分享

安裝nginx

技術分享
#添加www用戶和組
groupadd www
useradd -g www www

#創建網站根目錄
mkdir -p     /var/www/root/
chmod -R 775 /var/www/root/

#進入安裝目錄
cd /usr/local/src

#刪除原有安裝
rm -rf nginx
rm -rf nginx-1.12.0

wget http://nginx.org/download/nginx-1.12.0.tar.gz
 
tar zxvf nginx-1.12.0.tar.gz

cd nginx-1.12.0

#配置(使用openssl、pcre、zlib的源碼路徑)
./configure --user=www --group=www --prefix=/usr/local/src/nginx --with-http_ssl_module --with-openssl=/usr/src/openssl-1.0.2k --with-pcre=/usr/src/pcre-8.37 --with-zlib=/usr/src/zlib-1.2.11 --with-http_stub_status_module --with-threads

make && make install

#驗證
/usr/local/src/nginx/sbin/nginx -V
技術分享

修改配置文件

vi /usr/local/src/nginx/conf/nginx.conf

技術分享
修改配置文件中
一:
location / {
            root   html;                    //網站根目錄
            index  index.html index.htm;
        }

修改為
 location / {
            root    /var/www/root;
            index  index.html index.htm;
        }

二:
#location ~ \.php$ {
        #    root           html;            //網站根目錄
        #    fastcgi_pass   127.0.0.1:9000;    //將php傳給9000好端口php-fpm
        #    fastcgi_index  index.php;        
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;    //需修改
        #    include        fastcgi_params;
        #}


修改為
location ~ \.php$ {    
  autoindex_exact_size off;         #文件大小從KB開始顯示
  autoindex_localtime on;            #顯示文件修改時間為服務器本地時間 
   root    /var/www/root;        #自定義網站根目錄
   fastcgi_pass   127.0.0.1:9000;    #需要開啟
   fastcgi_index  index.php;        #需要開啟
   fastcgi_param  SCRIPT_FILENAME   /var/www/root/$fastcgi_script_name;  
   include        fastcgi_params;    #需要開啟
}

如果要開啟目錄瀏覽功能 ,在 server大括號選項中加入
autoindex on;                 #開啟nginx目錄瀏覽功能
 
技術分享

啟動nginx

/usr/local/src/nginx/sbin/nginx 

如果出現 nginx: [error] open() "/usr/local/src/nginx/logs/nginx.pid" failed (2: No such file or directory) 這個錯誤

/usr/local/src/nginx/sbin/nginx -c /usr/local/src/nginx/conf/nginx.conf 

這時已經啟動成功

ps aux | grep nginx

技術分享

可以看到master 和 worker 進程開啟

重啟 nginx命令

/usr/local/src/nginx/sbin/nginx -s reload

關閉ngixn

pkill -9 nginx

測試端口

netstat | grep 80

瀏覽器測試

IP:80

本地的話 
127.0。0.1

如果訪問不了,查看下防火墻的80端口是否開通

技術分享
vi /etc/sysconfig/iptables

#看是否有下面這句 沒有補上
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

#重啟防火墻
/etc/init.d/iptables restart

CentOS 6.5 編譯安裝 LNMP環境