centos7 搭建 php7.1.3+mysql5.7+nginx1.11

勇康博客网
预计阅读时长 106 分钟
位置: 首页 服务器 正文

一、nginx1.11的安装:

1. 安装

centos 7 下安装 nginx-1.11前需要先切换到root环境,通过命令su root 切换,然后再输入密码, 如果不能切换需要把下载的nginx文件夹给予777的权限

su root

下载nginx-1.11.10的压缩包文件到根目录

yum update #更新系统软件
cd /
wget nginx.org/download/nginx-1.11.10.tar.gz

解压tar.gz压缩包文件,进去nginx-1.11.10

tar -xzvf nginx-1.11.10.tar.gz 
cd nginx-1.11.10

进去后如果发现有configure这个文件,说明这个源码包安装前需要先进行配置,主要是为了检查当前的环境是否满足要安装软件的依赖关系,如果没有这个文件说明是二进制包,解压后直接使用不用configure

./configure

通过安装前的配置检查,发现有报错 检查中发现一些依赖库没有找到,这时候需要先安装nginx的一些依赖库

yum -y install pcre* #安装使nginx支持rewrite
yum -y install gcc-c++        
yum -y install zlib*
yum -y install openssl openssl-devel

再次进行检查操作 ./configure 没发现报错显示,接下来进行编译并安装的操作

#检查模块支持
  ./configure  --prefix=/usr/local/nginx  --with-http_ssl_module --with-http_v2_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_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-mail --with-mail_ssl_module --with-stream --with-stream_ssl_module --with-stream_realip_module --with-stream_ssl_preread_module --with-threads --user=www --group=www

编译并安装

make && make install

编译安装可能出现的问题

#错误1
src/core/ngx_murmurhash.c: In function ‘ngx_murmur_hash2’:
src/core/ngx_murmurhash.c:37:11: error: this statement may fall through [-Werror=implicit-fallthrough=]
         h ^= data[2] << 16;
         ~~^~~~~~~~~~~~~~~~
src/core/ngx_murmurhash.c:38:5: note: here
     case 2:
     ^~~~
src/core/ngx_murmurhash.c:39:11: error: this statement may fall through [-Werror=implicit-fallthrough=]
         h ^= data[1] << 8;
         ~~^~~~~~~~~~~~~~~
src/core/ngx_murmurhash.c:40:5: note: here
     case 1:
     ^~~~
cc1: all warnings being treated as errors
make[1]: *** [objs/Makefile:432: objs/src/core/ngx_murmurhash.o] Error 1
#解决:
1、使用编辑器打开nginx/objs/Makefile,删除CFLAGS中的-Werror
CC =    cc
CFLAGS =  -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g
CPP =   cc -E
LINK =  $(CC)
2、重新进行make操作即可

#错误2
src/os/unix/ngx_user.c: In function ‘ngx_libc_crypt’:
src/os/unix/ngx_user.c:35:7: error: ‘struct crypt_data’ has no member named ‘current_salt’
     cd.current_salt[0] = ~salt[0];
       ^
make[1]: *** [objs/Makefile:712: objs/src/os/unix/ngx_user.o] Error 1
#解决:
1、vi /nginx-1.11.10/src/os/unix/ngx_user.c
2、在如下位置将 cd.current_salt[0] = ~salt[0] 注释
#ifdef __GLIBC__
    /* work around the glibc bug */
     /* cd.current_salt[0] = ~salt[0]; */
#endif
3、重新编译

查看nginx安装后在的目录,可以看到已经安装到 /usr/local/nginx 目录了

whereis nginx
nginx: /usr/local/nginx

启动nginx服务

cd /usr/local/nginx/sbin/
./nginx

服务启动的时候报错了:nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) ,通过命令查看本机网络地址和端口等一些信息,找到被占用的80端口(netstat -ntpl)的tcp连接,并杀死进程(kill 进程pid)

netstat -ntpl
kill 进程PID

继续启动nginx服务,启动成功

./nginx

nginx安装好后在/usr/local/nginx/conf下不会自动创建vhosts文件夹,为了方便管理可以手动创建vhosts文件夹

mkdir /usr/local/nginx/conf/vhosts

修改配置文件,引入vhosts文件夹

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

加上 include vhosts/*.conf;

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the php scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # https server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    include vhosts/*.conf;

}

2. 设置nginx开机自启动

#创建开机启动命令脚本文件:
vi /etc/init.d/nginx

在这个nginx文件中插入一下启动脚本代码,启动脚本代码来源网络复制,实测有效

#! /bin/bash
# chkconfig: - 85 15
PATH=/usr/local/nginx
DESC="nginx daemon"
NAME=nginx
DAEMON=$PATH/sbin/$NAME
CONFIGFILE=$PATH/conf/$NAME.conf
PIDFILE=$PATH/logs/$NAME.pid
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}" >&2
exit 3
;;
esac
exit 0

设置所有人都有对这个启动脚本nginx文件的执行权限

chmod a+x /etc/init.d/nginx

把nginx加入系统服务中

chkconfig --add nginx

把服务设置为开机启动

chkconfig nginx on

reboot重启系统生效

#启动nginx服务
systemctl start nginx.service
#停止nginx服务
systemctl stop nginx.service
#重启nginx服务
 
#同时也可以通过下面的命令进行服务重启,停止操作
service nginx restart
service nginx start
service nginx stop

注意:启动可能报如下错误(window下复制启动脚本换行符导致)
env: ‘/etc/init.d/nginx’: No such file or directory
#解决:运行下面指令,在重新启动,下面安装php如遇类似问题也一样解决
dos2unix /etc/init.d/nginx

如果服务启动的时候出现 Restarting nginx daemon: nginxnginx: [error] open() “/usr/local/nginx/logs/nginx.pid” failed (2: No such file or directory)  nginx not running 的错误,通过nginx -c 参数指定配置文件即可解决

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

如果服务启动中出现 nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) 的错误,可以先通过service nginx stop 停止服务,再启动就好。


二、安装php7.1.10

1.安装

目前官网已经出到 PHP8.0 ,这里以安装相对稳定的php7为例

#在官网下载php-7.1.3的php压缩包
wget -c http://cn2.php.net/distributions/php-7.1.3.tar.gz

下载好后解压 php-7.1.3.tar.gz 压缩包,并进入解压后的目录

tar -xzvf php-7.1.3.tar.gz
cd php-7.1.3

安装php7需要的一些依赖库包 libxml2和一些其他依赖的扩展库

yum -y install libxml2 
yum -y install libxml2-devel 
yum -y install openssl 
yum -y install openssl-devel 
yum -y install curl-devel 
yum -y install libjpeg-devel 
yum -y install libpng-devel 
yum -y install freetype-devel
yum -y install bzip2-devel
yum -y install libmcrypt libmcrypt-devel
yum -y install postgresql-devel
yum -y install aspell-devel
yum -y install readline-devel
yum -y install libxslt-devel
yum -y install net-snmp-devel
yum -y install unixODBC-devel
yum -y install libicu-devel
yum -y install libc-client-devel
yum -y install libXpm-devel
yum -y install libvpx-devel
yum -y install enchant-devel
yum -y install openldap
yum -y install openldap-devel
yum -y install db4-devel
yum -y install gmp-devel
yum -y install sqlite-devel
yum -y install mysql-devel

添加用户和组

groupadd -r www && useradd -r -g www -s /sbin/nologin

安装前的环境配置检查,php7的一些依赖包的检查和php扩展的启动,这个过程如果缺少php依赖的库包会有报错提示。

./configure      --prefix=/usr/local/php    --with-config-file-path=/usr/local/php/etc    --enable-fpm    --with-fpm-user=www    --with-fpm-group=www    --enable-inline-optimization    --disable-debug    --disable-rpath    --enable-shared    --enable-soap    --with-xmlrpc    --with-openssl    --with-mcrypt    --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-freetype-dir    --enable-gd-native-ttf    --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

对php7进行编译和安装的操作

make && make install
#以下是编译安装常见的一些错误及处理方法,笔者安装php7.1正常,安装php7.3有报错,主要和zip有关

#错误一: ERROR: [pool www] cannot get uid for user 'www' 
#解决:(缺少www用户和用户组)
groupadd www
useradd -g www www

#错误一: configure: error: mcrypt.h not found. Please reinstall libmcrypt.
#解决:
yum install -y epel-release
yum install -y libmcrypt-devel

#错误二: configure: error: Please reinstall the libzip distribution
#解决:
wget https://nih.at/libzip/libzip-1.2.0.tar.gz
tar -zxvf libzip-1.2.0.tar.gz
cd libzip-1.2.0
./configure
make && make install

#错误三:configure: error: off_t undefined; check your library configuration
#解决:
# 添加搜索路径到配置文件
echo '/usr/local/lib64
/usr/local/lib
/usr/lib
/usr/lib64'>>/etc/ld.so.conf
# 更新配置
ldconfig -v

#错误四:/usr/local/include/zip.h:59:21: fatal error: zipconf.h: No such file or dire
#解决:
cp /usr/local/lib/libzip/include/zipconf.h /usr/local/include/zipconf.h

#错误五:make: *** [sapi/cli/php] Error 1
#解决:
make ZEND_EXTRA_LIBS='-liconv'

编译成功,查看php版本

/usr/local/php/bin/php -v

运行php-fpm

/usr/local/php/sbin/php-fpm
启动php-fpm可能会遇到的错误,基本都是配置文件配置和重命名的问题:

错误一:缺少www.conf配置文件(WARNING: Nothing  matches the include pattern /usr/local/php/etc/php-fpm.d/*.conf),需要把/usr/local/php/etc/php-fpm.d/*.conf目录中的www.conf.default重名名为www.conf。

错误二:缺少php-fpm.conf配置文件 需要在/usr/local/php/etc/php-fpm.conf 把默认的配置文件php-fpm.conf.default改成php-fpm.conf

错误三:缺少php.ini配置文件 php7编译安装好后会发现没有php.ini配置文件,需要在解压包的根目录中复制php.ini-production或php.ini-development并重命名为php.ini到php编译好的安装目录中的/usr/local/php/etc文件夹即可。

2.设置php-fpm 开机启动

开启配置php-fpm pid

#找到php-fpm.conf配置文件
/usr/local/php/etc/php-fpm.conf
#开启pid ,去掉分号注释
pid = run/php-fpm.pid

创建开机启动脚本

vim /etc/init.d/php-fpm #编辑 i
#! /bin/sh
#chkconfig:   2345 15 95
# Comments to support chkconfig on CentOS
 
set -e
 
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="php-fpm daemon"
NAME=php-fpm
DAEMON=/usr/local/php/sbin/$NAME
 
CONFIGFILE=/usr/local/php/etc/php-fpm.conf
PIDFILE=/usr/local/php/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
 
# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0
 
d_start() {
  $DAEMON -y $CONFIGFILE || echo -n " already running"
}
 
d_stop() {
  kill -QUIT `cat $PIDFILE` || echo -n " not running"
}
 
d_reload() {
  kill -HUP `cat $PIDFILE` || echo -n " can't reload"
}
 
case "$1" in
  start)
        echo -n "Starting $DESC is success"
        d_start
        echo "."
        ;;
  stop)
        echo -n "Stopping $DESC is success"
        d_stop
        echo "."
        ;;
  reload)
        echo -n "Reloading $DESC configuration..."
        d_reload
        echo "reloaded."
  ;;
  restart)
        echo -n "Restarting $DESC is success"
        d_stop
        sleep 1
        d_start
        echo "."
        ;;
  *)
         echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
         exit 3
        ;;
esac

对开机启动脚本添加所有人都执行的权限

chmod a+x /etc/init.d/php-fpm

php-fpm加入开机启动服务

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

查看开机启动的所有服务

chkconfig --list

服务操作

#启动服务
service php-fpm start 
#停止服务
service php-fpm stop  
#重启服务
service php-fpm reload

3.安装MongoDB扩展

#第一步
/usr/local/php/bin/pecl install mongodb

#第二步 写入配置文件
echo "extension=mongodb.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`

4.安装redis扩展

#第一步
/usr/local/php/bin/pecl install redis

#第二步 写入php配置文件
echo "extension=redis.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`

三、安装mysql

1.下载MySQL 安装包:

[root@localhost local]# wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm

安装mysql安装源:

[root@localhost local]#  yum -y localinstall mysql57-community-release-el7-11.noarch.rpm

2.在线安装MySQL

[root@localhost local]# yum -y install mysql-community-server

3.启动mysql 服务

[root@localhost local]# systemctl start mysqld

4.设置开机启动

[root@localhost local]# systemctl enable mysqld
[root@localhost local]# systemctl daemon-reload

5.修改root登录密码

mysql安装完成之后,会在/var/log/mysqld.log文件中给root生成了一个临时的默认密码。

[root@localhost local]# cat /var/log/mysqld.log

在这里插入图片描述

修改root 密码:

[root@localhost local]# mysql -u root -p
mysql>  ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)
# 设置远程登录
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)

注意:MySQL5.6.6版本之后增加了密码强度验证插件validate_password,相关参数设置的较为严格。使用了该插件会检查设置的密码是否符合当前设置的强度规则,若不满足则拒绝设置,对应报错如下:

mysql>  ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

解决:

mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.05 sec)

mysql> set global validate_password_mixed_case_count=0;
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_number_count=3;
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_special_char_count=0;
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_length=3;
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+-------+
| Variable_name                        | Value |
+--------------------------------------+-------+
| validate_password_dictionary_file    |       |
| validate_password_length             | 3     |
| validate_password_mixed_case_count   | 0     |
| validate_password_number_count       | 3     |
| validate_password_policy             | LOW   |
| validate_password_special_char_count | 0     |
+--------------------------------------+-------+

参数解释:

validate_password_dictionary_file
--插件用于验证密码强度的字典文件路径。
validate_password_length
--密码最小长度,参数默认为8,它有最小值的限制,最小值为:validate_password_number_count + validate_password_special_char_count + (2 * validate_password_mixed_case_count)
validate_password_mixed_case_count
--密码至少要包含的小写字母个数和大写字母个数。
validate_password_number_count
--密码至少要包含的数字个数。
validate_password_policy
--密码强度检查等级,0/LOW、1/MEDIUM、2/STRONG。有以下取值:
Policy                 Tests Performed
0 or LOW               Length
1 or MEDIUM         Length; numeric, lowercase/uppercase, and special characters
2 or STRONG        Length; numeric, lowercase/uppercase, and special characters; dictionary file
--默认是1,即MEDIUM,所以刚开始设置的密码必须符合长度,且必须含有数字,小写或大写字母,特殊字符。
validate_password_special_char_count
--密码至少要包含的特殊字符数。

6.配置mysql默认编码为utf-8

[root@localhost sysconfig]# vim /etc/my.cnf

在最后添加如下代码:

character_set_server=utf8
init_connect='SET NAMES utf8'

7.重启MySQL

[root@localhost data]# systemctl restart mysqld


本文来自投稿,不代表本站立场,如若转载,请注明出处:
-- 展开阅读全文 --
头像
Composer的安装和使用详解
« 上一篇 2021-07-29
PHP单例模式
下一篇 » 2021-08-05
取消
微信二维码
微信二维码
支付宝二维码

发表评论

暂无评论,2406人围观

作者信息

勇康博客网
承接企业、个人,仿站、定制。域名,主机一键代发
TA的最新作品

热门文章

2
3

动态快讯

标签列表

目录[+]