一、查看系统版本

[root@localhost ~]# cat /etc/redhat-release  

CentOS Linux release 7.6.1810 (Core) 

二、安装Mysql数据库

[root@localhost ~]# yum install wget -y

[root@localhost ~]# cd /usr/local/src

MySQL从5.7版本之后,boost是必须的,建议把系统自带的boost库卸载,源码编译安装高版本Boost库是为C++语言标准库提供扩展的一些C++程序库的总称

[root@localhost ~]# wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.25.tar.gz

#卸载系统自带boots库

[root@localhost ~]# yum -y remove boost-*  

#安装依赖包

[root@localhost ~]# yum install -y cmake make gcc gcc-c++ bison ncurses ncurses-devel

[root@localhost ~]# useradd -s /sbin/nologin -M mysql  //建立用户

创建数据存放目录

[root@localhost ~]# mkdir -p /data/mysql

[root@localhost ~]# tar -zxvf mysql-5.7.25.tar.gz 

[root@localhost ~]# cd mysql-5.7.25/

因为下载boost是国外源下载会比较慢,这个时候我们更改下配置文件

[root@localhost ~]# vi cmake/boost.cmake

SET(BOOST_DOWNLOAD_URL
  "http://sourceforge.net/projects/boost/files/boost/1.59.0/${BOOST_TARBALL}"
  )
更改为
SET(BOOST_DOWNLOAD_URL
  "http://mirrors.ahaoyw.com/autoconf/lin/other/boost_1_59_0.tar.gz"
  )

编译mysql

[root@localhost ~]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql  -DMYSQL_DATADIR=/data/mysql -DSYSCONFDIR=/etc -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=l -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/src/mysql-5.7.25/boost/

解释:

DCMAKE_INSTALL_PREFIX:指定MySQL程序的安装目录,默认/usr/local/mysql

DEFAULT_CHARSET:指定服务器默认字符集,默认latin1

DEFAULT_COLLATION:指定服务器默认的校对规则,默认latin1_general_ci

ENABLED_LOCAL_INFILE:指定是否允许本地执行LOAD DATA INFILE,默认OFF

WITH_COMMENT:指定编译备注信息

WITH_xxx_STORAGE_ENGINE:指定静态编译到mysql的存储引擎,MyISAM,MERGE,MEMBER以及CSV四种引擎默认即被编译至服务器,不需要特别指定。

WITHOUT_xxx_STORAGE_ENGINE:指定不编译的存储引擎

SYSCONFDIR:初始化参数文件目录

MYSQL_DATADIR:数据文件目录

MYSQL_TCP_PORT:服务端口号,默认3306

MYSQL_UNIX_ADDR:socket文件路径,默认/tmp/mysql.sock

编译会比较久请耐心等待

[root@localhost ~]# make (会占用大量CPU)

[root@localhost ~]# make -j 2  后面数字根据你CPU核数来决定)

[root@localhost ~]# make install

修改目录权限

[root@localhost ~]# chown -R mysql:mysql /usr/local/mysql

将原服务器配置文件移动备份或删除

[root@localhost ~]# mv /etc/my.cnf{,.bak}

因5.7版本无此配置模板文件/server/mysql/support-files/my-default.cnf   

#自行编写my.cnf文件

[root@localhost ~]# vi /etc/my.cnf

[mysqld]
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
#default
user = mysql
basedir = /usr/local/mysql
datadir = /data/mysql
port = 3306
pid-file = /usr/local/mysql/mysql.pid
socket = /tmp/mysql.sock
character-set-server=utf8
[client]
socket = /tmp/mysql.sock

设置开机启动项,权限之类的

[root@localhost ~]# cp /usr/local/src/mysql-5.7.25/support-files/mysql.server /etc/init.d/mysqld

[root@localhost ~]# vi /etc/init.d/mysqld

修改配置文件段为以下内容

basedir=/usr/local/mysql
datadir=/data/mysql

[root@localhost ~]# chmod +x /etc/init.d/mysqld

[root@localhost ~]# chkconfig --add mysqld

[root@localhost ~]# chkconfig mysqld on

初始化数据库

[root@localhost ~]# /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql

image.png

启动服务

[root@localhost ~]# /etc/init.d/mysqld start

加入系统变量

[root@localhost ~]# echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile    //加入系统变量

[root@localhost ~]# source /etc/profile   刷新变量开机加载

设置MySQL密码

[root@localhost ~]# /usr/local/mysql/bin/mysqladmin -u root password 你要的密码

image.png

三、编译PHP7.1

首先添加依赖应用

[root@localhost ~]# yum install -y gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libpng libpng-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses curl openssl-devel gdbm-devel db4-devel libXpm-devel libX11-devel gd-devel gmp-devel readline-devel libxslt-devel expat-devel xmlrpc-c xmlrpc-c-devel libmcrypt

[root@localhost ~]# cd /usr/local/src

[root@localhost ~]# http://mirrors.ahaoyw.com/autoconf/lin/PHP/php-7.1.22.tar.gz

[root@localhost ~]# tar -zxvf php-7.1.22.tar.gz

[root@localhost ~]# cd php-7.1.22

[root@localhost ~]# useradd -s /sbin/nologin www  -M 新建用户并禁止登入

[root@localhost ~]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www --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  --with-openssl

报错:

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

解决办法

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

[root@localhost ~]#yum install -y libmcrypt-devel

安装完成后,重新执行下configure 配置段

[root@localhost ~]#make && make install

[root@localhost ~]# cp php.ini-production /usr/local/php/etc/php.ini  

[root@localhost ~]# vi /usr/local/php/etc/php-fpm.conf

[global]
pid = /usr/local/php/var/run/php-fpm.pid
error_log = /usr/local/php/var/log/php-fpm.log 日志
[www]
listen = /tmp/php-fcgi.sock
listen.mode = 666 
user = www
group = www
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

# /usr/local/php/sbin/php-fpm -t  //验证配置是否正确

显示如下,表示配置成功正确

image.png

设置开机启动项,权限之类的

[root@localhost ~]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm   拷贝启动文件

[root@localhost ~]# chmod 755 /etc/init.d/php-fpm   设置权限

[root@localhost ~]# chkconfig --add php-fpm   加入开机服务

[root@localhost ~]# chkconfig php-fpm on   开机启动

[root@localhost ~]# /etc/init.d/php-fpm start  启动php-fpm

[root@localhost ~]# ps aux |grep php-fpm   检查php-fpm是否启动

image.png

四、安装nginx1.17

[root@localhost ~]# cd /usr/local/src/

[root@localhost ~]#wget http://mirrors.ahaoyw.com/autoconf/lin/Nginx/nginx-1.16.0.tar.gz

 新建用户并禁止登入//如果刚才php用的是这个用户可以不用在新建直接调用

[root@localhost ~]# useradd -s /sbin/nologin www  -M

[root@localhost ~]# tar -zxvf nginx-1.16.0.tar.gz

[root@localhost ~]# cd nginx-1.16.0

[root@localhost ~]# ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --without-http_rewrite_module --with-http_ssl_module --with-pcre 

make && make install

编辑nginx启动文件

[root@localhost ~]# vi  /etc/init.d/nginxd

#!/bin/bash
# chkconfig: - 30 21
# 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

编辑完后,设置下启动文件的权限

[root@localhost ~]# chmod 755 /etc/init.d/nginxd

加入系统服务并设置开机启动

[root@localhost ~]# chkconfig --add nginxd

[root@localhost ~]# chkconfig nginxd  on

替换nginx默认的配置文件

[root@localhost ~]# cd /usr/local/nginx/conf/

[root@localhost ~]# >nginx.conf

[root@localhost ~]# vim nginx.conf //写入如下内容

user www www;
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 ~]# /usr/local/nginx/sbin/nginx -t 验证配置文件是否正确

image.png

[root@localhost ~]# /etc/init.d/nginxd  start   //启动

image.png

[root@localhost ~]# netstat -lntp |grep 80   //验证是否监听80

image.png

[root@localhost ~]# vi /usr/local/nginx/html/index.php  添加一个php文件测试是否正常解析

测试

有防护墙记得关闭防护墙或者允许80端口通过

image.png