linux下LNMP搭建

軟件版本:
php

nginx 1.6.2版本下載:http://mirrors.sohu.com/nginx/nginx-1.6.2.tar.gzhtml

mysql 5.1.72版本下載:http://mirrors.sohu.com/mysql/MySQL-5.1/mysql-5.1.72-linux-x86_64-glibc23.tar.gzmysql

php  5.4.37版本下載http://mirrors.sohu.com/php/php-5.4.38.tar.bz2linux

安裝LAMP以前必備安裝包nginx

yum install wget gcc gcc-c++ make re2c curl curl-devel libxml2 libxml2-devel libjpeg libjpeg-devel libpng libpng-devel libmcrypt libmcrypt-devel zlib zlib-devel openssl openssl-devel freetype freetype-devel gd gd-devel perl perl-devel ncurses ncurses-devel bison bison-devel libtool gettext gettext-devel cmake bzip2 bzip2-devel pcre pcre-devel

編譯安裝mysqlc++

解壓並移動;
sql

# tar zxvf mysql-5.1.40-linux-i686-icc-glibc23.tar.gz
# mv mysql-5.1.40-linux-x86_64-icc-glibc23 /usr/local/mysql

建立數據庫用戶、目錄、更改屬主;數據庫

# useradd -s /sbin/nologin mysql 
# mkdir -p /data/mysql 
# chown -R mysql:mysql /data/mysql

初始化數據庫;apache

# cd /usr/local/mysql 
# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql

拷貝配置文件,修改my.cnf;vim

# cp support-files/my-large.cnf /etc/my.cnf 
# vim /etc/init.d/mysqld
[mysqld]
user=mysql 
datadir=/data/mysql

不少模板配置文件在/support-files/目錄下;

根據內存大小選擇: 

·my-small.cnf (內存 <= 64M)

·my-medium.cnf (內存 128M )

·my-large.cnf (內存 512M)

·my-huge.cnf (內存 1G-2G)

·my-innodb-heavy-4G.cnf (內存 4GB)

編輯mysqld啓動腳本,找到basedir和datadir添加路徑;

# vi /etc/init.d/mysqld
basedir=/usr/local/mysql
datadir=/data/mysql


拷貝啓動文件、添加開機啓動;

# cp support-files/mysql.server /etc/init.d/mysqld
# chmod 755 /etc/init.d/mysqld 
# chkconfig --add mysqld 
# chkconfig mysqld on

啓動mysql;

service mysqld  start

編譯安裝php 

[root@localhost src]# tar jxvf php-5.4.37.tar.bz2
[root@localhost src]# cd php-5.4.37
[root@localhost php-5.4.37]# ./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=/usr/local/mysql  --with-mysql-sock=/tmp/mysql.sock  --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-curl 
[root@localhost php-5.4.37]# make && make install

拷貝php配置文件;

[root@localhost php-5.4.37]#cp /usr/local/src/php-5.3.28/php.ini-production /usr/local/php/etc/php.ini

拷貝php-fpm啓動腳本;

·php-fpm是管理fastCGI的一個管理器

[root@localhost php-5.4.37]#cp /usr/local/src/php-5.4.37/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

拷貝php-fpm配置文件、修改權限、開機啓動;

[root@localhost php-5.4.37]#mv /usr/local/php/etc/php-fpm.conf.default  /usr/local/php/etc/php-fpm.conf
[root@localhost php-5.4.37]#chmod 755 /etc/init.d/php-fpm 
[root@localhost php-5.4.37]#chkconfig --add php-fpm
[root@localhost php-5.4.37]#chkconfig php-fpm on

啓動php-fpm;

service php-fpm start

編譯安裝nginx 

安裝pcre庫

·pcre是什麼:perl兼容正表達式

·爲何安裝pcre:使nginx支持HTTP rewrite模塊

    tar zxvf  pcre-8.37.tar
    cd pcre-8.37
    ./configure
    make && make install

編譯安裝nginx;

[root@localhost ~]# cd /usr/local/src/
[root@localhost src]# tar zxvf nginx-1.6.2.tar.gz 
[root@localhost src]# cd nginx-1.6.2
[root@localhost nginx-1.6.2]# ./configure   --prefix=/usr/local/nginx  
[root@localhost nginx-1.6.2]# make && 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
###########################################################################

設置nginx權限、開機啓動;

[root@localhost nginx-1.6.2]# chmod 755 /etc/init.d/nginx 
[root@localhost nginx-1.6.2]# chkconfig --add nginx
[root@localhost nginx-1.6.2]# chkconfig nginx on

配置解析php

編輯nginx配置文件,找到下面的代碼,刪除前面的#號,更改 fastcgi_param這一行,加入nginx存放路徑;

wKioL1VRnqWBDMIlAAEFHxnhyw8501.jpg

[root@localhost nginx-1.6.2]# vi /usr/local/nginx/conf/nginx
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME /data/www$fastcgi_script_name;
            include        fastcgi_params;
        }

從新加載nginx;

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

測試解析php;

[root@localhost nginx-1.6.2]# vi /data/www/index.php
<?php
        phpinfo();
?>

訪問網站出現頁面則爲成功;

wKioL1VRnunBmKx7AALjPhDXbW8247.jpg

相關文章
相關標籤/搜索