新购入一台阿里云服务器,系统版本是CentOS 7.4,以下是搭建PHP环境的步骤
一、部署yum源
1、打开centos的yum文件夹
输入命令cd /etc/yum.repos.d/
2、用wget下载repo文件
输入命令wget http://mirrors.aliyun.com/repo/Centos-7.repo
如果wget命令不生效,说明还没有安装wget工具,输入yum -y install wget
回车进行安装。
当前目录是/etc/yum.repos.d/,刚刚下载的Centos-7.repo也在这个目录上
3、备份系统原来的repo文件
mv CentOS-Base.repo CentOS-Base.repo.bak
即是重命名 CentOS-Base.repo -> CentOS-Base.repo.bak
4、替换系统原理的repo文件
mv Centos-7.repo CentOS-Base.repo
即是重命名 Centos-7.repo -> CentOs-Base.repo
5、执行yum源更新命令
yum clean all
yum makecache
yum update
但是阿里云的源有时候缺很多东西
可以继续安装其他源
安装epel源
yum install epel-release
安装remi源
yum install -y http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
然后进入到/etc/yum.repos.d/目录看一下源列表
yum repolist
二、yum源配好了下面就可以安装软件了
1、先随便安装个vim测试一下
yum install vim
下面开始安装PHP环境
2、安装PHP
通过search搜索合适的PHP源
yum search php7
这时候会列出一堆带php7的源,如果想更详细可以选php72或者php73等版本,如果发现没有从remi源里搜索
可以加上选择什么源
yum --enablerepo=remi search <keyword>
可以通过yum info <包名>
查看包的具体版本等信息
依次执行安装php
yum install php73
yum install php73-php-fpm
3、安装nginx,发现源里面的nginx版本比较旧,通过以下方式安装新的
wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum install nginx
4、接下来安装mysql
首先centOS 7 自带一个mariadb,需要卸载掉,否则会跟mysql冲突
rpm -qa | grep mariadb //列出来服务器上安装的mariadb包
rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64 //移除掉安装的包(包名从刚才的列表里粘贴)
依然是和nginx类似的步骤获取比较新的mysql 5.7版本
wget http://repo.mysql.com//mysql57-community-release-el7-7.noarch.rpm
rpm -Uvh mysql57-community-release-el7-7.noarch.rpm
yum install mysql-community-server //安装服务端
mysql5.7之后的版本为了安全起见会随机生成一个初始密码放在/var/log/mysqld.log?
找到日志中的
> A temporary password is generated for root@localhost:
之类就是初始密码。
修改mysql密码
拿到初始密码之后,登录mysql修改密码
mysql -u root -p
- 1: 修改密码,必须有大小写,含数字特殊符号,长度8位及以上
> SET PASSWORD = 'your password';
- 2: 设置密码不过期;
> ALTER USER root@localhost PASSWORD EXPIRE NEVER;
- 3: 刷新权限表;
> flush privileges;
然后打开nginx配置目录
新建pksfc.conf
server {
listen 80;
server_name www.pksfc.com;
root /var/www/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
index index.html;
if (!-e $request_filename) {
rewrite ^(.*)$ /404.html last;
break;
}
error_page 404 /var/www/html/404.html;
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
#fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
fastcgi_keep_conn on;
fastcgi_connect_timeout 180;
fastcgi_read_timeout 600;
fastcgi_send_timeout 600;
fastcgi_buffer_size 128k;
fastcgi_buffers 8 64k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
}
}
server {
listen 443 ssl http2;
server_name www.pksfc.com;
root /var/www/html;
ssl on;
ssl_certificate /etc/nginx/1_www.pksfc.com_bundle.crt;
ssl_certificate_key /etc/nginx/2_www.pksfc.com.key;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_dhparam /etc/nginx/ssl/dhparam.pem;
add_header Strict-Transport-Security "max-age=31536000; includeSubdomains; preload";
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;
location / {
index index.html;
if (!-e $request_filename) {
rewrite ^(.*)$ /404.html last;
break;
}
error_page 404 /var/www/html/404.html;
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
#fastcgi_pass unix:/var/run/php-fpm.sock;
#fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
#include fastcgi_params;
fastcgi_index index.php;
fastcgi_connect_timeout 180;
fastcgi_read_timeout 600;
fastcgi_send_timeout 600;
fastcgi_buffer_size 128k;
fastcgi_buffers 8 64k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
#fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
fastcgi_keep_conn on;
}
}
然后restart nginx之后就可以通过域名去访问了
如果想设置开机自启动的话就执行下面的语句
chkconfig nginx on
chkconfig php72-php-fpm on
chkconfig supervisord on
5、最后一步补充PHP扩展,当你发现缺少了某些扩展的时候依然是通过以下命令
yum search php-mysql
yum install -y php73-php-mysqlnd.x86_64
service php73-php-fpm restart
去搜索安装对应扩展并重启php-fpm服务
其他的如redis,mongodb,等参考上面例子安装即可
大功告成!!!