软件开发的家园,编程爱好者的天地.

现在是:北京时间 2016/4/14 上午11:50:51 星期四

设为首页  |  加入收藏  |  网站地图

如何手动在CentOS下搭建LNMP环境
发布于:第八基地 来源:互联网 作者:天堂路上 时间:2017-05-12 点击:422
原创文章,转发请注明出处:http://noob.d8jd.com/fine/view/55829

用到的软件版本

系统:CentOS 6.5 64位

cmake-2.8.12.2
nginx版本:1.8.1
php版本:5.5.9
mysql版本:5.5.15

如果程序下载不下来的,我在后面放上百度云,里面有用到的所有软件包。

Linux下面因为从MySQL 5.5开始使用cmake来做config了,所以编译安装的会和5.1版本有些区别。不过总体来说还是差别不大

安装cmake环境

#cd /usr/local/src/
#wget http://www.cmake.org/files/v2.8/cmake-2.8.12.2.tar.gz
#tarx zvfc cmake-2.8.12.2.tar.gz
# cd cmake-2.8.12.2
#./configure
#make && make install
# cmake -version
cmake version 2.8.12.2

最后一句检查安装完成,OK,接下来安装Mysql

安装Mysql

#cd /usr/local/src/
下载Mysql安装包
#wget http://syslab.comsenz.com/downloads/linux/mysql-5.5.15-linux-x86_64-glibc23.tar.gz
解压
#tar xzvf mysql-5.5.15.tar.gz
把解压完的数据包移动到/usr/local/mysql/目录下
#mv mysql-5.5.15 /usr/local/mysql
#cd /usr/local/mysql
#cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_READLINE=1 -DWITH_SSL=system -DWITH_ZLIB=system -DWITH_EMBEDDED_SERVER=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_DATADIR=/data/mysql
#make && make install

安装完成后,Mysql基本已安装完成,默认密码root

mysql -uroot -p
Enter password: ****

查看msyql是否运行

# service mysqld status
MySQL running (11411)  
安装php
创建www用户
# useradd www
进入安装包下载目录
# cd /usr/local/src/
下载源码
# wget http://cn2.php.net/distributions/php-5.5.9.tar.gz
解压
# tar -zxvf php-5.5.9.tar.gz
php依赖很多类库,先把所缺少的类库安装完后再编译安装php通常下面这个语句会将php所依赖的包安装上
	# yum install -y gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel 
freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel 
glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs 
e2fsprogs-devel krb5-libs krb5-devel krb5-server libidn libidn-devel openssl openssl-devel 
openldap openldap-devel nss_ldap openldap-clients openldap-servers
yum能安装上大多数的包,但个别包是安装不上的,有可能找不到源文件。安装不上的包,可以手动编译安装。
手动安装libmcrypt
#cd /usr/local/src/
#wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/attic/libmcrypt/libmcrypt-2.5.7.tar.gz
#tar -zxvf libmcrypt-2.5.7.tar.gz
#cd libmcrypt-2.5.7
# ./configure prefix=/usr/local/libmcrypt
#make && make install
安装完依赖的类库后便可以编译php了
进入php安装目录
# cd php-5.5.9
环境检测及配置
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc 
--with-mysql=/usr/local/mysql --with-mysql-sock=/tmp --with-libxml-dir --with-gd 
--with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir 
--with-mcrypt=/usr/local/libmcrypt --enable-soap --enable-gd-native-ttf --enable-ftp 
--enable-mbstring --enable-exif --disable-ipv6 --enable-fpm
看到“Thank you for using PHP.”后,我们就可以进行编译安装了!
安装好php后,我们需要对php进行配置,拷贝php初始化配置文件
#cp php.ini-production /usr/local/php/etc/php.ini
#cd /usr/local/php/etc/
#cp php-fpm.conf.default php-fpm.conf
#vi php-fpm.conf
找到以下项:
user = nobody
group = nobody
修改为:
user = www
group = www
php-fpm进程数量优化,这个根据服务器配置和项目访问量进行设置,具体大家可以百度一下“PHP-FPM 配置优化”这篇文章。
pm = static
pm.max_children = 20
修改php.ini文件
#vi php.ini
修改display_errors = On ,这个是为了调试方便,建议投入生产环境后将此项修改为Off
启动php
#cp /usr/local/src/php-5.5.9/sapi/fpm/init.d.php-fpm /usr/local/php/sbin/init.d.php-fpm
#cd /usr/local/php/sbin/
#chmod 755 init.d.php-fpm 
#/usr/local/php/sbin/init.d.php-fpm start
看到“Starting php-fpm  done”,php便启动成功了。
#ps aux |grep php
可以看到以www用户启动了几个php-fpm进程
加入自启动
#vi /etc/rc.d/rc.local
最后边加入一行
/usr/local/php/sbin/init.d.php-fpm start
然后可以reboot测试下php-fpm是否自启动了。

安装Nginx
# cd /usr/local/src/
# wget http://nginx.org/download/nginx-1.8.1.tar.gz 
# tar  xvf nginx-1.8.1.tar.gz
编译Nginx前,需要安装所需要的类库
# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.34.tar.gz
# tar -zxvf pcre-8.34.tar.gz
pcre解压出来就可以,不需要编译安装

进入Nginx目录

# cd /usr/local/src/cd nginx-1.8.1/
# ./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/usr/local/nginx/logs/error.log --http-log-path=/usr/local/nginx/logs/access.log --pid-path=/usr/local/nginx/var/nginx.pid --lock-path=/usr/local/nginx/var/nginx.lock --http-client-body-temp-path=/dev/shm/nginx_temp/client_body --http-proxy-temp-path=/dev/shm/nginx_temp/proxy --http-fastcgi-temp-path=/dev/shm/nginx_temp/fastcgi --user=www --group=www --with-cpu-opt=pentium4F --without-select_module --without-poll_module --with-http_realip_module --with-http_sub_module --with-http_gzip_static_module --with-http_stub_status_module --without-http_ssi_module --without-http_userid_module --without-http_geo_module --without-http_memcached_module --without-http_map_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --with-pcre=/usr/local/src/pcre-8.34
编译安装Nginx
# make && make install
# mkdir /dev/shm/nginx_temp

编写nginx的启动脚本,并加入系统服务

# vi /etc/init.d/nginx 写入以下内容:

#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f nginx defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add nginx'

### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO

# Author:   licess
# website:  http://lnmp.org

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=nginx
NGINX_BIN=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/$NAME.conf
PIDFILE=/usr/local/nginx/logs/$NAME.pid

case "$1" in
    start)
        echo -n "Starting $NAME... "

        if netstat -tnpl | grep -q nginx;then
            echo "$NAME (pid `pidof $NAME`) already running."
            exit 1
        fi

        $NGINX_BIN -c $CONFIGFILE

        if [ "$?" != 0 ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
        ;;

    stop)
        echo -n "Stoping $NAME... "

        if ! netstat -tnpl | grep -q nginx; then
            echo "$NAME is not running."
            exit 1
        fi

        $NGINX_BIN -s stop

        if [ "$?" != 0 ] ; then
            echo " failed. Use force-quit"
            exit 1
        else
            echo " done"
        fi
        ;;

    status)
        if netstat -tnpl | grep -q nginx; then
            PID=`pidof nginx`
            echo "$NAME (pid $PID) is running..."
        else
            echo "$NAME is stopped"
            exit 0
        fi
        ;;

    force-quit)
        echo -n "Terminating $NAME... "

        if ! netstat -tnpl | grep -q nginx; then
            echo "$NAME is not running."
            exit 1
        fi

        kill `pidof $NAME`

        if [ "$?" != 0 ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
        ;;

    restart)
        $0 stop
        sleep 1
        $0 start
        ;;

    reload)
        echo -n "Reload service $NAME... "

        if netstat -tnpl | grep -q nginx; then
            $NGINX_BIN -s reload
            echo " done"
        else
            echo "$NAME is not running, can't reload."
            exit 1
        fi
        ;;

    configtest)
        echo -n "Test $NAME configure files... "

        $NGINX_BIN -t
        ;;

    *)
        echo "Usage: $0 {start|stop|force-quit|restart|reload|status|configtest}"
        exit 1
        ;;

esac

保存后,更改/etc/init.d/nginx的权限

#chmod 755 /etc/init.d/nginx

加入自启动

#chkconfig --add nginx
#chkconfig nginx on
修改nginx配置文件
#vi /usr/local/nginx/conf/nginx.conf ,清空文件,然后粘贴以下内容。
user  www www;

worker_processes auto;

error_log  /home/wwwlogs/nginx_error.log  crit;

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

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;

events
    {
        use epoll;
        worker_connections 51200;
        multi_accept on;
    }

http
    {
        include       mime.types;
        default_type  application/octet-stream;

        server_names_hash_bucket_size 128;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 50m;

        sendfile   on;
        tcp_nopush on;

        keepalive_timeout 60;

        tcp_nodelay on;

        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_buffer_size 64k;
        fastcgi_buffers 4 64k;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 256k;

        gzip on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.1;
        gzip_comp_level 2;
        gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;
        gzip_vary on;
        gzip_proxied   expired no-cache no-store private auth;
        gzip_disable   "MSIE [1-6]\.";

        #limit_conn_zone $binary_remote_addr zone=perip:10m;
        ##If enable limit_conn_zone,add "limit_conn perip 10;" to server section.

        server_tokens off;
        #log format
        log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
             '$status $body_bytes_sent "$http_referer" '
             '"$http_user_agent" $http_x_forwarded_for';
		access_log off;

server
    {
        listen 80 default_server;
        #listen [::]:80 default_server ipv6only=on;
        server_name www.lnmp.org;
        index index.html index.htm index.php;
        root  /home/www;

        #error_page   404   /404.html;
        include enable-php.conf;

        location /nginx_status
        {
            stub_status on;
            access_log   off;
        }

        location / {
           if (!-e $request_filename) {
                rewrite  ^(.*)$  /index.php?s=$1  last;
                break;
            }
         }

        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:8000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;
        }

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        location ~ /\.
        {
            deny all;
        }

        access_log  /home/wwwlogs/access.log  access;
    }
include vhost/*.conf;
}
接下来让我们启动Nginx
#/usr/local/nginx/sbin/nginx -t
如果出现syntax is ok 或者 test is successful 之类的提示,那么配置便没有问题了。
#service nginx start
提示"Starting Nginx: [  OK  ]"那么便可以了。
对Nginx操作
#/etc/init.d/nginx {start|stop|reload|restart|configtest}
也可以使用
#service nginx {start|stop|reload|restart|configtest}
所有软件包下载:https://pan.baidu.com/s/1o8JjSlO
到此搞定,如果有什么问题,请留言...
对我有帮助
(0)
0%
对我没帮助
(0)
0%
返回顶部
在线反馈
在线反馈