源码安装LNMP环境

Posted 普朗克·李

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了源码安装LNMP环境相关的知识,希望对你有一定的参考价值。

新装CentOS 6.7,安装默认服务版本basic server

安装顺序linux(忽略...)--> nginx--> Mariadb--> php

为了不影响测试效果,首先关闭selinux及iptables。校对系统时间。

iptables -F
chkconfig iptables off
vi /etc/selinux/config
将
     SELINUX=enforcing
更改为
     SELINUX=disabled
修改时区
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime  
安装rz上传组件:
yum install lrzsz -y

1、安装Nginx:

安装依赖,新建文件夹,rz上传nginx安装包。

mkdir packet
cd packet/
创建nginx用户及组
groupadd -r nginx
useradd -r -g nginx nginx
解压,编译安装
tar xf nginx-1.11.5.tar.gz
cd nginx-1.11.5
./configure --prefix=/usr/local/nginx   --sbin-path=/usr/local/nginx/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/nginx.pid    --lock-path=/var/lock/nginx.lock   --user=nginx   --group=nginx   --with-http_ssl_module   --with-http_flv_module   --with-http_stub_status_module   --with-http_gzip_static_module   --http-client-body-temp-path=/var/tmp/nginx/client/   --http-proxy-temp-path=/var/tmp/nginx/proxy/   --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/   --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi   --http-scgi-temp-path=/var/tmp/nginx/scgi   --with-pcre
make && make install
为nginx编写启用脚本,需要注意两个位置的填写:
nginx="/usr/local/nginx/sbin/nginx" (configure参数中--sbin-path参数)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"(nginx配置文件,configure参数中 --conf-path参数)
vi /etc/rc.d/init.d/nginx
#!/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/local/nginx/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\' -`
   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
给启动脚本执行权限
chmod +x /etc/rc.d/init.d/nginx
设置开机启动nginx
chkconfig --add nginx
chkconfig nginx on
service nginx start
2、安装mariadb:
rz上传mariadb安装包
创建mariadb数据存放文件夹,并进行挂载至单独的分区(建议使用LVM)
mkdir -p /mydata/data
fdisk /dev/sdb
‘n’一个新分区,‘w’保存退出
mkfs.ext4 /dev/sdb1
编辑/etc/fstab末行新增
vi /etc/fstab
/dev/sdb1               /mydata/data            ext4    defaults        0 0

挂载
mount -a 
创建mysql用户及组
groupadd -r mysql
useradd -g mysql -r -s /sbin/nologin -M -d /mydata/data mysql
解压并为解压目录创建链接文件
tar xf mariadb-10.1.18-linux-x86_64.tar.gz
ln -sv /root/packet/mariadb-10.1.18-linux-x86_64 /usr/local/mysql
cd /usr/local/mysql
更改所有文件属主属组为mysql
chown -R mysql:mysql .
使用mysql用户将数据库初始化至上面创建并挂载的目录/mydata/data目录下
scripts/mysql_install_db --user=mysql --datadir=/mydata/data
更改文件属主root属组mysql
chown -R root .
为mysql提供配置文件
cp /usr/local/mysql/support-files/my-large.cnf /etc/my.cnf
更改参数,并新增datadir参数
vi /etc/my.cnf
# Try number of CPU\'s*2 for thread_concurrency (CPU数量*2)
thread_concurrency = 2
datadir = /mydata/data
为mysql提供启动文件,并设置开机自启
cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
配置mysql的man手册,编辑/etc/my.config
vi /etc/man.config
新增行
MANPATH  /usr/local/mysql/man

mysql的头文件输出至系统的头文件

ln -sv /usr/local/mysql/include  /usr/include/mysql  
mysql库文件路径输出至系统库文件
echo \'/usr/local/mysql/lib\' > /etc/ld.so.conf.d/mysql.conf  
重载数据库
ldconfig  
修改PATH变量,使系统可直接使用mysql相关命令
vi /etc/profile
末尾新增行
PATH=/usr/local/mysql/bin:$PATH
export PATH

  

source /etc/profile 
启动数据库
service mysqld start
3、安装PHP
安装依赖
yum -y groupinstall "X Software Development"
yum -y install libxml2 libxml2-devel bzip2-devel curl-devel libjpeg-devel libpng-devel freetype-devel

rz上传php,解压并编译安装

tar xf php-5.6.27.tar.bz2
cd php-5.6.27
./configure --prefix=/usr/local/php --with-config-file-path=/etc --with-bz2 --with-curl --enable-ftp --enable-sockets --disable-ipv6 --with-gd --with-jpeg-dir=/usr/local --with-png-dir=/usr/local --with-freetype-dir=/usr/local --enable-gd-native-ttf --with-iconv-dir=/usr/local --enable-mbstring --enable-calendar --with-gettext --with-libxml-dir=/usr/local --with-zlib --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-mysql=mysqlnd --enable-dom --enable-xml --enable-fpm --with-libdir=lib64 --enable-bcmath
make && make test && make install  
为php提供配置文件(configure编译时未使用--with-config-file-path参数,则默认在/usr/local/php/lib/目录下
cp packet/php-5.6.27/php.ini-production /etc/php.ini  
为php-fpm提供init文件,并设置开机自启
cp packet/php-5.6.27/sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
chmod +x /etc/rc.d/init.d/php-fpm
chkconfig --add php-fpm
chkconfig php-fpm on  
为php-fpm提供配置文件
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf  
编辑php-fpm配置文件
vi /usr/local/php/etc/php-fpm.conf
pm.max_children = 150
pm.start_servers = 8
pm.min_spare_servers = 5
pm.max_spare_servers = 10
pid = /usr/local/php/var/run/php-fpm.pid 
 启动
service php-fpm start
4、整合php和nginx
vi /etc/nginx/nginx.conf
添加index.php
 location / {
            root   html;
            index  index.html index.htm index.php;
        }
 
启用php
location ~ \\.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;
        }  
编辑/etc/nginx/fastcgi_params
vi /etc/nginx/fastcgi_params
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;
fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;
重载nginx
service nginx reload
在nginx的web主目录下添加index.php文件
cd /usr/local/nginx/ html/
vi index.php
<?php
phpinfo();
?>
浏览器中访问http://主机IP/index.php进行测试
[root@localhost ~]# netstat -antp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name    
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      2201/php-fpm        
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      2715/nginx          
tcp        0      0 127.0.0.1:34197             0.0.0.0:*                   LISTEN      2903/sshd           
tcp        0      0 127.0.0.1:34198             0.0.0.0:*                   LISTEN      2903/sshd           
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      2225/sshd           
tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      2020/cupsd          
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      2585/master         
tcp        0      0 172.28.1.8:10051            172.28.1.5:51231            TIME_WAIT   -                   
tcp        0     52 172.28.1.8:22               172.28.1.5:51242            ESTABLISHED 2903/sshd           
tcp        0      0 :::3306                     :::*                        LISTEN      2444/mysqld         
tcp        0      0 ::1:34197                   :::*                        LISTEN      2903/sshd           
tcp        0      0 ::1:34198                   :::*                        LISTEN      2903/sshd           
tcp        0      0 :::22                       :::*                        LISTEN      2225/sshd           
tcp        0      0 ::1:631                     :::*                        LISTEN      2020/cupsd          
tcp        0      0 ::1:25                      :::*                        LISTEN      2585/master         
[root@localhost ~]# 

 

以上是关于源码安装LNMP环境的主要内容,如果未能解决你的问题,请参考以下文章

源码安装LNMP环境

源码编译安装LNMP架构!

源码安装lnmp环境

centos6源码编译安装lnmp环境

centos下配置LNMP环境(源码安装)

在CENTOS上源码搭建LNMP环境