目录
6、修改php-fpm.conf配置文件,使用www用户和www用户组运行
第一步:安装Mysql5.7
说明:安装MySQL位置在 /usr/local 下
1、下载MySQL 安装包
[root@localhost local]# wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
2、安装Mysql 安装源
[root@localhost local]# yum -y localinstall mysql57-community-release-el7-11.noarch.rpm
3、在线安装Mysql
[root@localhost local]# yum -y install mysql-community-server
4、启动Mysql
[root@localhost local]# systemctl start mysqld
5、设置开机启动
[root@localhost local]# systemctl enable mysqld
[root@localhost local]# systemctl daemon-reload
6、修改root登录密码
Mysql安装完成之后,会在/var/log/mysqld.log文件中给root生成了一个临时的默认密码
[root@localhost local]# vim /var/log/mysqld.log
记住初始密码,输入初始密码登录Mysql,修改root密码:
[root@localhost local]# mysql -u root -p
修改密码:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '你的新密码';
Query OK, 0 rows affected (0.00 sec)
7、设置远程登录
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '你的心密码' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)
8、退出Msql
mysql> exit
9、防火墙开启3306 端口
[root@localhost sysconfig]# cd /etc/sysconfig/
[root@localhost sysconfig]# vim iptables
#添加代码如下
-A INPUT -p tcp --dport 3306 -j ACCEPT
:wq 保存退出;也可以登录服务器开启3306端口;
10、重启防火墙
[root@localhost sysconfig]# service iptables restart
11、 配置Mysql默认编码为utf-8
[root@localhost sysconfig]# vim /etc/my.cnf
添加如下代码:
character_set_server=utf8
init_connect='SET NAMES utf8'
:wq 保存退出;
12、重启Mysql
[root@localhost data]# systemctl restart mysqld
至此Mysql5.7安装完成,可以远程链接测试;
第二步:安装Nginx1.21.4
说明:安装Nginx位置在 /usr/local 下
1、安装GCC
nginx是C语言开发的,所以需要安装一个可以供C语言运行的环境gcc-c++
yum install gcc-c++
看到Complete则安装成功
2、安装PCRE
Nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库,nginx也需要此库
yum install -y pcre pcre-devel
看到Complete则安装成功
3、安装zlib
zlib是提供数据压缩用的函式库,nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库
yum install -y zlib zlib-devel
看到Complete则安装成功
4、安装openssl
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http)
yum install -y openssl openssl-devel
看到Complete则安装成功
5、安装Nginx
下载Nginx,我把Nginx下载到/usr/local/tools/nginx/,可根据自己下载位置下载
[root@VM-4-11-centos nginx]# wget http://nginx.org/download/nginx-1.21.4.tar.gz
解压Nginx
[root@VM-4-11-centos nginx]# tar -zxvf nginx-1.21.4.tar.gz
进入解压后的Nginx文件夹
[root@VM-4-11-centos nginx]# cd nginx-1.21.4/
./configure进行配置设置
[root@VM-4-11-centos nginx]# ./configure
如果需要配置SSL证书 ,则使用
[root@VM-4-11-centos nginx]# ./configure --with-http_ssl_module
编译安装
[root@VM-4-11-centos nginx]# make && make install
6、设置开机启动
编辑/etc/rc.local文件
[root@VM-4-11-centos nginx]# vim /etc/rc.local
添加/usr/local/nginx/sbin/nginx
:wq 保存退出
修改权限
chmod 755 /etc/rc.local
7、配置域名
服务器申请SSL证书,并下载,把证书上传到服务器,我这里把证书放到:/usr/local/tools/ssl/下
user root;
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;
sendfile on;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
# 你的域名
server_name www.xxx.com xxx.com;
rewrite ^(.*)$ https://$host$1 permanent;
}
server {
listen 443 ssl;
# 你的域名
server_name www.xxx.com xxx.com;
# 域名证书
ssl_certificate /usr/local/tools/ssl/xxx.pem;
ssl_certificate_key /usr/local/tools/ssl/xxx.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
client_max_body_size 500m;
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;
#}
}
}
8、检查nginx配置文件,并重启Nginx
#进入nginx sbin文件夹
[root@VM-4-11-centos nginx]# cd /usr/local/nginx/sbin
输入./nginx -t检查配置文件
[root@VM-4-11-centos sbin]# ./nginx -t
#出现successful表示配置文件正确
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
@重启Nginx
[root@VM-4-11-centos sbin]# ./nginx -s reload
9、浏览器输入域名访问
第三步:安装PHP5.6
说明:安装PHP位置在 /usr/local 下
1、安装编译环境
[root@VM-4-11-centos ~]# yum -y install epel-release
[root@VM-4-11-centos ~]# yum -y install gcc automake autoconf libtool make gcc-c++ glibc 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 libmcrypt mcrypt mhash php-mcrypt
2、下载PHP
下载到/usr/local/tools/php
[root@VM-4-11-centos ~]# cd /usr/local/tools/php
#下载php
[root@VM-4-11-centos php]# wget http://cn2.php.net/distributions/php-5.6.24.tar.gz
#解压
[root@VM-4-11-centos php]# tar -axvf php-5.6.24.tar.gz
#进入php文件夹
[root@VM-4-11-centos php]# cd php-5.6.24
3、编译php
#配置,需要支持PDO,gd,pdo_mysql,开启--enable-pdo,添加扩展--with-gd --with-pdo_mysql
[root@VM-4-11-centos php]# ./configure --prefix=/usr/local/php --enable-fpm --with-mcrypt --enable-mbstring --enable-pdo --with-curl --disable-debug --disable-rpath --enable-inline-optimization --with-bz2 --with-zlib --enable-sockets --enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex --with-mhash --enable-zip --with-pcre-regex --with-mysql --with-mysqli --with-gd --with-pdo_mysql
#编译
[root@VM-4-11-centos php]# make && make install
4、查看php版本及扩展
如果php -v
命令无法查看PHP版本号
#打开环境变量配置文件
vim /etc/profile
#将php安装目录添加进环境变量
export PATH=$PATH:/usr/local/php/bin
#刷新配置文件使之生效
source /etc/profile
#查看php版本
[root@VM-4-11-centos php]# php -v
PHP 5.6.24 (cli) (built: Feb 6 2023 16:57:48)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
#查看php编译后的扩展
[root@VM-4-11-centos php]# php -m
[PHP Modules]
bz2
Core
ctype
curl
date
dom
ereg
fileinfo
filter
gd
hash
iconv
json
libxml
mbstring
mcrypt
mhash
mysql
mysqli
mysqlnd
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
Reflection
session
SimpleXML
sockets
SPL
sqlite3
standard
sysvsem
sysvshm
tokenizer
xml
xmlreader
xmlwriter
zip
zlib
[Zend Modules]
5、复制配置文件
[root@VM-4-11-centos php]# cp php.ini-development /usr/local/php/php.ini
[root@VM-4-11-centos php]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@VM-4-11-centos php]# cp sapi/fpm/php-fpm /usr/local/bin
6、修改php-fpm.conf配置文件,使用www用户和www用户组运行
添加用户组: groupadd [groupname]
添加用户:useradd [username]
将用户添加进工作组:usermod -G [groupname] [username]
查看所有用户:cat /etc/passwd
vim /usr/local/php/etc/php-fpm.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
group = www
:wq 保存退出
7、部署项目到服务器
#php项目上传到/usr/local/projects/php/
cd /usr/local/projects
#修改php文件夹权限 赋予www:www
chown -R www:www /usr/local/projects/php
#查看文件夹权限
[root@VM-4-11-centos projects]# ll
drwxr-xr-x 12 www www 4096 Feb 6 17:08 php
8、修改Nginx配置文件
#编辑Nginx配置文件
vim /usr/local/nginx/conf/nginx.conf
添加php
user root;
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;
sendfile on;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
#你的域名
server_name www.xxx.com xxx.com;
rewrite ^(.*)$ https://$host$1 permanent;
}
server {
listen 443 ssl;
#你的域名
server_name www.xxx.com xxx.com;
#域名证书
ssl_certificate /usr/local/tools/ssl/xxx.pem;
ssl_certificate_key /usr/local/tools/ssl/xxx.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
client_max_body_size 500m;
location / {
#php项目存放路径
root /usr/local/projects/php;
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/projects/php;
}
location ~ \.php$ {
#php项目存放路径
root /usr/local/projects/php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi.conf;
}
}
}
:wq 保存退出,重启nginx
#进入nginx sbin文件夹
[root@VM-4-11-centos nginx]# cd /usr/local/nginx/sbin
输入./nginx -t检查配置文件
[root@VM-4-11-centos sbin]# ./nginx -t
#出现successful表示配置文件正确
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
@重启Nginx
[root@VM-4-11-centos sbin]# ./nginx -s reload
9、启动php-fpm
#运行php-fpm
/usr/local/bin/php-fpm
#查看运行情况
netstat -anop | grep php
#查看运行端口
ps -aux | grep php-fpm
www 7990 0.0 0.1 221528 14488 ? S Feb21 1:38 php-fpm: pool www
root 12429 0.0 0.0 213100 4740 ? Ss Feb06 2:07 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
www 14386 0.0 0.1 218240 11212 ? S Mar01 1:19 php-fpm: pool www
root 25977 0.0 0.0 112816 980 pts/0 S+ 11:15 0:00 grep --color=auto php-fpm
www 31950 0.0 0.1 219012 11960 ? S Apr01 0:07 php-fpm: pool www
#关闭运行
kill -9 端口号