CentOS6编译安装LNMP

6 篇文章 0 订阅
3 篇文章 0 订阅

1.编译安装各个程序

1.1 编译安装nginx1.10

下载nginx源码:
1
wget http://124.202.164.11/files/3182000009C41C0C/nginx.org/download/nginx-1.10.3.tar.gz
解压 ,移动 到指定目录:
1
tar -xvf nginx-1.10.3.tar.gz
2
mv nginx-1.10.3 /usr/local/nginx
编译安装之前的准备:
安装升级依赖包
1
yum -y install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel
新建nginx用户和组
1
useradd nginx -s /sbin/nologin -M  #不创建家目录,不能登录
我们编译安装时需要定义各种配置需要的目录进行,所以需要新建一些目录:
1
#日志存储目录:
2
mkdir /var/log/nginx
3
#各种缓存目录,客户端,代理,fastcgi目录:
4
mkdir -pv /var/tmp/nginx/{client,proxy,fastcgi}
下载pcre,使nginx支持正则表达式
1
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz
2
tar -xvf 
编译安装:
1
./configure \
2
--prefix=/usr/local/nginx \
3
--sbin-path=/usr/sbin/nginx \
4
--conf-path=/etc/nginx/nginx.conf \
5
--user=nginx \
6
--group=nginx \
7
--error-log-path=/var/log/nginx/error.log \
8
--http-log-path=/var/log/nginx/access.log \
9
--pid-path=/var/run/nginx/nginx.pid \
10
--lock-path=/var/lock/nginx.lock \
11
--with-http_ssl_module \
12
--with-http_stub_status_module \
13
--with-http_gzip_static_module \
14
--http-client-body-temp-path=/var/tmp/nginx/client \
15
--http-proxy-temp-path=/var/tmp/nginx/proxy \
16
--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi \
17
--with-pcre=/usr/local/pcre
18
#编译
19
make && make install
注意:如果编译安装过程中出现错误,屏幕上会出现报错信息。大部分的编译报错是由于缺少某些库的支持引起的。
另外,在没有改动源代码的情况下,如果需要重新编译安装nginx,就不必再使用configure脚本自动生成makefile了,可以像删除上次安装的nginx路径,解压源码包重新按上面的编译安装步骤来一遍。
检查安装是否成功:
1
#查看配置文件目录:
2
[root@www ~]# ls /etc/nginx/
1
查看nginx的版本:
2
[root@www nginx]# nginx -v
3
nginx version: nginx/1.10.3
4
测试nginx的配置文件:
5
[root@www nginx]# nginx -t
6
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
7
nginx: configuration file /etc/nginx/nginx.conf test is successful
配置nginx服务
1
vim /etc/init.d/nginx
1
#!/bin/bash
2
#
3
# nginx - this script starts and stops the nginx daemon
4
#
5
# chkconfig:  - 85 15 
6
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
7
#              proxy and IMAP/POP3 proxy server
8
# processname: nginx
9
# config:      /etc/nginx/nginx.conf
10
# config:      /etc/sysconfig/nginx
11
# pidfile:    /var/run/nginx.pid
12
  
13
# Source function library.
14
. /etc/rc.d/init.d/functions
15
  
16
# Source networking configuration.
17
. /etc/sysconfig/network
18
  
19
# Check that networking is up.
20
[ "$NETWORKING" = "no" ] && exit 0
21
  
22
nginx="/usr/sbin/nginx"
23
#注意:这个路径一定要跟nginx程序路径保持一致,否则nginx服务不能启动
24
prog=$(basename $nginx)
25
  
26
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
27
  
28
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
29
  
30
lockfile=/var/lock/subsys/nginx
31
  
32
make_dirs() {
33
  # make required directories
34
  user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
35
  options=`$nginx -V 2>&1 | grep 'configure arguments:'`
36
  for opt in $options; do
37
      if [ `echo $opt | grep '.*-temp-path'` ]; then
38
          value=`echo $opt | cut -d "=" -f 2`
39
          if [ ! -d "$value" ]; then
40
              # echo "creating" $value
41
              mkdir -p $value && chown -R $user $value
42
          fi
43
      fi
44
  done
45
}
46
  
47
start() {
48
    [ -x $nginx ] || exit 5
49
    [ -f $NGINX_CONF_FILE ] || exit 6
50
    make_dirs
51
    echo -n $"Starting $prog: "
52
    daemon $nginx -c $NGINX_CONF_FILE
53
    retval=$?
54
    echo
55
    [ $retval -eq 0 ] && touch $lockfile
56
    return $retval
57
}
58
  
59
stop() {
60
    echo -n $"Stopping $prog: "
61
    killproc $prog -QUIT
62
    retval=$?
63
    echo
64
    [ $retval -eq 0 ] && rm -f $lockfile
65
    return $retval
66
}
67
  
68
restart() {
69
    configtest || return $?
70
    stop
71
    sleep 1
72
    start
73
}
74
  
75
reload() {
76
    configtest || return $?
77
    echo -n $"Reloading $prog: "
78
    killproc $nginx -HUP
79
    RETVAL=$?
80
    echo
81
}
82
  
83
force_reload() {
84
    restart
85
}
86
  
87
configtest() {
88
  $nginx -t -c $NGINX_CONF_FILE
89
}
90
  
91
rh_status() {
92
    status $prog
93
}
94
  
95
rh_status_q() {
96
    rh_status >/dev/null 2>&1
97
}
98
  
99
case "$1" in
100
    start)
101
        rh_status_q && exit 0
102
        $1
103
        ;;
104
    stop)
105
        rh_status_q || exit 0
106
        $1
107
        ;;
108
    restart|configtest)
109
        $1
110
        ;;
111
    reload)
112
        rh_status_q || exit 7
113
        $1
114
        ;;
115
    force-reload)
116
        force_reload
117
        ;;
118
    status)
119
        rh_status
120
        ;;
121
    condrestart|try-restart)
122
        rh_status_q || exit 0
123
            ;;
124
    *)
125
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
126
        exit 2
127
esac
将nginx加入开机启动
1
chmod +x /etc/rc.d/init.d/nginx  #设置执行权限
2
chkconfig --add nginx
3
chkconfig nginx on
4
chkconfig --list|grep nginx  #检查是否加入
检查:
1
service nginx start
2
netstat -tnulp|grep nginx
ng inx常用命令
-v:显示nginx版本
-t:测试配置文件时候有问题
-s:stop, quit, reopen, reload这4种状态
-c:要加载的配置文件路径

1.2 编译安装php7(php-fpm模式)

下载php7
1
wget http://at2.php.net/distributions/php-7.0.19.tar.gz
解压,移动到指定目录
1
tar -xvf php-7.0.19.tar.gz
2
mv php-7.0.19 /usr/local/php
依赖包安装
1
yum -y install gcc gcc-c++ libxml2 libxml2-devel bzip2 bzip2-devel mhash mhash-devel\
2
bzip2-devel libmcrypt libmcrypt-devel libzip \
3
psmisc.x86_64 recode recode-devel libtidy libtidy-devel  libevent libevent-devel
软链接mysql相关文件:
1
ln -s /usr/local/mysql/lib/libmysqlclient.so /usr/lib64/ 
2
ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/libmysqlclient.so.18
编译安装:
1
./configure \
2
--prefix=/usr/local/php \
3
--with-config-file-path=/usr/local/php/etc \
4
--enable-inline-optimization \
5
--disable-debug \
6
--disable-rpath \
7
--enable-shared \
8
--enable-calendar \
9
--enable-opcache \
10
--enable-fpm \
11
--with-mysqli=/usr/local/mysql/bin/mysql_config \
12
--with-pdo-mysql=/usr/local/mysql \
13
--with-gettext \
14
--enable-mbstring \
15
--with-iconv \
16
--with-mcrypt \
17
--with-mhash \
18
--with-openssl \
19
--enable-bcmath \
20
--enable-soap \
21
--with-libxml-dir \
22
--enable-pcntl \
23
--enable-shmop \
24
--enable-sysvmsg \
25
--enable-sysvsem \
26
--enable-sysvshm \
27
--enable-sockets \
28
--enable-session \
29
--enable-xml \
30
--with-curl \
31
--with-zlib \
32
--enable-zip \
33
--with-bz2 \
34
--with-gd \
35
--with-freetype-dir \
36
--with-jpeg-dir \
37
--with-png-dir
注意:只有出现以下界面时才表示安装配置成功,其他的都不对:
如果mysql没有安装在本机上,其中有几个参数需要修改一下:
1
--with-mysql
2
--with-mysqli
3
--with-pdo-mysql
上面几个参数设置为空值即可。
说明:
PHP5.3以上版本,为了链接MySQL数据库,可以指定mysqlnd,这样在本机就不需要先安装MySQL或MySQL开发包。 mysqlnd从php 5.3开始可用,可以编译时绑定到它(而不用和具体的MySQL客户端库绑定形成依赖)。
从PHP 5.4开始,对于未明确指定--with-mysql的情形,mysql本地驱动将会被安装。
1
make && make install
make
 make install
 
复制配置文件到指定目录下
1
cd /usr/local/php/etc/php-fpm.d
2
cp www.conf.default www.conf
3
cd /usr/local/php/
4
cp php.ini-production /usr/local/php/etc/php.ini
5
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
6
cp /usr/local/php/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
修改php.ini中的时区设置
1
#vim /usr/local/php/etc/php.ini
2
date.timezone = Asia/Shanghai
测试php配置
1
/usr/local/php/sbin/php-fpm -t
在这一步的时候,突然报了个错,错误提示“ Nothing matches the include pattern '/usr/local/php/etc/php-fpm.d/*.conf' from /usr/local/php/etc/php-fpm.conf at line 125.”
后来在网上找到了修复方法,地址:http://d-prototype.com/archives/6230
解决方法如下:
1
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
然后再测试就可以看到:
提示“successful”就表示配置没有问题了。
配置php-fpm服务
1
chmod +x /etc/init.d/php-fpm
启动php-fpm
1
service php-fpm start
2
netstat -tnulp|grep php   #可以看到监听的9000端口启动了

1.3 编译安装mysql5.5

下载mysql5.5
1
wget https://cdn.mysql.com//Downloads/MySQL-5.5/mysql-5.5.56-linux-glibc2.5-x86_64.tar.gz
下载好之后,解压,移动到指定目录
1
tar -xvf mysql-5.5.11-linux2.6-x86_64.tar.gz  #我是用的是另一个5.5的版本
添加用户,更改目录权限
1
groupadd -r mysql
2
useradd -r -g mysql -s /sbin/nologin mysql
3
chown -R mysql:mysql /usr/local/mysql
安装mysql
1
cd /usr/local/mysql
2
./scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
配置mysql路径
1
echo "export MYSQL_HOME=/usr/local/mysql" >> /etc/profile
2
echo "export PATH=$MYSQL_HOME/bin:$PATH" >> /etc/profile
3
source /etc/profile
拷贝启动程序
1
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
2
chkconfig --add mysqld
3
chkconfig mysqld on
拷贝配置文件
1
mv /etc/my.cnf /etc/my.bak
2
cp support-files/my-medium.cnf /etc/my.cnf
1
ln -sv /var/lib/mysqld/mysql.sock /tmp/mysql.sock
2
chown -R mysql.mysql /var/log/mysql
启动服务
1
service mysqld start
2
netstat -tnulp|grep mysql  #检查是否启动
使用mysql_secur做一些安全设置
1
/usr/local/mysql/bin/mysql_secure_installation
设置root登录的密码,禁止root用户远程登录,去除test库等设置。
登录测试


看到上面的就表示登陆成功了。

2.配置nginx支持php-fpm

2.1 nginx配置

建议配置如下
1
user  nginx nginx;
2
worker_processes  2;
3
4
error_log /var/log/nginx/error.log;
5
pid       /var/run/nginx/nginx.pid;
6
7
events {
8
    use epoll;
9
    worker_connections  51200;
10
}
11
12
http {
13
    include       mime.types;
14
    default_type  application/octet-stream;
15
16
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
17
                      '$status $body_bytes_sent "$http_referer" '
18
                      '"$http_user_agent" "$http_x_forwarded_for"';
19
    access_log  /var/log/nginx/access.log  main;
20
21
    sendfile        on;
22
    tcp_nopush     on;
23
    keepalive_timeout  65;
24
25
    #配置gzip功能
26
    gzip  on;
27
    gzip_min_length 1k;
28
    gzip_buffers    4 8k;
29
    gzip_http_version   1.1;
30
    gzip_types  text/plain application/x-javascripts text/css test/html application/xml;
31
32
    #配置fastcgi功能
33
    #设置nginx与fastcgi服务器建立连接的超时时间
34
    fastcgi_connect_timeout 200;
35
    #设置nginx向fastcgi发送请求之后等待的超时时间
36
    fastcgi_send_timeout    200;
37
    #设置nginx从fastcgi读取应答数据时等待的超时时间
38
    fastcgi_read_timeout    200;
39
    #设置每次从fastcgi读取应答数据使用的缓存区大小
40
    fastcgi_buffer_size   64k;
41
    fastcgi_buffers    4  64k;
42
    #设置每次将来自fastcgi的应答数据写入本地临时文件时数据块的大小上限
43
    fastcgi_temp_file_write_size 128k;
44
45
    server {
46
        listen       80;
47
        server_name  localhost;
48
        index index.php index.htm index.html;
49
        root /var/www/html;
50
        location / {
51
            try_files $uri $uri/ /index.php?$args;
52
        }
53
        location ~ .*\.(php|php5)?$ {
54
            include fastcgi.conf;
55
            fastcgi_pass 127.0.0.1:9000;
56
            fastcgi_index index.php;
57
            #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
58
            #include        fastcgi_params;
59
        }
60
        
61
        #error_page  404              /404.html;
62
        # redirect server error pages to the static page /50x.html
63
        #
64
65
        error_page   500 502 503 504  /50x.html;
66
        location = /50x.html {
67
            root   html;
68
        }
69
    }
70
}
根路径设置为/var/www/html。

2.2 测试配置

现在我们要测试一下nginx与php的连通性
/var/www/html目录下创建index.php文件
1
<html>
2
<h1>This is a php test page.</h1>
3
<?php
4
phpinfo();
5
?>
6
</html>
启动php-fpm服务,启动nginx服务,使用浏览器访问服务器80端口,可以看到如下内容,里面列出了php相关的配置

 看到上面的界面,表示我们已经配置成功了。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值