CentOS 编译WEB环境(PHP+Mysql+Nginx)

分类:技术 3.07k浏览

笔记。后期方便复制粘贴

一、前期准备 makeg++

yum -y install gcc automake autoconf libtool make gcc-c++ glibc

二、安装相关组件

1、下载解压pcre

wget https://sourceforge.net/projects/pcre/files/pcre/8.41/pcre-8.41.tar.gz

tar -zxvf pcre-8.41.tar.gz

2、下载解压zlib

wget https://sourceforge.net/projects/libpng/files/zlib/1.2.11/zlib-1.2.11.tar.gz

tar -zxvf zlib-1.2.11.tar.gz

3、下载解压openssl

wget https://www.openssl.org/source/openssl-1.1.0b.tar.gz

tar -zxvf openssl-1.1.0b.tar.gz

4、下载nginx

wget http://nginx.org/download/nginx-1.12.2.tar.gz

tar -zxvf nginx-1.12.2.tar.gz

三、添加用户组

groupadd -r www

useradd -r -g www www

四、 编译安装nginx

目录定为:/usr/local/nginx/

cd nginx-1.12.2/

./configure –prefix=/usr/local/nginx –sbin-path=/usr/local/nginx/sbin –conf-path=/usr/local/nginx/nginx.conf –pid-path=/usr/local/nginx/nginx.pid –user=www –group=www –with-http_ssl_module –with-http_realip_module –with-http_gzip_static_module –with-http_stub_status_module –with-select_module –with-poll_module –error-log-path=/usr/local/nginx/log/error.log –http-log-path=/usr/local/nginx/log/access.log  –with-pcre=/root/pcre-8.41 –with-zlib=/root/zlib-1.2.11 –with-openssl=/root/openssl-1.1.0b

设置完毕后提示,不需要执行SSh

  nginx path prefix: “/usr/local/nginx”

  nginx binary file: “/usr/local/nginx/sbin”

  nginx modules path: “/usr/local/nginx/modules”

  nginx configuration prefix: “/usr/local/nginx”

  nginx configuration file: “/usr/local/nginx/nginx.conf”

  nginx pid file: “/usr/local/nginx/nginx.pid”

  nginx error log file: “/usr/local/nginx/log/error.log”

  nginx http access log file: “/usr/local/nginx/log/access.log”

  nginx http client request body temporary files: “client_body_temp”

  nginx http proxy temporary files: “proxy_temp”

  nginx http fastcgi temporary files: “fastcgi_temp”

  nginx http uwsgi temporary files: “uwsgi_temp”

  nginx http scgi temporary files: “scgi_temp”

make

make install

五、修改nginx.conf

vi /usr/local/nginx/conf/nginx.conf

2、创建conf vhosts文件,方便放网站配置

mkdir conf

cd conf

mkdir vhosts

并将nginx.conf的默认配置文件创建为 default.conf

vi default.conf

并把下面的引用文件放入nginx引用

include /usr/local/nginx/conf/vhosts/*.conf;

六、加入系统变量,方便下次启动

1、启动nginx需要执行/usr/local/nginx/sbin

vi命令打开/etc/profile文件,在文件最末尾加上如下代码

export NGINX_HOME=/usr/local/nginx

export PATH=$PATH:$NGINX_HOME/sbin

保存修改后,使用source命令重新加载配置文件

source /etc/profile

可使用echo $PATH 命令查看环境变量中是否已经加入了相关的路径

2、加入系统服务

vi命令在/etc/init.d/目录下创建一个nginx文件

vi /etc/init.d/nginx

内容

#!/bin/bash

# chkconfig: – 85 15

PATH=/usr/local/nginx

DESC=”nginx daemon”

NAME=nginx

DAEMON=$PATH/sbin

CONFIGFILE=$PATH/$NAME.conf

PIDFILE=$PATH/sbin

SCRIPTNAME=/etc/init.d/$NAME

set -e

[ -x “$DAEMON” ] || exit 0

do_start() {

$DAEMON -c $CONFIGFILE || echo -n “nginx already running”

}

do_stop() {

$DAEMON -s stop || echo -n “nginx not running”

}

do_reload() {

$DAEMON -s reload || echo -n “nginx can’t reload”

}

case “$1” in

start)

echo -n “Starting $DESC: $NAME”

do_start

echo “.”

;;

stop)

echo -n “Stopping $DESC: $NAME”

do_stop

echo “.”

;;

reload|graceful)

echo -n “Reloading $DESC configuration…”

do_reload

echo “.”

;;

restart)

echo -n “Restarting $DESC: $NAME”

do_stop

do_start

echo “.”

;;

*)

echo “Usage: $SCRIPTNAME {start|stop|reload|restart}”;

exit 3

;;

esac

exit 0

需要注意几个变量,分别是PATHNAMEDAEMONCONFIGFILEPIDFILESCRIPTNAME,其中,PATH设置为nginx的安装路径,DAEMON设置为nginx启动脚本的路径,CONFIGFILEnginx.conf文件的路径,PIDFILE是进程文件的路径,需要与nginx.conf文件中的配置一致,SCRIPTNAME则是要指向当前文件路径/etc/init.d/nginx

赋予可执行的权限

chmod +x /etc/init.d/nginx

开启命令

systemctl enable nginx

CentOS7 开启关闭 重载

/bin/systemctl start nginx

/bin/systemctl stop nginx

/bin/systemctl reload nginx

3、设置开机自动启动

chkconfig –add nginx

chkconfig nginx on

——————————————————————————

安装php

0、安装依赖的高版本组件

bison-3.0.4http://www.kwx.gd/PHPEnvironment/Linux-bison-install.html

re2chttp://www.kwx.gd/PHPEnvironment/Linux-re2c-later.html

libziphttp://www.kwx.gd/PHPEnvironment/CetnOS-libzip.html

并设置动态链(64位系统):http://www.kwx.gd/PHPEnvironment/CentOS-PHP-off-t.html

1、常用组件

yum -y install libmcrypt-devel mhash-devel libxslt-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel

2、下载php

wget http://cn.php.net/distributions/php-7.2.4.tar.gz

tar -zxvf php-7.2.4.tar.gz

3、安装可能依赖的组件和包

1)

re2chttp://sourceforge.net/projects/re2c/files/re2c/

tar xf re2c-*

cd re2c-*

./configure

#make && make install

2)bison

wget http://ftp.gnu.org/gnu/bison/bison-3.0.4.tar.gz

tar -zxvf bison-*

cd bison-*

./configure

make && make install

4、编译安装

cd /root/php-7.2.4

./configure –prefix=/usr/local/php –with-curl –with-freetype-dir –with-gd –with-gettext –with-iconv-dir –with-kerberos –with-libdir=lib64 –with-libxml-dir –with-mysqli –with-openssl –with-pcre-regex –with-pdo-mysql –with-pdo-sqlite –with-pear –with-png-dir –with-jpeg-dir –with-xmlrpc –with-xsl –with-zlib –with-bz2 –with-mhash –enable-fpm –enable-bcmath –enable-libxml –enable-inline-optimization –enable-gd-native-ttf –enable-mbregex –enable-mbstring –enable-opcache –enable-pcntl –enable-shmop –enable-soap –enable-sockets –enable-sysvsem –enable-sysvshm –enable-xml –enable-zip

make

make install

完毕

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20170718/

Installing PHP CLI binary:        /usr/local/php/bin/

Installing PHP CLI man page:      /usr/local/php/php/man/man1/

Installing PHP FPM binary:        /usr/local/php/sbin/

Installing PHP FPM defconfig:     /usr/local/php/etc/

Installing PHP FPM man page:      /usr/local/php/php/man/man8/

Installing PHP FPM status page:   /usr/local/php/php/php/fpm/

Installing phpdbg binary:         /usr/local/php/bin/

Installing phpdbg man page:       /usr/local/php/php/man/man1/

Installing PHP CGI binary:        /usr/local/php/bin/

Installing PHP CGI man page:      /usr/local/php/php/man/man1/

Installing build environment:     /usr/local/php/lib/php/build/

Installing header files:          /usr/local/php/include/php/

Installing helper programs:       /usr/local/php/bin/

  program: phpize

  program: php-config

Installing man pages:             /usr/local/php/php/man/man1/

  page: phpize.1

  page: php-config.1

Installing PEAR environment:      /usr/local/php/lib/php/

[PEAR] Archive_Tar    – installed: 1.4.3

[PEAR] Console_Getopt – installed: 1.4.1

[PEAR] Structures_Graph- installed: 1.1.1

[PEAR] XML_Util       – installed: 1.4.2

[PEAR] PEAR           – installed: 1.10.5

Wrote PEAR system config file at: /usr/local/php/etc/pear.conf

You may want to add: /usr/local/php/lib/php to your php.ini include_path

/root/php-7.2.4/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin

ln -s -f phar.phar /usr/local/php/bin/phar

Installing PDO headers:           /usr/local/php/include/php/ext/pdo/

4、设置php文件

cp /root/php-7.2.4/php.ini-production /usr/local/php/etc/php.ini

vi  /usr/local/php/etc/php.ini

复制一份,然后修改

5、设置php-fpm文件

复制文件

cd /usr/local/php/etc

cp php-fpm.conf.default php-fpm.conf

cd /usr/local/php/etc/php-fpm.d

cp www.conf.default www.conf

修改www.conf如下字段:

user = www

group = www

listen = 127.0.0.1:9000

pm.max_children = 100

pm.start_servers = 20

pm.min_spare_servers = 5

pm.max_spare_servers = 35

可根据实际需求修改

6、启动和查看进程

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

ps aux | grep php-fpm

7、加入变量

vi /etc/profile

最后一行加入

export PHP_HOME=/usr/local/php

export PATH=$PATH:$PHP_HOME/bin:$PHP_HOME/sbin

8、加入系统服务

vi /etc/init.d/php-fpm

内容

#!/bin/bash

# php-fpm startup script for the php-fpm

# php-fpm version:7.1.11

# chkconfig: – 85 15

# description: php-fpm

# processname: php-fpm

# pidfile: /var/local/php/var/run/php-fpm.pid

# config: /var/local/php/etc/php-fpm.conf

php_command=/var/local/php/sbin/php-fom

php_config=/var/local/php/etc/php-fpm.conf

php_pid=/var/local/php/var/run/php-fpm.pid

RETVAL=0

prog=”php-fpm”

#start function

php_fpm_start() {

    /var/local/php/sbin/php-fpm

}

start(){

    if [ -e $php_pid  ]

    then

    echo “php-fpm already start…”

    exit 1

    fi

    php_fpm_start

}

stop(){

    if [ -e $php_pid ]

    then

    parent_pid=`cat $php_pid`

    all_pid=`ps -ef | grep php-fpm | awk ‘{if(‘$parent_pid’ == $3){print $2}}’`

    for pid in $all_pid

    do

            kill $pid

        done

        kill $parent_pid

    fi

    exit 1

}

restart(){

    stop

    start

}

# See how we were called.

case “$1” in

start)

        start

        ;;

stop)

        stop

        ;;

restart)

        stop

        start

        ;;

status)

        status $prog

        RETVAL=$?

        ;;

*)

        echo $”Usage: $prog {start|stop|restart|status}”

        exit 1

esac

exit $RETVAL

8、开机启动

chkconfig –add php-fpm

chkconfig php-fpm on

——————————————————————————

七、Mysql安装

1CMAKE

wget https://cmake.org/files/v3.11/cmake-3.11.0.tar.gz

tar -zxvf cmake-3*

cd cmake*

./configure –prefix=/usr/local/cmake

make

make install

export PATH=/usr/local/cmake/bin:$PATH

2Ncurses

wget http://ftp.gnu.org/gnu/ncurses/ncurses-6.1.tar.gz

tar -zxvf ncurses*

cd ncurses*

./configure –prefix=/usr/local/ncurses

make

make install

3、安装libaio

yum install libaio*

安装libaiolibaio-devel这两个库

4、设置mysql

groupadd mysql

useradd -r -g mysql mysql

5、下载mysql 5.7并挪到统一目录

wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz

tar -zxvf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz

mv mysql-5.7.20-linux-glibc2.12-x86_64 /usr/local/mysql-5.7.20/

使用的是mysql官方提供的解压缩版的安装方式,不需要进行make操作。将mysql文件夹移动到安装路径后,需要修改mysql安装文件夹的权限,执行以下命令

chown -R mysql.mysql /usr/local/mysql-5.7.20/

6、设置mysql文件

mysql的配置文件是放在/etc/目录下的,名字是my.cnf 也可以不配置,默认也有内容

[client]

port     = 3306

socket      = /usr/local/mysql-5.7.20/tmp/mysql.sock

[mysqld]

basedir=/usr/local/mysql-5.7.20

datadir=/usr/local/mysql-5.7.20/data

socket=/usr/local/mysql-5.7.20/tmp/mysql.sock

#skip-external-locking

# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

# Settings user and group are ignored when systemd is used.

# If you need to run mysqld under a different user or group,

# customize your systemd unit file for mariadb according to the

# instructions in http://fedoraproject.org/wiki/Systemd

log-error=/usr/local/mysql-5.7.20/log/error.log

pid-file=/usr/local/mysql-5.7.20/logs/mysql.pid

[mysqld_safe]

log-error=/var/log/mariadb/mariadb.log

pid-file=/var/run/mariadb/mariadb.pid

#

# include all files from the config directory

#

!includedir /etc/my.cnf.d

#

# These groups are read by MariaDB command-line tools

# Use it for options that affect only one utility

#

[mysql]

[mysql_upgrade]

[mysqladmin]

[mysqlbinlog]

[mysqlcheck]

[mysqldump]

[mysqlimport]

[mysqlshow]

[mysqlslap]

其中basedir指的是mysql的安装目录,datadir指的是mysql数据文件的存储目录,其他几项配置项

所有在配置文件中出现的路径、文件均需要具有可读写的权限,前面为MySQL创建了用户及组,所以上述配置文件中涉及到的路径和文件,均需要将其所有者及组指定为mysql用户,这样MySQL服务才能正常启动。 

顺便禁止 innodb

default-storage-engine=MYISAM

skip-innodb

loose-innodb-trx=0

loose-innodb-locks=0

loose-innodb-lock-waits=0

loose-innodb-cmp=0

loose-innodb-cmp-per-index=0

loose-innodb-cmp-per-index-reset=0

loose-innodb-cmp-reset=0

loose-innodb-cmpmem=0

loose-innodb-cmpmem-reset=0

loose-innodb-buffer-page=0

loose-innodb-buffer-page-lru=0

loose-innodb-buffer-pool-stats=0

loose-innodb-metrics=0

loose-innodb-ft-default-stopword=0

loose-innodb-ft-inserted=0

loose-innodb-ft-deleted=0

loose-innodb-ft-being-deleted=0

loose-innodb-ft-config=0

loose-innodb-ft-index-cache=0

loose-innodb-ft-index-table=0

loose-innodb-sys-tables=0

loose-innodb-sys-tablestats=0

loose-innodb-sys-indexes=0

loose-innodb-sys-columns=0

loose-innodb-sys-fields=0

loose-innodb-sys-foreign=0

loose-innodb-sys-foreign-cols=0

7、放入启动脚本

cp -a /usr/local/mysql-5.7.20/support-files/mysql.server /etc/init.d/mysqld

并修改/etc/init.d/mysqld中的basedirdatadir,确保其值与my.cnf文件中的内容相同

8、初始化mysql

先创建data目录(数据库文件目录)

mkdir /usr/local/mysql-5.7.20/data/

chown mysql:mysql  /usr/local/mysql-5.7.20/data/

chmod 755 /usr/local/mysql-5.7.20/data/

/usr/local/mysql-5.7.20/bin/mysqld –initialize –user=mysql –basedir=/usr/local/mysql-5.7.20/ –datadir=/usr/local/mysql-5.7.20/data/

若前面配置文件中的各配置项均正确,各路径、文件均存在并可读写,改命令将提示执行成功,则表示ok

然后提示内会生成mysql密码“[Note] A temporary password is generated for root@localhost: R6Ed7mdn!9=V

9、安全模式启动mysql

/usr/local/mysql-5.7.20/bin/mysqld_safe –user=mysql ;

mysql安装目录的bin目录下,使用以下命令启动mysql

若提示  mysqld_safe error: log-error set to ‘/var/log/mariadb/mariadb.log’, however file don’t exists. Create writable for user ‘mysql’.

需要手动创建目录并赋予mysql账号和组权限

mkdir /var/log/mariadb

chown mysql:mysql  /var/log/mariadb

chmod 755 /var/log/mariadb

vi  /var/log/mariadb/mariadb.log  # 直接保存文件

启动后,使用初始化所得的root密码登录mysql,查看是否可以正常操作,命令如下

/usr/local/mysql-5.7.20/bin/mysql -uroot -pR6Ed7mdn!9=V –socket=/usr/local/mysql-5.7.20/mysql.sock

登录后 使用命令

set password for root@localhost=password(‘woshipasswd’);

修改密码

10、设置开机自动启动

cd /usr/local/mysql-5.7.20/bin/

chkconfig –level 35 mysqld on

先杀掉所有safe启动的进程

ps aux | grep mysql

kill pid

再启动

/etc/init.d/mysqld start

八、配置Nginx+PHP+MySQL

/usr/local/nginx/conf中创建配置文件

server {

        listen       80;

        server_name  localhost;

        index index.php index.htm index.html;

        root /alidata/webapps;

        location ~ .*\.(php|php5)?$

        {

                #fastcgi_pass  unix:/tmp/php-cgi.sock;

                fastcgi_pass  127.0.0.1:9000;

                fastcgi_index index.php;

                include fastcgi.conf;

        }

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

        {

                expires 30d;

        }

        location ~ .*\.(js|css)?$

        {

                expires 1h;

        }

        # 配置页面静态化

        include /alidata/server/nginx-1.12.2/conf/rewrite/default.conf;

        access_log  /alidata/logs/nginx/access/default.log;

        error_log /alidata/logs/nginx/error/default.log;

}

载入

systemctl reload nginx