安装php+nginx环境

作者: 高多金 | 来源:发表于2020-01-13 13:29 被阅读0次

安装LNMP环境

yum -y install sqlite-devel libxml2-devel libxml2
yum -y install openssl-devel zlib-devel pcre-devel gcc gcc-c++ make cmake

获取软件包

建议从官网直接下载

php-7.4.1 : https://www.php.net/downloads

nginx-1.4.7 : nginx.org/en/download.html

也可以直接链接下载

wget -c https://www.php.net/distributions/php-7.4.1.tar.gz #容易失败
wget -c http://nginx.org/download/nginx-1.4.7.tar.gz

先部署Nginx

tar zxf nginx-1.4.7.tar.gz
cd nginx-1.4.7
./configure
make && make install
/usr/local/nginx/sbin/nginx

查看一下是否开启了

root@iZ2zed1cy0175oixwqtkf4Z nginx-1.4.7]# ss -tnl
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN      0      128             *:80                          *:*                  
LISTEN      0      128             *:22                          *:*                  
[root@iZ2zed1cy0175oixwqtkf4Z nginx-1.4.7]# ps -ef |grep nginx
root     11813     1  0 18:07 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody   11814 11813  0 18:07 ?        00:00:00 nginx: worker process
root     11829  9430  0 18:08 pts/0    00:00:00 grep --color=auto nginx

之后开始部署PHP

tar zxf php-7.4.1.tar.gz
cd php-7.4.1
./configure --enable-fpm --with-mysql  #没有MySQL先不管
make && make install

创建配置文件,并将其复制到正确的位置

cp php.ini-development /usr/local/php/php.ini
cp /usr/local/etc/php-fpm.d/www.conf.default /usr/local/etc/php-fpm.d/www.conf
cp sapi/fpm/php-fpm /usr/local/bin

需要着重提醒的是,如果文件不存在,则阻止 Nginx 将请求发送到后端的 PHP-FPM 模块, 以避免遭受恶意脚本注入的攻击。

将 php.ini 文件中的配置项 cgi.fix_pathinfo 设置为 0 。

vim /usr/local/php/php.ini

修改文件

cgi.fix_pathinfo=0

在启动服务之前,需要修改 php-fpm.conf 配置文件,确保 php-fpm 模块使用 www-data 用户和 www-data 用户组的身份运行

vim /usr/local/etc/php-fpm.d/www.conf

修改文件为

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
user = www-data
group = www-data

复制php-fpm配置文件

cp /usr/local/etc/php-fpm.conf.default /usr/local/etc/php-fpm.conf

修改php-fpm配置文件

找到这一行

include=NONE/etc/php-fpm.d/*.conf

修改为

include=etc/php-fpm.d/*.conf     #那个etc前面没有/号

然后创建用户和用户组

groupadd www-data
useradd -m -g www-data www-data

启动php-fpm

/usr/local/bin/php-fpm

查看一下

[root@iZ2zed1cy0175oixwqtkf4Z etc]# ss -tnl
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN      0      128     127.0.0.1:9000                        *:*                  
LISTEN      0      128             *:80                          *:*                  
LISTEN      0      128             *:22                          *:*                  

然后开始配置nginx配置文件

vim /usr/local/nginx/conf/nginx.conf

修改如下

在这一块index后加入index.php

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

之后在配置文件中复制这一段

location ~* \.php$ {
    fastcgi_index   index.php;
    fastcgi_pass    127.0.0.1:9000;
    include         fastcgi_params;
    fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
}

重启 Nginx

/usr/local/nginx/sbin/nginx -s stop
/usr/local/nginx/sbin/nginx

创建测试文件

rm /usr/local/nginx/html/index.html
y
echo "<?php phpinfo(); ?>" >> /usr/local/nginx/html/index.php

测试一下

关闭防火墙

systemctl stop firewalld
setenforce 0

安装mysql5.7 centos7

从官网下载rpm包

wget -c https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm

使用rpm包

yum localinstall mysql80-community-release-el7-3.noarch.rpm

通过以下命令检查是否已成功添加MySQL Yum存储库

yum repolist enabled | grep "mysql.*-community.*"
[root@iZ2zed1cy0175oixwqtkf4Z ~]# yum repolist enabled | grep "mysql.*-community.*"
mysql-connectors-community/x86_64 MySQL Connectors Community                 131
mysql-tools-community/x86_64      MySQL Tools Community                      100
mysql80-community/x86_64          MySQL 8.0 Community Server                 145

选择发行系列

yum repolist all | grep mysql
[root@iZ2zed1cy0175oixwqtkf4Z ~]# yum repolist all | grep mysql
mysql-cluster-7.5-community/x86_64 MySQL Cluster 7.5 Community   disabled
mysql-cluster-7.5-community-source MySQL Cluster 7.5 Community - disabled
mysql-cluster-7.6-community/x86_64 MySQL Cluster 7.6 Community   disabled
mysql-cluster-7.6-community-source MySQL Cluster 7.6 Community - disabled
mysql-cluster-8.0-community/x86_64 MySQL Cluster 8.0 Community   disabled
mysql-cluster-8.0-community-source MySQL Cluster 8.0 Community - disabled
mysql-connectors-community/x86_64  MySQL Connectors Community    enabled:    131
mysql-connectors-community-source  MySQL Connectors Community -  disabled
mysql-tools-community/x86_64       MySQL Tools Community         enabled:    100
mysql-tools-community-source       MySQL Tools Community - Sourc disabled
mysql-tools-preview/x86_64         MySQL Tools Preview           disabled
mysql-tools-preview-source         MySQL Tools Preview - Source  disabled
mysql55-community/x86_64           MySQL 5.5 Community Server    disabled
mysql55-community-source           MySQL 5.5 Community Server -  disabled
mysql56-community/x86_64           MySQL 5.6 Community Server    disabled
mysql56-community-source           MySQL 5.6 Community Server -  disabled
mysql57-community/x86_64           MySQL 5.7 Community Server    disabled
mysql57-community-source           MySQL 5.7 Community Server -  disabled
mysql80-community/x86_64           MySQL 8.0 Community Server    enabled:    145
mysql80-community-source           MySQL 8.0 Community Server -  disabled

要安装最新GA系列的最新版本,无需进行配置。要安装除最新GA系列以外的特定系列的最新版本,请在运行安装命令之前禁用最新GA系列的子存储库并启用该特定系列的子存储库。

因为我们要安装mysql5.7所以手动修改

vim /etc/yum.repos.d/mysql-community.repo

配置文件如下

[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

其中 ”enabled=1“ 的意思是开启 如果我们想安装5.7就把 80的 ”enabled=1“ 改成 ”enabled=0“ 然后把5.7的改成1

[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

我们查看一下

yum repolist enabled | grep mysql
[root@iZ2zed1cy0175oixwqtkf4Z ~]# yum repolist enabled | grep mysql
mysql-connectors-community/x86_64 MySQL Connectors Community                 131
mysql-tools-community/x86_64      MySQL Tools Community                      100
mysql57-community/x86_64          MySQL 5.7 Community Server                 384
[root@iZ2zed1cy0175oixwqtkf4Z ~]# 

安装mysql

yum -y install mysql-community-server

启动mysql

systemctl start mysqld
[root@iZ2zed1cy0175oixwqtkf4Z ~]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2020-01-09 10:40:49 CST; 15s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 5850 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 5801 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 5854 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─5854 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid...

Jan 09 10:40:43 iZ2zed1cy0175oixwqtkf4Z systemd[1]: Starting MySQL Server...
Jan 09 10:40:49 iZ2zed1cy0175oixwqtkf4Z systemd[1]: Started MySQL Server.

假设服务器的数据目录为空,则在服务器首次启动时,会发生以下情况:

  • 服务器已初始化。
  • SSL证书和密钥文件在数据目录中生成。
  • validate_password 已安装并启用。
  • 'root'@'localhost创建 一个超级用户帐户。设置超级用户的密码并将其存储在错误日志文件中。要显示它,请使用以下命令:
grep 'temporary password' /var/log/mysqld.log
[root@iZ2zed1cy0175oixwqtkf4Z ~]# grep 'temporary password' /var/log/mysqld.log
2020-01-09T02:40:45.729722Z 1 [Note] A temporary password is generated for root@localhost: D_i(;BCaf4(F

通过使用生成的临时密码登录并尽快为超级用户帐户设置自定义密码,以更改root密码

复制最后的字符串 “D_i(;BCaf4(F”

mysql -uroot -pD_i(;BCaf4(F    #如果密码带有括号 需要转义 可以加上双引号

登录后首先修改root密码

ALTER USER 'root'@'localhost' IDENTIFIED BY 'password';

mysql安装完毕

相关文章

网友评论

    本文标题:安装php+nginx环境

    本文链接:https://www.haomeiwen.com/subject/elwgactx.html