CentOS 7 源码搭建LNMP

Posted bigdevilking

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CentOS 7 源码搭建LNMP相关的知识,希望对你有一定的参考价值。

搭建 LNMP 环境

源码包版本 : 

CentOS Linux  7

nginx-1.15.1.tar.gz 

mysql-boost-5.7.21.tar.gz 

php-7.2.7.tar.gz

准备环境依赖包 :

yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel pcre-devel ncurses ncurses-devel bison cmake

安装nginx :

[[email protected] ~]# useradd -M -s /sbin/nologin nginx  # 创建nginx用户
[[email protected] ~]# tar xf nginx-1.15.1.tar.gz -C /usr/src/
[[email protected] ~]# cd /usr/src/nginx-1.15.1/
[[email protected] nginx-1.15.1]# ./configure   # 配置 编译 安装
> --prefix=/usr/local/nginx > --sbin-path=/usr/sbin/nginx > --conf-path=/etc/nginx/nginx.conf > --error-log-path=/var/log/nginx/error.log > --http-log-path=/var/log/nginx/access.log > --pid-path=/var/run/nginx.pid > --lock-path=/var/run/nginx.lock > --http-client-body-temp-path=/var/tmp/nginx/client > --http-proxy-temp-path=/var/tmp/nginx/proxy > --http-fastcgi-temp-path=/var/tmp/nginx/fcgi > --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi > --http-scgi-temp-path=/var/tmp/nginx/scgi > --user=nginx > --group=nginx > --with-pcre > --with-http_v2_module > --with-http_ssl_module > --with-http_realip_module > --with-http_addition_module > --with-http_sub_module > --with-http_dav_module > --with-http_flv_module > --with-http_mp4_module > --with-http_gunzip_module > --with-http_gzip_static_module > --with-http_random_index_module > --with-http_secure_link_module > --with-http_stub_status_module > --with-http_auth_request_module > --with-mail > --with-mail_ssl_module > --with-file-aio > --with-http_v2_module > --with-threads > --with-stream > --with-stream_ssl_module > &&make && make install

添加启动脚本:

[[email protected] nginx-1.15.1]# vim /etc/init.d/nginx 
#!/bin/sh 
# 
# nginx - this script starts and stops the nginx daemon 
# 
# chkconfig:   - 85 15 
# description: Nginx is an HTTP(S) server, HTTP(S) reverse  
#               proxy and IMAP/POP3 proxy server 
# processname: nginx 
# config:      /etc/nginx/nginx.conf 
# config:      /etc/sysconfig/nginx 
# pidfile:     /var/run/nginx.pid 
# Source function library. 
. /etc/rc.d/init.d/functions
# Source networking configuration. 
. /etc/sysconfig/network
# Check that networking is up. 
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: " 
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo 
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}
stop() {
    echo -n $"Stopping $prog: " 
    killproc $prog -QUIT
    retval=$?
    echo 
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
killall -9 nginx
}
restart() {
    configtest || return $?
    stop
    sleep 1
    start
}
reload() {
    configtest || return $?
    echo -n $"Reloading $prog: " 
    killproc $nginx -HUP
RETVAL=$?
    echo 
}
force_reload() {
    restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
    status $prog
}
rh_status_q() {
    rh_status >/dev/null 2>&1
}
case "$1" in
    start)
        rh_status_q && exit 0
    $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
      echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" 
        exit 2
esac

[[email protected] nginx-1.15.1]# chmod +x /etc/init.d/nginx 
[[email protected] nginx-1.15.1]# chkconfig -add nginx 
[[email protected] nginx-1.15.1]# vim /etc/nginx/nginx.conf   # 编辑nginx配置文件 , 关联php
user  nginx;                                                                                 
worker_processes  1;  
events {
    worker_connections  1024;
}
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    log_format  main  $remote_addr - $remote_user [$time_local] "$request" 
                      $status $body_bytes_sent "$http_referer" 
                      "$http_user_agent" "$http_x_forwarded_for";
    access_log  /var/log/nginx/access.log  main;
    server {
        listen       80; 
        server_name  192.168.94.111;
        location / { 
            root   /var/www/html;
            index index.php index.html index.htm;
        }   
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /var/www/html;
        }   
        location ~ .php$ {
            root           /var/www/html;
            fastcgi_pass   192.168.94.111:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /var/www/html/$fastcgi_script_name;
            include        fastcgi_params;
        }   
    }   
    sendfile        on; 
    #tcp_nopush     on; 
    keepalive_timeout  65; 
    #gzip  on; 
    include /etc/nginx/conf.d/*.conf;
}
[[email protected] nginx-1.15.1]# systemctl start nginx

测试:

技术分享图片

 

安装MySQL :

[[email protected] ~]# useradd -M -s /sbin/nologin mysql    # 创建mysql用户
[[email protected] ~]# tar xf mysql-boost-5.7.21.tar.gz -C /usr/src/
[[email protected] ~]# cd /usr/src/mysql-5.7.21/
[[email protected] mysql-5.7.21]# cmake > -DCMAKE_INSTALL_PREFIX=/usr/local/mysql > -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock > -DSYSCONFDIR=/etc > -DSYSTEMD_PID_DIR=/usr/local/mysql > -DDEFAULT_CHARSET=utf8 > -DDEFAULT_COLLATION=utf8_general_ci > -DWITH_INNOBASE_STORAGE_ENGINE=1 > -DWITH_ARCHIVE_STORAGE_ENGINE=1 > -DWITH_BLACKHOLE_STORAGE_ENGINE=1 > -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 > -DMYSQL_DATADIR=/usr/local/mysql/data > -DWITH_BOOST=boost > -DWITH_SYSTEMD=1 > && make -j4 && make install
[[email protected] mysql-5.7.21]# chown -R mysql.mysql /usr/local/mysql/
[[email protected] mysql-5.7.21]# vim /etc/my.cnf

[client]                                                                                     
port = 3306 
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1 

[[email protected] mysql-5.7.21]# chown mysql.mysql /etc/my.cnf
[[email protected] mysql-5.7.21]# echo -e PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH "
export PATH" >> /etc/profile
[[email protected] mysql-5.7.21]# source /etc/profile
[[email protected]ang mysql-5.7.21]# cd /usr/local/mysql/
[[email protected] mysql]# bin/mysqld > --initialize-insecure > --user=mysql > --basedir=/usr/local/mysql > --datadir=/usr/local/mysql/data/

[[email protected] mysql]# cp usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
[[email protected] mysql]# systemctl daemon-reload
[[email protected] mysql]# systemctl start mysqld

安装php :

[[email protected] ~]# tar xf php-7.2.7.tar.gz -C /usr/src
[[email protected] ~]# cd /usr/src/php-7.2.7/
[[email protected] php-7.2.7]# ./configure > --prefix=/usr/local/php > --with-config-file-path=/etc > --enable-fpm > --with-fpm-user=nginx  > --with-fpm-group=nginx > --enable-inline-optimization > --disable-debug > --disable-rpath > --enable-shared  > --enable-soap > --with-libxml-dir > --with-xmlrpc > --with-openssl > --with-mhash > --with-pcre-regex > --with-sqlite3 > --with-zlib > --enable-bcmath > --with-iconv > --with-bz2 > --enable-calendar > --with-curl > --with-cdb > --enable-dom > --enable-exif > --enable-fileinfo > --enable-filter > --with-pcre-dir > --enable-ftp > --with-gd > --with-openssl-dir > --with-jpeg-dir > --with-png-dir > --with-zlib-dir  > --with-freetype-dir > --enable-gd-jis-conv > --with-gettext > --with-gmp > --with-mhash > --enable-json > --enable-mbstring > --enable-mbregex > --enable-mbregex-backtrack > --with-libmbfl > --with-onig > --enable-pdo > --with-mysqli=mysqlnd > --with-pdo-mysql=mysqlnd > --with-zlib-dir > --with-pdo-sqlite > --with-readline > --enable-session > --enable-shmop > --enable-simplexml > --enable-sockets  > --enable-sysvmsg > --enable-sysvsem > --enable-sysvshm > --enable-wddx > --with-libxml-dir > --with-xsl > --enable-zip > --enable-mysqlnd-compression-support > --with-pear > --enable-opcache > && make -j4 && make install

[[email protected] php-7.2.7]# ln -s /usr/local/php/bin/* /usr/local/bin/
[[email protected] php-7.2.7]# php -v
PHP 7.2.7 (cli) (built: Aug  3 2018 01:03:35) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

[[email protected] php-7.2.7]# cp php.ini-production /etc/php.ini
[[email protected] php-7.2.7]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[[email protected] php-7.2.7]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
[[email protected] php-7.2.7]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[[email protected] php-7.2.7]# chmod +x /etc/init.d/php-fpm
[[email protected] php-7.2.7]# chkconfig --add php-fpm
[[email protected] php-7.2.7]# vim /usr/local/php/etc/php-fpm.d/www.conf
# 把listen = 127.0.0.0:9000 修改为 listen = 192.168.94.111:9000 
[[email protected] php-7.2.7]# systemctl start php-fpm
 

编写测试页面 :

[[email protected] ~]# vim /var/www/html/1.php 
<?php phpinfo(); ?>

测试 :

技术分享图片

 

[[email protected] ~]# vim /var/www/html/2.php 
<?php                                                                                        
$link=mysqli_connect(192.168.94.111,damowang,475541270);
if($link) echo "<h1>Success!!</h1>";
else echo "Fail!!";
?>

授权MySQL用户 :

[[email protected] ~]# mysql -uroot -p
Enter password:
mysql> grant all on *.* to damowang@% identified by 475541270

测试 : 

技术分享图片

 



以上是关于CentOS 7 源码搭建LNMP的主要内容,如果未能解决你的问题,请参考以下文章

CentOS 7.0源码包搭建LNMP

CentOS 7.0源码包搭建LNMP

Linux精华篇—CentOS 7.4下源码编译构建LNMP架构

Centos 7 下yum搭建lnmp环境(yum安装方式)

CentOS 7上源码编译安装和配置LNMP Web+phpMyAdmin服务器环境

CentOS源码安装搭建LNMP全过程(包括nginx,mysql,php,svn)转