centos7从零开始搭建php7.2+nginx+mysql5.7.21


安装前
首先进行升级,要不然可能安装的是老的版本
yum -y update

查看是否已安装编译器

    rpm -qa gcc
1
如果没有,则进行安装

    yum install gcc gcc-c++
1
安装上传下载命令

    yum install lrzsz
1
1、安装nginx
安装nginx依赖包
nginx的Rewrite模块和HTTP核心模块会使用到PCRE正则表达式语法:

    yum -y install pcre pcre-devel
1
nginx的各种模块中需要使用gzip压缩:

    yum -y install zlib zlib-devel
1
安全套接字层密码库:

    yum -y install openssl openssl-devel
1
下载nginx文件
链接:https://pan.baidu.com/s/1DLZh8Kl9n5TofAeER7ok4A
提取码:hi5e
下载到电脑上,用rz命令进行


    tar -zxvf nginx-1.12.2.tar.gz
1
编译安装(到/usr/local/nginx目录中)

    cd nginx-1.12.2
    ./configure --prefix=/usr/local/nginx
    make
    make install
1
2
3
4
创建并设置nginx运行账号:

    groupadd nginx
    useradd -M -g nginx -s /sbin/nologin nginx
    cd /usr/local/nginx/conf
1
2
3
设置user参数

    vi nginx.conf
1

修改user 为nginx

设置nginx为系统服务

    vi /lib/systemd/system/nginx.service
1
    [Unit]
    Description=nginx
    After=network.target
    [Service]
    Type=forking
    ExecStart=/usr/local/nginx/sbin/nginx
    ExecReload=/usr/local/nginx/sbin/nginx -s reload
    ExecStop=/usr/local/nginx/sbin/nginx -s stop
    PrivateTmp=true
    [Install]
    WantedBy=multi-user.target
1
2
3
4
5
6
7
8
9
10
11
启动nginx

    service nginx start
1
或者

    systemctl start nginx.service
1
设置nginx开机自启动

    systemctl enable nginx.service
1
检查开机自动是否设置成功

    systemctl list-dependencies | grep nginx
1

创建conf.d目录

修改nginx.conf配置
log_format前面#去掉
增加include conf.d/*.conf;

创建目录,并赋予权限

    mkdir -p /www/logs/nginx
    chmod -R 777 /www/
1
2
在conf.d中添加配置文件test.conf

    server {
       listen       80;
       server_name  test.bj.local;
  
       #charset koi8-r;
       access_log  /www/logs/nginx/$host.access.log  main;
  
       location / {
           root /usr/local/nginx/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   /usr/share/nginx/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$ {
        #   fastcgi_pass   127.0.0.1:9000;
         #  fastcgi_index  index.php;
          # fastcgi_param  SCRIPT_FILENAME  $document_root$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;
       #}
   }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
如果此时不能进行访问,防火墙需要关闭,或者开启80端口
查看防火墙状态

    systemctl status firewalld.service
1

1)、关闭防火墙
systemctl stop firewalld.service
2)、开启80端口

     # --permanent永久生效,没有此参数重启后失效
     firewall-cmd --zone=public --add-port=80/tcp --permanent    
1
2
查看哪些端口开启

 firewall-cmd --zone=public --list-ports
1
然后就可以访问了。
至此,nginx就算安装成功!

2、PHP
安装php依赖包

    yum 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
1
下载php包并解压
链接:https://pan.baidu.com/s/1C-JgaAFTqIJCCpJNoCI4yw
提取码:kcwj

    tar -zxvf php-7.2.13.tar.gz
    cd php-7.2.13
1
2
创建php目录

      mkdir -p /usr/local/php
1
编译安装
一定要先make clean,否则通过phpinfo()发现Loaded Configuration File => (none) 不存在,这样就安装扩展不生效

    ./configure \
    --prefix=/usr/local/php \
    --enable-fpm \
    --enable-inline-optimization \
    --enable-shared  \
    --enable-soap \
    --enable-opcache \
    --enable-session \
    --enable-shmop \
    --enable-simplexml \
    --enable-sockets  \
    --enable-sysvmsg \
    --enable-sysvsem \
    --enable-sysvshm \
    --enable-wddx \
    --enable-json \
    --enable-mbregex \
    --enable-mbregex-backtrack \
    --with-libmbfl \
    --disable-debug \
    --disable-rpath \
    --with-config-file-path=/etc \
    --with-config-file-scan-dir=/etc/php.d \
    --with-openssl \
    --with-mhash \
    --with-zlib \
    --with-curl \
    --enable-ftp \
    --with-gd \
    --with-xmlrpc \
    --with-jpeg-dir \
    --with-png-dir \
    --with-libxml-dir \
    --with-xsl \
    --with-freetype-dir \
    --enable-mbstring \
    --enable-zip \
    --enable-mysqlnd \
    --with-mysqli=mysqlnd \
    --with-pdo-mysql=mysqlnd \
    --with-mysql-sock=/var/lib/mysql/mysql.sock \
    --without-pear  \
    --enable-bcmath
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
(注意:–with-mcrypt参数指定的是libmcrypt的安装目录。Php7不再使用mysql的库来支持mysql的连接,而是启用了mysqlnd来支持,所以php7的编译已经不再使用–with-mysql参数指定mysql的安装位置了,若想支持mysql,需要设置–enable-mysqlnd、–with-mysqli和–with-pdo-mysql=mysqlnd参数,–with-mysql-sock指定的是编译mysql时-DMYSQL_UNIX_ADDR参数指定的文件)

      make && make install
1
大兄dei,这个可能需要半小时左右,可以先干点别的事情了!

如果出现

在安裝 PHP 到系统中时要是发生「undefined reference to libiconv_open’」之类的错误信息,那表示在「./configure 」沒抓好一些环境变数值。错误发生点在建立「-o sapi/cli/php」是出错,没給到要 link 的 iconv 函式库参数。 解决方法:

    make ZEND_EXTRA_LIBS='-liconv'
1
完毕之后

    make test
1
    ln -s /usr/local/lib/libiconv.so.2 /usr/lib64/
1
然后再执行:

     make && make install
1
将php包解压目录中的配置文件放置到正确位置(configure命令中的–with-config-file-path设置的位置)

    cp php.ini-development /etc/php.ini
1
创建并设置php-fpm运行账号

    groupadd www-data
    useradd -M -g www-data -s /sbin/nologin www-data
    cd /usr/local/php/etc
    cp php-fpm.conf.default php-fpm.conf
    vi php-fpm.conf
1
2
3
4
5
发现搜索不到“user”(设置运行账号的位置),但发现文件的最后一行:

所以:

    cd php-fpm.d
    #否则include匹配不到文件
    cp www.conf.default www.conf
    vi www.conf
1
2
3
4
搜索“user”设置运行账号:

将nobody更改为www-data

    user=www-data
    group=www-data
1
2
进行编辑

    server {
       listen       80;
       server_name  test.local;
   
       #charset koi8-r;
       access_log  /www/logs/nginx/$host.access.log  main;
   
       root /usr/local/nginx/html;
       index index.php index.html index.htm;
       location / {
           try_files $uri $uri/ /index.php?$args;
       }
   
       #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   /usr/share/nginx/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$ {
           fastcgi_pass   127.0.0.1:9000;
           fastcgi_index  index.php;
           fastcgi_param  SCRIPT_FILENAME  $document_root$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;
       #}
   }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
然后进行查看配置是否正确

    nginx -t
1
如果正确,则进行nginx重启

    service nginx restart
1
设置php-fpm为系统服务:

    vi /etc/systemd/system/php-fpm.service
1
文件内容:

    [Unit]
    Description=php-fpm
    After=network.target
    [Service]
    Type=forking
    ExecStart=/usr/local/php/sbin/php-fpm
    PrivateTmp=True
    [Install]
    WantedBy=multi-user.target
1
2
3
4
5
6
7
8
9
设置php-fpm服务开机自启动:

    systemctl enable php-fpm.service
1
检查开机自启动是否设置成功

    systemctl list-dependencies | grep php-fpm
1

启动php-fpm:

    service php-fpm start
1
检查是否启动成功

    service php-fpm status
1
查看是否启动成功:

    ps aux | grep php-fpm
1


    cp /usr/local/php/bin/php /usr/bin/
1
查看对应的扩展

    php -m
1
在网站根目录下编写一个index.php

 <?php
    echo phpinfo();
1
2
至此php安装完毕。

3、MySQL·
由于yum 安装mysql国内太慢,所以用源码安装。
链接:https://pan.baidu.com/s/1do6EfXrk2yxH4WhNd8elOA
提取码:ebd7
下载完成之后.
输入 rz命令。如果没有则需要安装

    yum install lrzsz
1

我这是先拷贝到home目录

解压

    tar -xvf mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz
1
移动

    mv mysql-5.7.21-linux-glibc2.12-x86_64 /usr/local/
1
重命名

    mv /usr/local/mysql-5.7.21-linux-glibc2.12-x86_64 /usr/local/mysql
1
新建data目录

    mkdir -p /data/mysql
1
新建mysql用户、mysql用户组

    groupadd mysql
    useradd mysql -g mysql
1
2
将/usr/local/mysql的所有者及所属组改为mysql

    chown -R mysql.mysql /usr/local/mysql
1
初始化

    cd /usr/local/mysql
1

初始化

    /usr/local/mysql/bin/mysqld --initialize --basedir=/usr/local/mysql --datadir=/data/mysql --explicit_defaults_for_timestamp
1

软连接

    ln -s /usr/local/mysql/bin/mysql /usr/bin/
1
给数据库加密

    /usr/local/mysql/bin/mysql_ssl_rsa_setup --datadir=/data/mysql
1

编辑配置文件

    vi /etc/my.cnf
1
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'
basedir=/usr/local/mysql
datadir=/data/mysql
socket=/tmp/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
#不区分大小写
lower_case_table_names = 1
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
max_connections=5000
 
default-time_zone = '+8:00'

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
    cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
1
    vi /etc/init.d/mysql +46
1


启动mysql

    /usr/local/mysql/bin/mysqld_safe -user=mysql &
1

启动服务

发现不能启动,再查看错误日志。
日志文件在刚才/etc/my.cnf里面

    vi /var/log/mysqld.log
1

显然是文件不存在

    mkdir -p /var/run/mysqld
1
然后再执行

这里明显就是权限不够

    chown -R mysql /var/run/mysqld
    chgrp -R mysql /var/run/mysqld
1
2
此时执行
仍然不行,查看错误日志显示

显然是/data/mysql没有权限可写。

    chown -R mysql:mysql /data/mysql
1
此时再执行,就显示成功了

看到这,基本就是大功告成。
但是此时好像已经忘了root密码了,别慌~

    vi /etc/my.cnf
1
在mysqld中加入skip-grant-tables
然后重启mysql,这时就可以不用密码登陆了。

    mysql -uroot -p
1
直接回车

切换到mysql

更改root密码

    update user set authentication_string=password('123456') where user='root';
1

允许远程连接

    update user set host = '%' where user = 'root';
1


    Flush Privileges;
1
完毕之后,记得删除/etc/my.cnf里面skip-grant-tables参数
如果用navicat不能进行连接,需要开启3306端口。

    firewall-cmd --zone=public --add-port=3306/tcp --permanent
    firewall-cmd --reload
1
2
如果有下面的错误。

则只需要

    mysql -uroot -p123456
    alter user user() identified by "123456";
1
2
mysql开启自启动

    cd /usr/local/mysql/support-files
    cp mysql.server /etc/init.d/mysqld
    chkconfig --add mysqld
————————————————
版权声明:本文为CSDN博主「菜鸟一直在路上」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/lizarel/article/details/90269745

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值