centos7從零開始搭建php7.2+nginx+mysql5.7.21

安裝前
首先進(jìn)行升級,要不然可能安裝的是老的版本
yum -y update

查看是否已安裝編譯器

    rpm -qa gcc

如果沒有,則進(jìn)行安裝

    yum install gcc gcc-c++

安裝上傳下載命令

    yum install lrzsz

1、安裝nginx
安裝nginx依賴包
nginx的Rewrite模塊和HTTP核心模塊會使用到PCRE正則表達(dá)式語法:

    yum -y install pcre pcre-devel

nginx的各種模塊中需要使用gzip壓縮:

    yum -y install zlib zlib-devel

安全套接字層密碼庫:

    yum -y install openssl openssl-devel

下載nginx文件
鏈接:https://pan.baidu.com/s/1DLZh8Kl9n5TofAeER7ok4A
提取碼:hi5e
下載到電腦上,用rz命令進(jìn)行

在這里插入圖片描述

    tar -zxvf nginx-1.12.2.tar.gz

編譯安裝(到/usr/local/nginx目錄中)

    cd nginx-1.12.2
    ./configure --prefix=/usr/local/nginx
    make
    make install

創(chuàng)建并設(shè)置nginx運行賬號:

   groupadd nginx
   useradd -M -g nginx -s /sbin/nologin nginx
   cd /usr/local/nginx/conf

設(shè)置user參數(shù)

    vi nginx.conf
在這里插入圖片描述

修改user 為nginx


在這里插入圖片描述

設(shè)置nginx為系統(tǒng)服務(wù)

    vi /lib/systemd/system/nginx.service
    [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

啟動nginx

    service nginx start

或者

    systemctl start nginx.service

設(shè)置nginx開機自啟動

   systemctl enable nginx.service

檢查開機自動是否設(shè)置成功

   systemctl list-dependencies | grep nginx
nginx

創(chuàng)建conf.d目錄


在這里插入圖片描述

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


在這里插入圖片描述

創(chuàng)建目錄,并賦予權(quán)限
    mkdir -p /www/logs/nginx
    chmod -R 777 /www/

在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;
       #}
   }

如果此時不能進(jìn)行訪問,防火墻需要關(guān)閉,或者開啟80端口
查看防火墻狀態(tài)

   systemctl status firewalld.service
防火墻

1)、關(guān)閉防火墻
systemctl stop firewalld.service
2)、開啟80端口

    # --permanent永久生效,沒有此參數(shù)重啟后失效
    firewall-cmd --zone=public --add-port=80/tcp --permanent    

查看哪些端口開啟

 firewall-cmd --zone=public --list-ports

然后就可以訪問了。
至此,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

下載php包并解壓
鏈接:https://pan.baidu.com/s/1C-JgaAFTqIJCCpJNoCI4yw
提取碼:kcwj

    tar -zxvf php-7.2.13.tar.gz
    cd php-7.2.13

創(chuàng)建php目錄

     mkdir -p /usr/local/php

編譯安裝

    ./configure \
    --prefix=/usr/local/php \
    --disable-fileinfo \
    --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-gd-native-ttf \
    --enable-mbstring \
    --with-mcrypt=/usr/local/libmcrypt \
    --enable-zip \
    --enable-mysqlnd \
    --with-mysqli=mysqlnd \
    --with-pdo-mysql=mysqlnd \
    --with-mysql-sock=/var/lib/mysql/mysql.sock \
    --without-pear  \
    --enable-bcmath

(注意:--with-mcrypt參數(shù)指定的是libmcrypt的安裝目錄。Php7不再使用mysql的庫來支持mysql的連接,而是啟用了mysqlnd來支持,所以php7的編譯已經(jīng)不再使用--with-mysql參數(shù)指定mysql的安裝位置了,若想支持mysql,需要設(shè)置--enable-mysqlnd、--with-mysqli和--with-pdo-mysql=mysqlnd參數(shù),--with-mysql-sock指定的是編譯mysql時-DMYSQL_UNIX_ADDR參數(shù)指定的文件)

    make && make install

大兄dei,這個可能需要半小時左右,可以先干點別的事情了!

將php包解壓目錄中的配置文件放置到正確位置(configure命令中的--with-config-file-path設(shè)置的位置)

  cp php.ini-development /etc/php.ini

創(chuàng)建并設(shè)置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

發(fā)現(xiàn)搜索不到“user”(設(shè)置運行賬號的位置),但發(fā)現(xiàn)文件的最后一行:


在這里插入圖片描述

所以:

   cd php-fpm.d
   #否則include匹配不到文件
   cp www.conf.default www.conf
   vi www.conf

搜索“user”設(shè)置運行賬號:


在這里插入圖片描述

將nobody更改為www-data

    user=www-data
    group=www-data

進(jìn)行編輯

    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;
       #}
   }

然后進(jìn)行查看配置是否正確

    nginx -t

如果正確,則進(jìn)行nginx重啟

    service nginx restart

設(shè)置php-fpm為系統(tǒng)服務(wù):

    vi /etc/systemd/system/php-fpm.service

文件內(nèi)容:

    [Unit]
    Description=php-fpm
    After=network.target
    [Service]
    Type=forking
    ExecStart=/usr/local/php/sbin/php-fpm
    PrivateTmp=True
    [Install]
    WantedBy=multi-user.target

設(shè)置php-fpm服務(wù)開機自啟動:

    systemctl enable php-fpm.service

檢查開機自啟動是否設(shè)置成功

    systemctl list-dependencies | grep php-fpm
在這里插入圖片描述

啟動php-fpm:

    service php-fpm start

檢查是否啟動成功

    service php-fpm status

查看是否啟動成功:

    ps aux | grep php-fpm
在這里插入圖片描述
    cp /usr/local/php/bin/php /usr/bin/

查看對應(yīng)的擴展

    php -m

在網(wǎng)站根目錄下編寫一個index.php

 <?php
    echo phpinfo();

至此php安裝完畢。

3、MySQL·
由于yum 安裝mysql國內(nèi)太慢,所以用源碼安裝。
鏈接:https://pan.baidu.com/s/1OGOdu--7GZZQOp4f409eJA
提取碼:z36q
下載完成之后.
輸入 rz命令。如果沒有則需要安裝

    yum install lrzsz
上傳

我這是先拷貝到home目錄


在這里插入圖片描述

解壓

    tar -xvf mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz

移動

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

重命名

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

新建data目錄

    mkdir -p /data/mysql

新建mysql用戶、mysql用戶組

    groupadd mysql
    useradd mysql -g mysql

將/usr/local/mysql的所有者及所屬組改為mysql

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

初始化

    cd /usr/local/mysql
在這里插入圖片描述

初始化

    /usr/local/mysql/bin/mysqld --initialize --basedir=/usr/local/mysql --datadir=/data/mysql --explicit_defaults_for_timestamp
在這里插入圖片描述

軟連接

    ln -s /usr/local/mysql/bin/mysql /usr/bin/

給數(shù)據(jù)庫加密

    /usr/local/mysql/bin/mysql_ssl_rsa_setup --datadir=/data/mysql
在這里插入圖片描述

編輯配置文件

    vi /etc/my.cnf
[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
#不區(qū)分大小寫
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
    cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
    vi /etc/init.d/mysql +46
在這里插入圖片描述

啟動mysql

    /usr/local/mysql/bin/mysqld_safe -user=mysql &
在這里插入圖片描述

啟動服務(wù)


在這里插入圖片描述

發(fā)現(xiàn)不能啟動,再查看錯誤日志。
日志文件在剛才/etc/my.cnf里面

    vi /var/log/mysqld.log
在這里插入圖片描述

顯然是文件不存在

    mkdir -p /var/run/mysqld

然后再執(zhí)行


在這里插入圖片描述

這里明顯就是權(quán)限不夠

    chown -R mysql /var/run/mysqld
    chgrp -R mysql /var/run/mysqld

此時執(zhí)行
仍然不行,查看錯誤日志顯示


在這里插入圖片描述

顯然是/data/mysql沒有權(quán)限可寫。

    chown -R mysql:mysql /data/mysql

此時再執(zhí)行,就顯示成功了


在這里插入圖片描述

看到這,基本就是大功告成。
但是此時好像已經(jīng)忘了root密碼了,別慌~

    vi /etc/my.cnf

在mysqld中加入skip-grant-tables
然后重啟mysql,這時就可以不用密碼登陸了。

    mysql -uroot -p

直接回車


在這里插入圖片描述

切換到mysql


在這里插入圖片描述

更改root密碼
    update user set authentication_string=password('123456') where user='root';
在這里插入圖片描述

允許遠(yuǎn)程連接

    update user set host = '%' where user = 'root';
在這里插入圖片描述
    Flush Privileges;

完畢之后,記得刪除/etc/my.cnf里面skip-grant-tables參數(shù)
如果用navicat不能進(jìn)行連接,需要開啟3306端口。

    firewall-cmd --zone=public --add-port=3306/tcp --permanent
    firewall-cmd --reload

如果有下面的錯誤。


在這里插入圖片描述

則只需要

    mysql -uroot -p123456
    alter user user() identified by "123456";

mysql開啟自啟動

    cd /usr/local/mysql/support-files
    cp mysql.server /etc/init.d/mysqld
    chkconfig --add mysqld
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 本文是介紹使用源碼編譯安裝,包括具體的編譯參數(shù)信息。 正式開始前,編譯環(huán)境gcc g++ 開發(fā)庫之類的需要提前裝好...
    oYoY閱讀 3,076評論 0 1
  • 文章目錄 1.安裝 Nginx 1.1 安裝依賴 1.2 下載,解壓,編譯 1.3 設(shè)置軟鏈并啟動 1.4 配置 ...
    慶慶_ce88閱讀 1,305評論 0 1
  • 一、安裝依賴包。 [root@study~]# yum -y install gcc gcc++ gcc-c++ ...
    簡言簡語_wade閱讀 1,140評論 0 1
  • [toc] 在公司的網(wǎng)站上推薦使用 docker 容器來安裝環(huán)境,一個項目一個 docker 容器。 、、 百度百...
    Mdvtrw閱讀 1,564評論 0 1
  • 系統(tǒng)環(huán)境 所需軟件官方下載地址: 一、 安裝開發(fā)包環(huán)境: 二、 關(guān)閉iptables和Selinux(生產(chǎn)...
    莫名其妙的一生閱讀 1,391評論 0 4

友情鏈接更多精彩內(nèi)容