首页 > 其他 > 详细

LNMP搭建

时间:2016-02-17 22:38:07      阅读:257      评论:0      收藏:0      [点我收藏+]


安装mysql

[root@hongsrc]# wgethttp://cdn.mysql.com//Downloads/MySQL-5.5/mysql-5.5.47-linux2.6-x86_64.tar.gz

一、解压

[root@hongsrc]# tar zxf mysql-5.5.47-linux2.6-x86_64.tar.gz

[root@hongsrc]# mv mysql-5.5.47-linux2.6-x86_64 /usr/local/mysql

 

二、建立mysql用户

[root@localhost src]# useradd -s/sbin/nologin –M  mysql

 

 

三、初始化数据库

[root@hongmysql]# mkdir -p /data/mysql ; chown -R mysql:mysql /data/mysql

[root@hong mysql]# yum -y installlibaio-*  (apt-*)

[root@localhostmysql]#  ./scripts/mysql_install_db--user=mysql --datadir=/data/mysql

 

四、拷贝启动脚本和配置文件

[root@hongmysql]# cp support-files/my-large.cnf /etc/my.cnf

[root@hongmysql]# cp support-files/mysql.server /etc/init.d/mysqld

[root@hongmysql]# chmod 755 /etc/init.d/mysqld

 

五、修改配置文件

[root@hongmysql]# vim /etc/init.d/mysqld

46basedir=/usr/local/mysql

 47 datadir=/data/mysql

 

六、添加开机启动,启动mysql

[root@hongmysql]# chkconfig --add mysqld

[root@hongmysql]# chkconfig mysqld on

[root@hongmysql]#  service mysqld start

 

七、查看mysql服务和端口有没启动

[root@hongmysql]# netstat –lnp

[root@hongmysql]# ps aux |grep mysqld

 

安装php

[root@localhost src]# wget http://cn2.php.net/distributions/php-5.5.30.tar.gz

 

  • 解压。

[root@hong php]#tar zxf php-5.5.30.tar.gz

[root@hong php]#mv /usr/local/src/php-5.5.30 /usr/local/php

 

二、安装所需要的包

[root@hong php]# yum -y install gcc   libxml2-*  curl-devel  libjpeg-*  libpng-*  freetype-* openssl-*(libcurl-devel)
 

[root@hong php]# rpm -ivh http://www.aminglinux.com/bbs/data/attachment/forum/month_1211/epel-release-6-7.noarch.rpm

 

[root@hong php]# yum install -y  libmcrypt-devel

 

三、创建用户

[root@hong etc]# useradd -s /sbin/nologin -M php-fpm

 

四、配置编译

[root@hong php]#  ./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 --enable-zend-multibyte --disable-ipv6 --with-pear --with-curl--with-openssl

 

五、进行编译和安装

[root@hong php]#make  &&  make install

/usr/bin/ld: cannot find -lltdl
collect2: ld returned 1 exit status
make: *** [sapi/fpm/php-fpm] 错误 1

解决方法:

yum install -y libtool-ltdl-devel

 

四、修改配置文件

[root@hong php]# cp php.ini-production /usr/local/php/etc/php.ini

[

root@hong php]# vim /usr/local/php/etc/php.ini

 

1933 [global]

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

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

1936 [www]

1937 listen = /tmp/php-fcgi.sock

1938 user = php-fpm

1939 group = php-fpm

1940 pm = dynamic

1941 pm.max_children = 50

1942 pm.start_servers = 20

1943 pm.min_spare_servers = 5

1944 pm.max_spare_servers = 35

1945 pm.max_requests = 500

1946 rlimit_files = 1024

 

六、复制启动脚本,开机自动加载

[root@hong php]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

[root@hong php]# chmod 755 /etc/init.d/php-fpm

[root@hong php]# chkconfig php-fpm on

[root@hong php]# mv /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

 

七、启动服务并查看端口

[root@hong php]# service php-fpm start

[root@hong etc]# ps aux |grep php-fpm

[root@hong etc]# netstat –lnp

 

 

Nginx安装

[root@hong src]#wget http://nginx.org/download/nginx-1.8.0.tar.gz

  • 解压

[root@hong src]# tar zxf nginx-1.8.0.tar.gz

[root@localhost src]# cd nginx-1.8.0

 

二、配置编译

[root@hong nginx]# yum -y install pcre-*

[root@hong nginx]#./configure --prefix=/usr/local/nginx --conf-path=/usr/local/nginx/conf/nginx.conf--with-http_realip_module --with-http_sub_module --with-http_gzip_static_module--with-http_stub_status_module --with-pcre

 

三、编译安装

[root@hong nginx]#make && make install

 

四、启动nginx

[root@hong nginx]# /usr/local/nginx/sbin/nginx

注意:要关闭httpd

 

五、配置nginx文件

[root@hong nginx]# vim /usr/local/nginx/nginx.conf

 

65         location ~ \.php$ {

 66             root           html;

 67             fastcgi_pass   127.0.0.1:9000;

 68             fastcgi_index  index.php;

 69  fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;   //这个地方如果没更改路径,访问时就会是404

 70            include       fastcgi_params;

 71         }

 

六、测试php

 [root@hong html]# cd /usr/local/nginx/html

 

1.编辑一个php文件

 [root@hong html]#vim info.php

<?php

 phpinfo();

?>

 

2.检查配置文件

~[root@hong html]# /usr/local/nginx/sbin/nginx  -t

 

3.启动nginx服务

~[root@hong html]# /usr/local/nginx/sbin/nginx -s reload

 

~4.访问网页

                           

 

六、编辑nginx脚本(自己编辑)

[root@hong html]# 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

 

赋予权限755

[root@hong html]# chmod 755 /etc/init.d/nginx

 

添加成开机自启动

[root@hong html]# chkconfig --add nginx

[root@hong html]# chkconfig  nginxon

[root@hong html]# service nginx restart

 

七、修改nginx配置文件

[root@hong html]# > /usr/local/nginx/conf/nginx.conf  

[root@hong html]# 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

{

    useepoll;

   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 hong ‘$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/htmapplication/xml;

    includevhosts/*.conf;    #nginx支持默认的虚拟主机

}

 

然后编辑虚拟主机配置文件

[root@hong html]# cd /usr/local/nginx/conf

[root@hong conf]# mkdir vhosts

[root@localhost vhosts]# vim default.conf

 

server

{

   listen 80 default_server;

   server_name localhost;

   index index.html index.htm index.php;

root /tmp/123;     //限制第一个默认主机为403

deny all

}

 

 

创建限制访问的目录

[root@hong vshosts]# mkdir /tmp/123

 

检查配置是否有错

[root@localhost vhosts]# /usr/local/nginx/sbin/nginx –t

 

访问本地时
[root@hong 123]# curl -x127.0.0.1:80 
www.dshjkf.com

 

 

虚拟主机添加你需要的网站如:

[root@hong vshosts]# vim hong.conf

 

server

{

    listen 80;

    server_name www.hong.com;

    index index.html index.htmindex.php;

    root/data/www;     #网站所在路径

 

    location ~ \.php$ {

        include fastcgi_params;

       # fastcgi_passunix:/tmp/php-fcgi.sock;

                  fastcgi_pass127.0.0.1:9000;            #这个地方不添加端口可能会报502错误

        fastcgi_index index.php;

        fastcgi_paramSCRIPT_FILENAME/data/www$fastcgi_script_name;

    }

}

[root@localhost vhosts]# /usr/local/nginx/sbin/nginx -t

[root@localhost vhosts]# /etc/init.d/nginx reload

 

测试网站访问

[root@hong www]# curl -x127.0.0.1:80 hong.com -I

HTTP/1.1 302 Moved Temporarily

Server: nginx/1.8.0

Date: Sun, 13 Dec 2015 21:09:07 GMT

Content-Type: text/html

Connection: keep-alive

X-Powered-By: PHP/5.5.30

location: install

 

 

PHP-fpm配置文件

 /usr/local/php/etc/php-fpm.conf   //php服务配置

  /usr/local/php/etc/php.ini        //php全局配置

  • 清空配置文件

[root@hong~]# > /usr/local/php/etc/php-fpm.conf 

[root@hong~]# 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/www.sock  (可自定义)

user =php-fpm           (可自定义)

group =php-fpm           (可自定义)

pm = dynamic           //动态加载管理以下模块

pm.max_children= 50     //子进程最大50

pm.start_servers= 20      //刚开始启动20

pm.min_spare_servers= 5    //最小5

pm.max_spare_servers= 35   //最多

pm.max_requests= 500      //在生命周期之类处理多少个请求,自动销毁

rlimit_files= 1024           //每一个进程描述的限制

slowlog = /tmp/www_slow.log  //查找网站速度慢

request_slowlog_timeout = 1  //超时1分钟

php_admin_value[open_basedir]=/data/www/:/tmp/ //多个路径用:冒号隔开

 

 

建立多个pool池子(可选)

[www1]

listen =/tmp/php-fcgi1.sock  (可自定义)

user =php-fpm           (可自定义)

group =php-fpm           (可自定义)

pm = dynamic           //动态加载管理以下模块

pm.max_children= 50     //子进程最大50

pm.start_servers= 20      //刚开始启动20

pm.min_spare_servers= 5    //最小5

pm.max_spare_servers= 35   //最多

pm.max_requests= 500      //在生命周期之类处理多少个请求,自动销毁

rlimit_files= 1024           //每一个进程描述的限制

 

检查是否有错

[root@hong php]# /usr/local/php/sbin/php-fpm -t

 

重启服务

[root@hong ~]# /etc/init.d/php-fpm restart



建立池子的好处:可以把权限分开,不同域名在不同池子也可以到同一个池子;其次如果一个池子挂了,整个池子的网站都挂了,分开多个池子网站可以分开



解决nginx502

1.编辑虚拟配置文件

[root@hong vhosts]# vim /usr/local/nginx/conf/vhosts/hong.conf

 

server

{

    listen 80;

    server_name www.hong.com;

    index index.html index.htmindex.php;

    root /data/www;

 

    location ~ \.php$ {

        include fastcgi_params;

        fastcgi_pass unix:/tmp/www.sock;

        #fastcgi_pass127.0.0.1:9000;

        fastcgi_index index.php;

        fastcgi_paramSCRIPT_FILENAME /data/www$fastcgi_script_name;

    }

}

 

检查错误

[root@hong vhosts]# /usr/local/nginx/sbin/nginx -t

重新启动或加载配置文件

[root@hong vhosts]# /etc/init.d/nginx reload

 

访问出现502,排查步骤:

1.查看错误日志

[root@hong www]# cat /usr/local/nginx/logs/nginx_error.log

2.查看sock文件权限,权限的属主和属组都是root 并且只有读写权限,而nginx的用户是nobody

 

[root@hong www]# ll -d /tmp/php-fcgi.sock

srw-rw---- 1 root root 0 12 14 05:26/tmp/php-fcgi.sock

 

3.所以要修改配置。进入php配置文件

[root@hong www]# vim /usr/local/php/etc/php-fpm.conf

6 user = php-fpm

  7 group = php-fpm

  8 listen.owner = nobody        //监听的用户

  9 listen.group = nobody         //监听的组

(listen.mode =444)

 

4.启动php服务

[root@localhostvhosts]# /usr/local/php/sbin/php-fpm -t

[12-Jan-201602:14:25] NOTICE: configuration file /usr/local/php/etc/php-fpm.conf test issuccessful

 

[root@hongwww]# /etc/init.d/php-fpm restart

Gracefullyshutting down php-fpm . done

Startingphp-fpm  done

 

5.在访问域名看能否访问www.hong.com


本文出自 “8653294” 博客,请务必保留此出处http://8653294.blog.51cto.com/8643294/1742801

LNMP搭建

原文:http://8653294.blog.51cto.com/8643294/1742801

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!