多学一点(八)——LNMP环境搭建

来源:互联网 发布:龙轩导航 源码 编辑:程序博客网 时间:2024/06/16 17:06

在之前的博客中虽然已经总结过了 MySQL Apache 的安装、配置,但分开总结总是不太系统,这篇博客将会对LNMPLinux + Nginx + MySQL +PHP)做一个比较系统的总结。需要提醒的一点是,这里我们并没有将 NMP 都安装在 /usr/local/ 目录下,而是统一安装在了 /opt/ 目录下,在博客末尾给出的参考资料中提供了/usr/local/ 目录下的安装方法,如有需要可以参考。

 

1. MySQL

    这里我们使用的 MySQL 版本为 mysql-5.1.40,免编译安装包。首先进入 /usr/local/src/ 目录下载安装包:

[root@localhost ~]# cd /usr/local/src[root@localhost ~]# wget http://syslab.comsenz.com/downloads/linux/mysql-5.1.40-linux-i686-icc-glibc23.tar.gz

解压,移动到 /opt/ 目录下,并改名为 mysql

[root@localhost src]# tar -zxf mysql-5.1.40-linux-i686-icc-glibc23.tar.gz[root@localhost src]# mv mysql-5.1.40-linux-i686-icc-glibc23 /opt/mysql

添加 mysql 用户,不允许其登录:

[root@localhost src]# cd /opt/mysql[root@localhost mysql]# useradd -s /sbin/nologin mysql

创建 /data/mysql/目录,存放 MySQL 数据:

[root@localhost mysql]# mkdir -p /data/mysql[root@localhost mysql]# chown -R mysql:mysql !$

初始化 MySQL

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

复制、修改配置文件:

[root@localhost mysql]# /bin/cp ./support-files/my-large.cnf /etc/my.cnf

添加到服务中方便启动/ 关闭:

[root@localhost mysql]# cp ./support-files/mysql.server /etc/init.d/mysqld[root@localhost mysql]# chmod 755 !$[root@localhost mysql]# vim !$

修改其中两行,修改前分别为:

basedir=datadir=

修改后为:

basedir=/opt/mysqldatadir=/data/mysql

   保存退出。其中,basedir设置为 MySQL 的安装目录,对于 MySQL 来说,它的默认安装目录为/usr/local/mysql ,如果已经安装到了该目录下,则无需特意设置 basedir ,否则像本例中,将 MySQL 安装到了/opt/mysql 下,basedir 则要设置为 /opt/mysql 。同理,将 datadir 设置为和前面 mysql_install_db 中对应的 /data/mysql 。

   将 mysqld 添加到服务:

[root@localhost mysql]# chkconfig --add mysqld[root@localhost mysql]# chkconfig mysqld on[root@localhost mysql]# service mysql start / stop / restart

MySQL 相关命令路径添加到 PATH 变量中:

[root@localhost mysql]# vim /etc/profile

最下方添加如下一行内容:

export PATH=$PATH:/opt/mysql/bin[root@localhost mysql]# source !$

至此 MySQL 已经安装完毕,位于 /opt/ 目录下。如果启动 MySQL 失败,可以去 /data/mysql/ 目录下查看错误日志排查原因,如此处主机名为 localhost.localhostdomain,则错误日志名称通常为localhost.localdomain.err 。有关 MySQL 配置可以参考之前的博客,地址如下:

http://xitongjiagoushi.blog.51cto.com/9975742/1627864

 

2. PHP

这里我们使用的 PHP版本为 php-5.3.27,为源码包,需要编译安装。首先进入/usr/local/src/ 目录下载安装包:

[root@localhost ~]# cd /usr/local/src[root@localhost src]# wget http://am1.php.net/distributions/php-5.3.27.tar.gz

解压:

[root@localhost src]# tar -zxf php-5.3.27.tar.gz[root@localhost src]# cd php-5.3.27

添加 php-fpm 用户,不允许其登录:

[root@localhost php-5.3.27]# useradd -s /sbin/nologin php-fpm

编译、安装前的配置:

[root@localhost php-5.3.27]# ./configure \--prefix=/opt/php\      //指定PHP安装路径--with-config-file-path=/opt/php/etc\      //与上述路径对应--enable-fpm \--with-fpm-user=php-fpm\--with-fpm-group=php-fpm\--with-mysql=/opt/mysql\      //对应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

注意,在 ./configure 的过程中可能会有报出很多次错,这是缺少某些库造成的,下面列出一些错误信息及解决方法,供参考:

   (1)缺少 openssl库:

   configure: error: Cannot find OpenSSL's<evp.h>

   解决方法:yum 安装 openssl-devel

[root@localhost php-5.3.27]# yum install -yopenssl-devel

   (2)缺少 libcurl库:

   configure: error: Please reinstall thelibcurl distribution -easy.h should be in <curl-dir>/include/curl/

   解决方法:yum 安装 libcurl-devel

[root@localhost php-5.3.27]# yum install -ylibcurl-devel

   (3)缺少 jpeglib库:

   configure: error: jpeglib.h not found.

   解决方法:yum 安装 jpeglib-devel

[root@localhost php-5.3.27]# yum install -ylibjpeg-devel

   (4)缺少 libpng-devel库:

   configure: error: png.h not found.

   解决方法:yum 安装 libpng-devel

[root@localhost php-5.3.27]# yum install -ylibpng-devel

   (5)缺少freetype 库:

   configure: error: freetype.h not found.

   解决方法:yum 安装freetype-devel

[root@localhost php-5.3.27]# yum install -yfreetype-devel

   (6)缺少 libmcrypt库:

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

[root@localhost php-5.3.27]# yum install -ylibmcrypt-devel

上面给出的几种错误信息可能会涵盖不全具体安装过程中的错误,但是总结一下我们不难发现,可以根据报错提示大概猜出所需安装的文件名,而 -devel development 的缩写,表示开发包(无 -devel 和有 -devel 就类似于 jre jdk 的关系,jdk 包含了 jre,而 jre 不包含 jdk)。如果实在不知道要 yum 哪个名字,利用搜索引擎也能很快得到答案。如果 yum 安装失败,可能是 yum 源中无对应安装包,更换 yum 源(如 epel 源)或者直接搜索相应安装包使用 rpm 命令进行安装即可。这里给出 epel 源的安装方法,作为补充知识:

[root@localhost ~]# yum install -y epel-release

在 ./configure 成功后,开始编译、安装:

[root@localhost php-5.3.27]# make && make install

检验是否安装成功:

[root@localhost php-5.3.27]# echo $?

 

3.NGINX

这里我们使用的 NGINX版本为nginx- 1.4.4,为源码包,需要编译安装。首先进入 /usr/local/src/ 目录下载安装包:

[root@localhost ~]# cd /usr/local/src[root@localhost src]# wget http://nginx.org/download/nginx-1.4.4.tar.gz

解压:

[root@localhost src]# tar -zxf nginx-1.4.4.tar.gz

编译、安装前的配置:

[root@localhost src]# cd nginx-1.4.4[root@localhost nginx-1.4.4]#./configure \--prefix=/opt/nginx\      //指定nginx 安装路径--with-http_realip_module\--with-http_sub_module\--with-http_gzip_static_module\--with-http_stub_status_module  \--with-pcre

注意,在./configure 的过程中可能会报出一些错误(相对于 php 来说少了很多),这是缺少某些库造成的,下面列出一些错误信息及解决方法,供参考:

   (1HTTPrewrite错误:

   ./configure: error: the HTTP rewrite modulerequires the PCRE library.

   You can either disable the module by using--without-http_rewrite_module

   option, or install the PCRE library intothe system, or build the PCRE library

   statically from the source with nginx byusing --with-pcre=<path> option.

   解决方法:yum 安装 pcre-devel

[root@localhost nginx-1.4.4]# yum install-y pcre-devel

   (2)缺少 zlib 库:

   ./configure: error: the HTTP gzip modulerequires the zlib library.

   You can either disable the module by using--without-http_gzip_module

   option, or install the zlib library intothe system, or build the zlib library

   statically from the source with nginx byusing --with-zlib=<path> option.

   解决方法:yum 安装zlib-devel

[root@localhost nginx-1.4.4]# yum install-y zlib-devel

   (3)缺少 libxml2库:

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

   解决方法: yum 安装libxml2-devel

[root@localhost nginx-1.4.4]# yum install-y libxml2-devel

在 ./configure 成功后,开始编译、安装:

[root@localhost nginx-1.4.4]# make && make install

配置 nginx 成为服务:

[root@localhost nginx-1.4.4]# vim /etc/init.d/nginx
#!/bin/bash# chkconfig: -30 21# description:http service.# SourceFunction Library./etc/init.d/functions# Nginx Settings //下面三行要与 nginx 实际安装路径匹配NGINX_SBIN="/opt/nginx/sbin/nginx"NGINX_CONF="/opt/nginx/conf/nginx.conf"NGINX_PID="/opt/nginx/logs/nginx.pid"RETVAL=0prog="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=1esac exit $RETVAL

保存退出,添加到系统服务中:

[root@localhost nginx-1.4.4]# chmod 755 !$[root@localhost nginx-1.4.4]# chkconfig --add nginx[root@localhost nginx-1.4.4]# chkconfig nginx on

更改 nginx 配置:

[root@localhost nginx-1.4.4]# cd /opt/nginx[root@localhost nginx]# > ./conf/nginx.conf[root@localhost nginx]# vim !$
user nobodynobody;worker_processes2;error_log /opt/nginx/logs/nginx_error.logcrit;pid /opt/nginx/logs/nginx.pid;worker_rlimit_nofile51200; 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 /opt/nginx/client_body_temp;    proxy_temp_path /opt/nginx/proxy_temp;    fastcgi_temp_path /opt/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/plainapplication/x-javascript text/css text/htm application/xml; server{    listen 80;    server_name localhost;    index index.html index.htm index.php;    root /opt/nginx/html;     location ~ \.php$ {        include fastcgi_params;        fastcgi_pass unix:/tmp/php-fcgi.sock;        fastcgi_index index.php;        fastcgi_param SCRIPT_FILENAME /opt/nginx/html$fastcgi_script_name;    } } }

保存退出后,检验配置文件是否正确:

[root@localhost nginx]# sbin/nginx -t

显示如下信息则配置正确:

nginx: theconfiguration file /usr/local/nginx/conf/nginx.conf syntax is ok

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

启动 nginx

[root@localhost nginx]# service nginx start

如果启动失败,可以查看错误日志排查原因:

[root@localhost nginx]# vim ./logs/error.log

 

至此,我们已经将 MySQL PHP NGINX 安装完毕,通过能否解析php 文件检验是否安装成功:

[root@localhost nginx]# vim ./html/test.php
<?php    echo "testing php";?>

保存退出,测试:

[root@localhost nginx]# curl localhost/test.phptestingphp[root@localhost nginx]#

如上显示结果表明php 解析正确。

 

参考资料:

http://www.aminglinux.com/study_v2/chapter18.html

本文出自 “细桶假狗屎” 博客,请务必保留此出处http://xitongjiagoushi.blog.51cto.com/9975742/1631820

0 0