CentOS 7源碼安裝最新版LNMP環境

來源:互聯網
上載者:User

標籤:防火牆   資料庫   二進位   最新版   lnmp   

由於公司要求需要最新版的ZABBIX2.4.4需要最新版的系統CENTOS7和最新版的LNMP環境,所以本人摸索著使用最新版的環境搭建了LNMP系統,環境版本如下:

系統:CentOS 7 x86_64

NGINX:nginx-1.7.12

資料庫:mariadb-10.0.13

PHP:php-5.5.23

首先做一些準備工作,先把centos7的防火牆更換成iptables,可以參見如下連結

centos7防火牆改為iptables

修改成iptables之後就可以清空iptables裡面的過濾規則了,然後再關閉selinux服務。記得不要忘了先安裝gcc gcc-c++ wget net-tools等功能哦。


首先安裝mariadb


應為資料庫編譯需要很長時間,所以我這裡下載的是已經編譯好了的二進位包,下載版本為 mariadb-10.0.13-linux-glibc_214-x86_64.tar.gz 


1、下載二進位包到/usr/local/src 目錄下:

[[email protected] ~]# cd /usr/local/src/

[[email protected] src]# wget http://ftp.osuosl.org/pub/mariadb/mariadb-10.0.13/bintar-quantal-amd64/mariadb-10.0.13-linux-glibc_214-x86_64.tar.gz

2、將壓縮包解壓到/usr/local 目錄下:

[[email protected] src]# tar zvxf mariadb-10.0.13-linux-glibc_214-x86_64.tar.gz -C /usr/local/

3、建立mariadb 資料初始化目錄/data/mysql:

[[email protected] src]# mkdir -p /data/mysql

4、添加系統使用者mysql,禁止登陸系統,同時,將mariadb 資料初始化目錄所屬主和組都修改為mysql:

[[email protected] src]# useradd -r -s /sbin/nologin mysql ;chown -R mysql.mysql /data/mysql/

5、重新命名解壓出來的mariadb 目錄:

[[email protected] src]# mv /usr/local/mariadb-10.0.13-linux-x86_64/ /usr/local/mysql

6、進入重新命名後的目錄,初始化mariadb:

[[email protected] src]# cd /usr/local/mysql/

[[email protected] mysql]# ./scripts/mysql_install_db --datadir=/data/mysql --user=mysql

Installing MariaDB/MySQL system tables in ‘/data/mysql‘ ...

140906  2:03:19 [Note] InnoDB: Using mutexes to ref count buffer pool pages

140906  2:03:19 [Note] InnoDB: The InnoDB memory heap is disabled

140906  2:03:19 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins

140906  2:03:19 [Note] InnoDB: Compressed tables use zlib 1.2.3

140906  2:03:19 [Note] InnoDB: Using Linux native AIO

140906  2:03:19 [Note] InnoDB: Using CPU crc32 instructions

140906  2:03:19 [Note] InnoDB: Initializing buffer pool, size = 128.0M

........................................................................

The latest information about MariaDB is available at http://mariadb.org/.

You can find additional information about the MySQL part at:

http://dev.mysql.com

Support MariaDB development by buying support/new features from

SkySQL Ab. You can contact us about this at [email protected]

Alternatively consider joining our community based development effort:

http://mariadb.com/kb/en/contributing-to-the-mariadb-project/

報錯:WARNING: The host ‘test4‘ could not be looked up with resolveip.

解決辦法:vim /etc/hosts 在最後一行添加192.168.1.242 test4

報錯:./bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

解決辦法:yum -y install libaio-devel libaio

7、複製設定檔到/etc目錄覆蓋之前的my.cnf:

[[email protected] mysql]# cp support-files/my-large.cnf /etc/my.cnf

cp:是否覆蓋"/etc/my.cnf"? y

8、複製mysql 啟動指令檔到/etc/init.d 目錄下並重新命名為mysqld:

[[email protected] mysql]# cp support-files/mysql.server /etc/init.d/mysqld

同時修改啟動指令碼的許可權為755:

[[email protected] mysql]# chmod 755 !$

chmod 755 /etc/init.d/mysqld

9、編輯啟動指令碼,定義datadir 路徑:

[[email protected] mysql]# vim /etc/init.d/mysqld

定義資料存放路徑:

datadir=/data/mysql

10、將mariadb內建命令放入$PATH

[[email protected] ~]# PATH=$PATH:/etc/init.d/#當前有效,重啟shell就失效

[[email protected] ~]# echo "export PATH=$PATH:/etc/init.d/" >>/etc/profile

[[email protected] ~]# echo "export PATH=$PATH:/usr/local/mysql/bin/" >>/etc/profile

[[email protected] ~]# source !$



11、啟動mariadb:

[[email protected] mysql]# /etc/init.d/mysqld start

Starting MySQL. SUCCESS!




第二步,開始安裝php


這裡要先聲明一下,針對Nginx的php安裝和針對apache的php安裝是有區別的,因為Nginx中的php是以fastcgi的方式結合nginx的,可以理解為nginx代理了php的fastcgi,而apache是把php作為自己的模組來調用的。同樣的,php官方: http://www.php.net/downloads.php

下載php

[[email protected] src]# cd /usr/local/src

[[email protected] src]# wget http://am1.php.net/distributions/php-5.5.23.tar.gz

解壓php

[[email protected] src]# tar zxf php-5.5.23.tar.gz

建立相關賬戶

[[email protected] src]# useradd -s /sbin/nologin php-fpm

配置編譯參數

[[email protected] src]# cd php-5.5.23

[[email protected] src]# yum -y install gcc make gd-devel libjpeg-devel libpng-devel libxml2-devel bzip2-devel libcurl-devel 

[[email protected] php-5.5.23]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=mysqlnd  --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --disable-ipv6 --with-pear --with-curl --enable-bcmath --enable-mbstring --enable-sockets --with-gd --with-libxml-dir=/usr/local --with-gettext

錯誤:configure: error: xml2-config not found. Please check your libxml2 installation.

解決辦法:yum -y install libxml2-devel

錯誤:configure: error: Please reinstall the libcurl distribution -easy.h should be in <curl-dir>/include/curl/

解決辦法:yum -y install libcurl-devel

錯誤:configure: error: jpeglib.h not found.

解決辦法:yum -y install libjpeg-turbo-devel

錯誤:configure: error: png.h not found.

解決辦法:um -y install libpng-devel

錯誤:configure: error: freetype-config not found.

解決辦法:configure: error: mcrypt.h not found. Please reinstall libmcrypt.

錯誤:configure: error: mcrypt.h not found. Please reinstall libmcrypt.

解決辦法:yum -y install libmcrypt-devel



安裝php

[[email protected]  php-5.3.27]# make && make install

以上每一個步驟,如果沒有完全執行正確,那麼下一步是無法進行的,使用 echo $? 看結果是否為 “0” , 如果不是,就是沒有執行正確。

修改設定檔

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

vim /usr/local/php/etc/php-fpm.conf

把如下內容寫入該檔案:

[global]

pid = /usr/local/php/var/run/php-fpm.pid

error_log = /usr/local/php/var/log/php-fpm.log

[www]

listen = /tmp/php-fcgi.sock

user = php-fpm

group = php-fpm

listen.owner = nobody

listen.group = nobody

pm = dynamic

pm.max_children = 50

pm.start_servers = 20

pm.min_spare_servers = 5

pm.max_spare_servers = 35

pm.max_requests = 500

rlimit_files = 1024

儲存設定檔後,檢驗配置是否正確的方法為:

/usr/local/php/sbin/php-fpm -t

如果出現諸如 “test is successful” 字樣,說明配置沒有問題。

啟動php-fpm

cp /usr/local/src/php-5.5.23/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

chmod 755 /etc/init.d/php-fpm

service php-fpm start

如果想讓它開機啟動,執行:

chkconfig php-fpm on

檢測是否啟動:

ps aux |grep php-fpm

看看是不是有很多個進程(大概20多個)。




安裝nginx


下載nginx

cd /usr/local/src/

wget http://nginx.org/download/nginx-1.7.12.tar.gz

解壓nginx

tar zxvf nginx-1.7.12.tar.gz

配置編譯參數

cd nginx-1.7.12

./configure \

--prefix=/usr/local/nginx \

--with-http_realip_module \

--with-http_sub_module \

--with-http_gzip_static_module \

--with-http_stub_status_module  \

--with-pcre

報錯:./configure: error: the HTTP rewrite module requires the PCRE library.

解決辦法:yum -y install pcre-devel

報錯:./configure: error: the HTTP gzip module requires the zlib library.

解決辦法:yum install -y zlib-devel


編譯nginx

make

安裝nginx

make install


編寫nginx啟動指令碼,並加入系統服務

vim /etc/init.d/nginx

寫入如下內容:

#!/bin/bash

# chkconfig: - 30 21

# description: http service.

# Source Function Library

. /etc/init.d/functions

# Nginx Settings


NGINX_SBIN="/usr/local/nginx/sbin/nginx"

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

NGINX_PID="/usr/local/nginx/logs/nginx.pid"

RETVAL=0

prog="Nginx"


start() {

        echo -n $"Starting $prog: "

        mkdir -p /dev/shm/nginx_temp

        daemon $NGINX_SBIN -c $NGINX_CONF

        RETVAL=$?

        echo

        return $RETVAL

}


stop() {

        echo -n $"Stopping $prog: "

        killproc -p $NGINX_PID $NGINX_SBIN -TERM

        rm -rf /dev/shm/nginx_temp

        RETVAL=$?

        echo

        return $RETVAL

}


reload(){

        echo -n $"Reloading $prog: "

        killproc -p $NGINX_PID $NGINX_SBIN -HUP

        RETVAL=$?

        echo

        return $RETVAL

}


restart(){

        stop

        start

}


configtest(){

    $NGINX_SBIN -c $NGINX_CONF -t

    return 0

}


case "$1" in

  start)

        start

        ;;

  stop)

        stop

        ;;

  reload)

        reload

        ;;

  restart)

        restart

        ;;

  configtest)

        configtest

        ;;

  *)

        echo $"Usage: $0 {start|stop|reload|restart|configtest}"

        RETVAL=1

esac


exit $RETVAL

儲存後,更改許可權:

chmod 755 /etc/init.d/nginx

chkconfig --add nginx

如果想開機啟動,請執行:

chkconfig nginx on

更改nginx配置

首先把原來的設定檔清空:

> /usr/local/nginx/conf/nginx.conf

“>” 這個符號為重新導向的意思,單獨用它,可以把一個文字文件快速清空。

vim /usr/local/nginx/conf/nginx.conf

寫入如下內容:

user nobody nobody;

worker_processes 2;

error_log /usr/local/nginx/logs/nginx_error.log crit;

pid /usr/local/nginx/logs/nginx.pid;

worker_rlimit_nofile 51200;


events

{

    use epoll;

    worker_connections 6000;

}


http

{

    include mime.types;

    default_type application/octet-stream;

    server_names_hash_bucket_size 3526;

    server_names_hash_max_size 4096;

    log_format combined_realip ‘$remote_addr $http_x_forwarded_for [$time_local]‘

    ‘$host "$request_uri" $status‘

    ‘"$http_referer" "$http_user_agent"‘;

    sendfile on;

    tcp_nopush on;

    keepalive_timeout 30;

    client_header_timeout 3m;

    client_body_timeout 3m;

    send_timeout 3m;

    connection_pool_size 256;

    client_header_buffer_size 1k;

    large_client_header_buffers 8 4k;

    request_pool_size 4k;

    output_buffers 4 32k;

    postpone_output 1460;

    client_max_body_size 10m;

    client_body_buffer_size 256k;

    client_body_temp_path /usr/local/nginx/client_body_temp;

    proxy_temp_path /usr/local/nginx/proxy_temp;

    fastcgi_temp_path /usr/local/nginx/fastcgi_temp;

    fastcgi_intercept_errors on;

    tcp_nodelay on;

    gzip on;

    gzip_min_length 1k;

    gzip_buffers 4 8k;

    gzip_comp_level 5;

    gzip_http_version 1.1;

    gzip_types text/plain application/x-javascript text/css text/htm application/xml;


server

{

    listen 80;

    server_name localhost;

    index index.html index.htm index.php;

    root /usr/local/nginx/html;


    location ~ \.php$ {

        include fastcgi_params;

        fastcgi_pass unix:/tmp/php-fcgi.sock;

        fastcgi_index index.php;

        fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;

    }


}


}

儲存配置後,先檢驗一下設定檔是否有錯誤存在:

/usr/local/nginx/sbin/nginx  -t

如果顯示內容如下,則配置正確,否則需要根據錯誤提示修改設定檔:

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

啟動nginx:

service nginx start

如果不能啟動,請查看 “/usr/local/nginx/logs/error.log” 檔案,檢查nginx是否啟動:

ps aux |grep nginx

看是否有進程。




測試是否解析php檔案


建立測試檔案:

vim /usr/local/nginx/html/2.php

內容如下:

<?php echo phpinfo();?>

測試:

[[email protected] nginx]# curl localhost/2.php

或者使用瀏覽器開啟http://YourServerIPAddress/2.php



重要:如果解析不了,檢查日誌發現串連不到php,我的php版本為5.5.23,比較新的版本,需要在php/etc/php-fpm.conf檔案中添加

listen.owner = nobody

listen.group = nobody

這兩行,再重啟一下服務就能使用php了

原因是/tmp/php-fcgi.sock這個檔案沒有讀許可權

至此,最新版的LNMP環境源碼編譯安裝完成了,感謝我的老師阿銘給我提供的協助,另外提供老師開設的論壇地址,歡迎大家前來學習討論:阿銘linux,歡迎前來共同學習

本文出自 “檸檬” 部落格,請務必保留此出處http://xianglinhu.blog.51cto.com/5787032/1631007

CentOS 7源碼安裝最新版LNMP環境

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.