linux运维我爱编程

centos7.4下源码编译安装LNMP服务

2018-05-27  本文已影响61人  dabule

本次实验用Nginx+Mysql+PHP的源码包来编译安装一个LNMP系统,实验之前先做一些准备工作.

前期准备工作:

  1. 下载对应的源码包"
    可以将源码包统一放在一个文件路径下方便管理,这个选/usr/local/src,直接用wget命令下载源码包即可,

Mysql源码包地址:

http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz

PHP源码包的下载地址:

http://cn2.php.net/distributions/php-5.6.30.tar.gz

Nginx源码包的下载地址:

http://nginx.org/download/nginx-1.12.1.tar.gz

2.安装环境及依赖关系:
这里先提供一些环境按键是需要的一下服务,比如:gcc,prec-devel等等,在编译./configure的时候配置不一样所需要依赖的包有可能也不一样,这里不一一列举了,提供一个解决依赖问题的思路.

在./configure配置好选项后检查时如果缺少了哪一个依赖包会有一个configure:error:please reinstall XXXX distribution 的语句,看到这个你就可以yum -yinstall XXXX或XXXX.devel,然后再重新./configure编译一次,遇到依赖问题在安装在编译直到成功就ok了

这个就是编译安装时遇上依赖问题的一种排错方法.

这里简单列举一些依赖文件,

Mysql:
perl-Module-Install.noarch
libaio*

PHP:
libcurl-devel

Nginx:
pcre-devel

下载好源码包和依赖关系之后就可以开始配置了.

1. Mysql安装配置

解压缩源码包:


tar -zxvf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz

移动目录并进入:

mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql

cd /usr/local/mysql

创建数据目录及用户名:

mkdir /data/

useradd mysql

执行初始化脚本:


./scripts/mysql_install_db --user=mysql --datadir=/data/mysql

copy配置文件并配置:

cp support-files/my-default.cnf /etc/my.cnf

--------------分割线--------------

在vim /etc/my.cnf配置如下:
[root@localhost mysql]# vim /etc/my.cnf

# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
datadir =/data/mysql   #添加这个数据存放路径
# port = .....
# server_id = .....
# socket = .....
socket = /tmp/mysql.sock   #还有这个,其他暂时不用动
...省略...

完成以上操作就可以启动Mysql服务了:


/etc/init.d/mysqld start

检查服务是否启动:

[root@localhost mysql]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      46168/nginx: master 
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      1548/dnsmasq        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1165/sshd           
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1163/cupsd          
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1396/master         
tcp        0      0 127.0.0.1:6010          0.0.0.0:*               LISTEN      5103/sshd: root@pts 
tcp        0      0 127.0.0.1:6011          0.0.0.0:*               LISTEN      5103/sshd: root@pts 
tcp        0      0 127.0.0.1:6012          0.0.0.0:*               LISTEN      5103/sshd: root@pts 
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::22                   :::*                    LISTEN      1165/sshd           
tcp6       0      0 ::1:631                 :::*                    LISTEN      1163/cupsd          
tcp6       0      0 ::1:25                  :::*                    LISTEN      1396/master         
tcp6       0      0 ::1:6010                :::*                    LISTEN      5103/sshd: root@pts 
tcp6       0      0 ::1:6011                :::*                    LISTEN      5103/sshd: root@pts 
tcp6       0      0 ::1:6012                :::*                    LISTEN      5103/sshd: root@pts 
tcp6       0      0 :::3306                 :::*                    LISTEN      5939/mysqld 

#看到服务已经启动了

加入开机启动:

chkconfig --add mysqld
chkconfig mysqld on

2. PHP安装及配置

编译之前在此提醒一下,准备工作中的依赖问题提醒,这里并没有全部列出所有的依赖包,授人鱼不如授人与渔,试验环境及要安装的选项不同请按需自行安装缺少的依赖包.

解压并进入目录:

tar -zxvf php-5.6.30.tar.gz

cd php-5.6.30

创建 php-fpm用户:

useradd -s /sbin/nologin php-fpm

./configure:这里的选项很多,报错缺那个包就装那个包就ok了.


./configure --prefix=/usr/local/php-fpm --with-config-file-path=/usr/local/php-fpm/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-pdo-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 --with-pear --with-curl

执行编译安装:

make && make install

copy配置文件

cp php.ini-production /usr/local/php-fpm/etc/php.ini

创建vim /usr/local/php-fpm/etc/php-fpm.conf:

[root@localhost php-5.6.30]# vim /usr/local/php-fpm/etc/php-fpm.conf

[global]
pid = /usr/local/php-fpm/var/run/php-fpm.pid
error_log = /usr/local/php-fpm/var/log/php-fpm.log
[www]
listen = /tmp/php-fcgi.sock   #监听ip和端口,端口默认为9000
listen.mode = 666    #用来定义php-fcgi.sock文件的权限
user = php-fpm  
group = php-fpm
pm = dynamic   #后面这些都是关于进程的信息
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
~                     
:wq

拷贝启动脚本并修改权限:

cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

chmod 755 /etc/init.d/php-fpm

设置开机启动:

chkconfig --add php-fpm
chkconfig php-fpm on

检查配置服务:

/usr/local/php-fpm/sbin/php-fpm -t

启动服务:

service php-fpm start

检查是否启动:

[root@localhost conf]# ps aux | grep php
root      42351  0.0  0.2 123448  4736 ?        Ss   15:39   0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm   42352  0.0  0.2 125532  4900 ?        S    15:39   0:00 php-fpm: pool www
php-fpm   42353  0.0  0.3 125532  6476 ?        S    15:39   0:00 php-fpm: pool www
php-fpm   42354  0.0  0.2 125532  4904 ?        S    15:39   0:00 php-fpm: pool www
php-fpm   42355  0.0  0.2 125532  4904 ?        S    15:39   0:00 php-fpm: pool www
php-fpm   42356  0.0  0.2 125532  4908 ?        S    15:39   0:00 php-fpm: pool www
php-fpm   42357  0.0  0.2 125532  4908 ?        S    15:39   0:00 php-fpm: pool www
...省略...

服务启动成功了.

3. Nginx安装配置

解压缩并进入目录:

tar -zxvf nginx-1.12.1.tar.gz

cd nginx-1.12.1/

执行configure 检查编译环境:参数方面根据自己的需求去配置,依赖问题还是缺哪个包就装哪个包

./configure --prefix=/usr/local/nginx

执行编译:

make && make install

创建启动脚本: 在vim /etc/init.d/nginx中创建

#!/bin/bash
# chkconfig: - 30 21   #这个和下面的选项好正确,否则会报service nginx does not support chkconfig的错误,
# 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

修改权限:

chmod 755 /etc/init.d/nginx

加入服务列表并开机启动:

chkconfig --add nginx

chkconfig nginx on

修改nginx.conf文件:文件路径在/usr/local/nginx/conf下,备份原来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
{
    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 /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/htm 
    application/xml;
    server
    {
        listen 80;
        server_name localhost;
        index index.html index.htm index.php;
        root /usr/local/nginx/html;
        location ~ \.php$ 
        {
            include fastcgi_params;
            fastcgi_pass unix:/tmp/php-fcgi.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
        }    
    }
}

检查错误:

[root@localhost mysql]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

启动Nginx:

service nginx start

检查服务是否启动:

[root@localhost mysql]# netstat -lntp |grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      46168/nginx: master

到这里LNMP服务已经搭建好了,现在来检查一下nginx是否能解析php,只要在在nginx的/usr/local/nginx/html目录下创建一个php文件,然后通过浏览器访问就能看到效果了.

[root@localhost php-5.6.30]# vim /usr/local/nginx/html/test.php 

<?php
        phpinfo();
?>
~       

:wq

如果php生效了会出现如下图片:

Nginx的php功能生效.jpg
上一篇下一篇

猜你喜欢

热点阅读