CentOS 6~6.7 架設LNMP (自行編譯)

LNMP系列教學文:https://3cyber.com/category/teach/lnmp

點我觀看上一篇文章 https://3cyber.com/1093
(如果你在台灣 可以按照上一篇文章 把yum mirror設定修改 使用hinet的會比較快喔)

為什麼要自行編譯呢 其實就是因為除了版本可以自由選擇
如果有高危險性漏洞也能即時解決、而且自訂性高
眾所皆知,CentOS的yum庫很多東西版本都很老…

※基本上我建議VPS、實體主機的記憶體至少有512MB,否則可能會編譯失敗。

Step 1. 更新系統到最新

yum update -y

Step 2. 給YUM添加EPEL以及Remi源

rpm -ivh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

Step 3. 安裝所需要用到的lib

yum install wget gcc make unzip autoconf cmake gcc-c++ libxml2-devel openssl-devel libcurl-devel libjpeg-devel libpng-devel libicu-devel openldap-devel -y

Step 4. 編譯安裝最新版本的OpenSSL

https://www.openssl.org/source/下載最新的版本,本文使用1.0.2d

cd /root
wget https://www.openssl.org/source/openssl-1.0.2d.tar.gz
tar -zxvf openssl-1.0.2d.tar.gz
cd openssl-1.0.2d
./config --prefix=/usr no-threads shared
make
make test
make install

Step 5. 編譯Nginx + LibreSSL

此處編譯LibreSSL的原因 是因為可以在SSL使用CHACHA20-POLY1305這個ciphers
nginx到 http://nginx.org/download/ 下載最新的版本,本文使用1.9.4
LibreSSL到 http://ftp.openbsd.org/pub/OpenBSD/LibreSSL/
下載最新的版本,本文使用2.2.2

cd /root
wget http://nginx.org/download/nginx-1.9.4.tar.gz
wget http://ftp.openbsd.org/pub/OpenBSD/LibreSSL/libressl-2.2.2.tar.gz
tar -zxvf nginx-1.9.4.tar.gz
tar -zxvf libressl-2.2.2.tar.gz
cd nginx-1.9.4
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_spdy_module --with-ld-opt=-lrt --with-pcre-jit --with-openssl=/root/libressl-2.2.2 --with-cc-opt='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'
make
make install

Step 6. 添加nginx啟動腳本到/etc/init.d

然後將以下內容複製進去

#!/bin/sh # # nginx - this script starts and stops the nginx daemon # # 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: /etc/sysconfig/nginx # pidfile: /var/run/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/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/etc/nginx/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

Step 7. 設定nginx開機自動啟動

chkconfig –add nginx
chkconfig nginx on
service nginx start

Step 8. 編譯安裝 MySQL 5.6

#先建立MySQL所使用的帳號及群組

groupadd mysql
useradd -g mysql mysql

#下載Source & compile
cd /root
wget http://cdn.mysql.com/Downloads/MySQL-5.6/mysql-5.6.25.tar.gz
tar -zxvf mysql-5.6.25.tar.gz
cd mysql-5.6.25
cmake . -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock
make 
make install

#將編譯出來的所有者和group均更改為mysql這個account
chown -R mysql:mysql /usr/local/mysql/*

#初始化MySQL
scripts/mysql_install_db –user=mysql –datadir=/var/lib/mysql

#將MySQL自啟動腳本添加到/etc/init.d,以便使用service控制,且設定開機時自動啟動
cp support-files/mysql.server /etc/init.d/mysqld
chkconfig –add mysqld
chkconfig mysqld on
service mysqld start

#修改MySQL root帳號的密碼
./bin/mysqladmin -u root password 'your_password'

Step 9. 編譯安裝PHP

到 https://secure.php.net/downloads.php下載自己需要的版本,本文使用PHP 5.6

#添加php的user&group
groupadd www
useradd -c php-fpm-user -g www -M php-fpm

#編譯安裝PHP 我們使用的編譯參數如下,如果需要新增功能,請自行修改編譯參數。
cd /root
wget http://tw1.php.net/distributions/php-5.6.12.tar.gz
tar -zxvf php-5.6.12.tar.gz
cd php-5.6.12
./configure --prefix=/usr/local/php --with-libdir=lib64 --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=www --enable-mysqlnd --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-opcache --enable-pcntl --enable-mbstring --enable-soap --enable-zip --enable-calendar--enable-bcmath --enable-exif --enable-ftp --enable-intl --with-openssl --with-zlib --with-curl --with-gd --with-zlib-dir=/usr/lib --with-png-dir=/usr/lib --with-jpeg-dir=/usr/lib --with-gettext --with-mhash --with-ldap
make
make install

#添加PHP自啟動腳本且設定為可執行 添加到/etc/init.d 設定開機自啟動
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.con
chmod +x /etc/init.d/php-fpm
chkconfig -add php-fpm
service php-fpm start

恭喜你已經編譯完成LNMP完成,conf的部份下一次再寫囉~

未經允許不得轉載:三號科技報 » CentOS 6~6.7 架設LNMP (自行編譯)

分享到:更多 ()