栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

PHP CentOS 服务器 PHP、Nginx、Mysql、Redis、Composer ... 运行环境安装

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PHP CentOS 服务器 PHP、Nginx、Mysql、Redis、Composer ... 运行环境安装

软件列表
  • nginx
  • mysql
  • php
  • redis
  • composer
  • php-cs-fixer
  • git
  • nodejs
  • yarn
准备工作 更新系统
$ yum update
安装编译依赖
$ yum install -y gcc gcc-c++ autoconf pcre pcre-devel openssl openssl-devel zlib zlib-devel
创建 web && conf 目录
$ mkdir -p /data/www
$ mkdir -p /data/conf
创建 www 用户并设置密码
$ useradd www
$ passwd www
添加 www 用户 sudo 权限
$ visudo

在文件结尾添加以下内容并保存

www ALL=(ALL) ALL
安装 Nginx 下载并安装 nginx
$ cd /root/Downloads
// 下载
$ wget http://nginx.org/download/nginx-1.12.2.tar.gz

// 解压
$ tar -zxvf nginx-1.12.2.tar.gz

// 解压完删除源码包
$ rm -rf ./nginx-1.12.2.tar.gz

// 进入源码目录
$ cd ./nginx-1.12.2

// 编译
$ ./configure 
--prefix=/usr/local/nginx 
--user=www 
--group=www 
--with-http_ssl_module 
--with-http_v2_module 
--with-http_realip_module 
--with-http_addition_module 
--with-http_gzip_static_module 
--with-http_stub_status_module 
--with-pcre 
--with-stream

// 编译安装
$ make
$ make install
创建开机自启动脚本
$ vi /usr/lib/systemd/system/nginx.service

输入以下内容(注意路径)

[Unit]
Description=nginx - high performance web server
documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target
设置权限
$ chmod 0644 /usr/lib/systemd/system/nginx.service
添加自启动
$ systemctl enable nginx.service
启动 Nginx
$ systemctl start nginx.service

一些常用命令

systemctl is-enabled nginx.service #查询nginx是否开机启动
systemctl enable nginx.service #开机运行nginx
systemctl disable nginx.service #取消开机运行nginx
systemctl start nginx.service #启动nginx
systemctl stop nginx.service #停止nginx
systemctl restart nginx.service #重启nginx
systemctl reload nginx.service #重新加载nginx配置文件
systemctl status nginx.service #查询nginx运行状态
创建 nginc.conf 软连接
$ ln -s /usr/local/nginx/conf/nginx.conf /data/conf/
设置 Nginx 运行用户(需要重启)
// /data/conf/nginx.conf
user www
开放防火墙 80 端口
$ firewall-cmd --permanent --zone=public --add-port=80/tcp
$ systemctl restart firewalld

在浏览器访问服务器 IP 地址如果看到 "Welcome to nginx!" 说明安装成功!

安装 Mysql 下载 Mysql 社区版
$ cd /root/Downloads
$ wget http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
安装 Mysql Yum Repository

yum update 时会自动升级 Mysql 版本

$ yum install -y ./mysql57-community-release-el7-11.noarch.rpm
安装 Mysql 服务器版本
$ yum -y install mysql-community-server
启动 Mysql
$ systemctl start mysqld.service

一些常用命令

systemctl is-enabled mysqld.service #查询mysqld是否开机启动
systemctl enable mysqld.service #开机运行mysqld
systemctl disable mysqld.service #取消开机运行mysqld
systemctl start mysqld.service #启动mysqld
systemctl stop mysqld.service #停止mysqld
systemctl restart mysqld.service #重启mysqld
systemctl status mysqld.service #查询mysqld运行状态
设置默认字符集

在 /etc/my.cnf 中的 mysqld 段添加以下内容并重启:

[mysqld]
collation-server     = utf8mb4_general_ci
character-set-server = utf8mb4
开放防火墙 3306 端口
$ firewall-cmd --permanent --zone=public --add-port=3306/tcp
$ systemctl restart firewalld
查看初始密码
$ grep "password" /var/log/mysqld.log
进入 Mysql 并重置密码

Mysql 安全策略在默认情况下,禁止设置过于简单的密码,如果有需要,可以关闭安全策略,在 /etc/my.cnf 结尾添加 validate-password=OFF 并重启 mysql 即可生效。

// 进入 mysql
$ mysql -uroot -p

// 重置密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'NEW PASSWORD';
开启 Mysql 远程链接

注意远程链接授权密码

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'PASSWORD' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;
创建 my.cnf 软连接
$ ln -s /etc/my.cnf /data/conf/
安装 PHP 安装依赖
$ yum install -y libxml2-devel bzip2 bzip2-devel curl curl-devel gmp-devel readline-devel libxslt-devel libjpeg libpng freetype libjpeg-devel libpng-devel freetype-devel
下载并安装 libzip
$ cd /root/Downloads
$ wget https://nih.at/libzip/libzip-1.2.0.tar.gz
$ tar -zxvf ./libzip-1.2.0.tar.gz
$ rm -rf ./libzip-1.2.0.tar.gz
$ cd ./libzip-1.2.0
$ ./configure  --prefix=/usr/local/libzip
$ make -j
$ make install

// 复制 zip.h 和 zipconf.h 到 include
$ cp /usr/local/libzip/include/zip.h /usr/local/include/
$ cp /usr/local/libzip/lib/libzip/include/zipconf.h /usr/local/include/
指定 libzip 扩展文件目录
$ vi /etc/ld.so.conf.d/libzip.conf

添加内容为 /usr/local/libzip/lib,然后执行:

$ ldconfig -v
下载并安装 PHP
$ cd /root/Downloads
// 下载 PHP 源码包
$ wget http://cn2.php.net/distributions/php-7.2.3.tar.gz

// 解压
$ tar -zxvf ./php-7.2.3.tar.gz

// 删除 PHP 源码包
$ rm -rf ./php-7.2.3.tar.gz

// 进入 PHP 源码目录
$ cd php-7.2.3

// 编译
$ ./configure 
--prefix=/usr/local/php 
--with-config-file-path=/etc 
--enable-fpm 
--enable-inline-optimization 
--disable-debug 
--disable-rpath 
--enable-shared  
--with-libxml-dir 
--with-xmlrpc 
--with-mhash 
--with-pcre-regex 
--with-sqlite3 
--with-zlib 
--with-libzip=/usr/local/libzip 
--enable-bcmath 
--with-iconv 
--with-bz2 
--with-openssl 
--enable-calendar 
--with-curl 
--with-cdb 
--enable-dom 
--enable-exif 
--enable-fileinfo 
--enable-filter 
--with-pcre-dir 
--enable-ftp 
--with-gd 
--with-openssl-dir 
--with-jpeg-dir 
--with-png-dir 
--with-zlib-dir 
--with-freetype-dir 
--enable-gd-jis-conv 
--with-gettext 
--with-gmp 
--with-mhash 
--enable-json 
--enable-mbstring 
--enable-mbregex 
--enable-mbregex-backtrack 
--with-libmbfl 
--with-onig 
--enable-pdo 
--with-mysqli=mysqlnd 
--with-pdo-mysql=mysqlnd 
--with-pdo-sqlite 
--with-readline 
--enable-session 
--enable-shmop 
--enable-simplexml 
--enable-sockets  
--enable-sysvmsg 
--enable-sysvsem 
--enable-sysvshm 
--enable-wddx 
--with-libxml-dir 
--with-xsl 
--enable-zip 
--enable-mysqlnd-compression-support 
--with-pear 
--enable-opcache

// 编译安装
$ make
$ make install
添加 PHP 环境变量

在 /etc/profile 文件结尾添加:

export PATH=$PATH:/usr/local/php/bin
执行 source 使其生效
$ source /etc/profile
配置 PHP 配置文件
$ cp ./php.ini-development /etc/php.ini
$ cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
$ cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
创建配置文件软连接
$ ln -s /etc/php.ini /data/conf
$ ln -s /usr/local/php/etc/php-fpm.conf /data/conf/
$ ln -s /usr/local/php/etc/php-fpm.d/www.conf /data/conf/
创建 php-fpm 自启动脚本
$ cp ./sapi/fpm/php-fpm.service /usr/lib/systemd/system/
$ chmod 0644 /usr/lib/systemd/system/php-fpm.service
修改 php.ini 参数
// 设置时区
date.timezone = Asia/Shanghai
// 开启 opcache
opcache.enable=1
// 脚本最大执行时间
max_execution_time = 60
// 请求解析时间
max_input_time = 120
// 最大提交数据
post_max_size = 32M
// 单个文件最大限制
upload_max_filesize = 16M
// 单个进程最大内存分配数
memory_limit = 256M
设置 php-fpm 运行用户(需要重启)
// /data/conf/www.conf
user = www
group = www
添加 php-fpm 自启动
$ systemctl enable php-fpm.service
启动 php-fpm
$ systemctl start php-fpm.service

一些常用命令

systemctl is-enabled php-fpm.service #查php-fpm是否开机启动
systemctl enable php-fpm.service #开机运行php-fpm
systemctl disable php-fpm.service #取消开机运行php-fpm
systemctl start php-fpm.service #启动php-fpm
systemctl stop php-fpm.service #停止php-fpm
systemctl restart php-fpm.service #重启php-fpm
systemctl status php-fpm.service #查询php-fpm运行状态
配置 nginx.conf 并访问 php 文件

新建 /data/www/index.php 文件并添加内容:


修改 /data/conf/nginx.conf 并添加:

server {
    listen 80 default;

    root /data/www;
    index index.html index.php;

    location / {
 try_files $uri $uri/ /index.php;
    }

    location ~* .php$ {
 fastcgi_pass 127.0.0.1:9000;
 include fastcgi.conf;
    }
}

重启 nginx 后,浏览器里访问服务器 IP,如果看到 phpinfo 页面说明 php 安装完成。

安装 mcrypt 扩展(兼容老版本) 下载并安装 libmcrypt
$ cd /root/Downloads
$ wget http://soft.vpser.net/web/libmcrypt/libmcrypt-2.5.8.tar.gz
$ tar -zxvf ./libmcrypt-2.5.8.tar.gz
$ rm -rf ./libmcrypt-2.5.8.tar.gz
$ cd ./libmcrypt-2.5.8
$ ./configure
$ make
$ make install
下载并安装 mhash
$ cd /root/Downloads
$ wget http://soft.vpser.net/web/mhash/mhash-0.9.9.9.tar.gz
$ tar -zxvf ./mhash-0.9.9.9.tar.gz
$ rm -rf ./mhash-0.9.9.9.tar.gz
$ cd ./mhash-0.9.9.9
$ ./configure
$ make
$ make install
下载并安装 mcrypt
$ cd /root/Downloads
$ wget http://soft.vpser.net/web/mcrypt/mcrypt-2.6.8.tar.gz
$ tar -zxvf ./mcrypt-2.6.8.tar.gz
$ rm -rf ./mcrypt-2.6.8.tar.gz
$ cd ./mcrypt-2.6.8
$ LD_LIBRARY_PATH=/usr/local/lib ./configure
$ make
$ make install
下载并安装 mcrypt PHP 扩展
$ cd /root/Downloads
$ wget http://pecl.php.net/get/mcrypt-1.0.1.tgz
$ tar -zxvf ./mcrypt-1.0.1.tgz
$ rm -rf ./mcrypt-1.0.1.tgz
$ cd ./mcrypt-1.0.1
$ phpize
$ ./configure --with-php-config=/usr/local/php/bin/php-config
$ make
$ make install
在 php.ini 结尾添加:
extension=mcrypt.so
安装 Composer 下载 Composer 安装脚本
$ cd /root/Downloads/
$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
$ php -r "if (hash_file('SHA384', 'composer-setup.php') === '544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd586475ca9813a858088ffbc1f233e9b180f061') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
$ php ./composer-setup.php
$ php -r "unlink('composer-setup.php');"
移动到 bin 目录
$ mv ./composer.phar /usr/local/bin/composer
$ chmod a+x /usr/local/bin/composer
// 设置中国镜像
$ composer config -g repo.packagist composer https://packagist.phpcomposer.com
$ composer --version
安装 php-cs-fixer
$ cd /root/Downloads/
$ wget http://cs.sensiolabs.org/download/php-cs-fixer-v2.phar -O php-cs-fixer
$ mv ./php-cs-fixer /usr/local/bin/php-cs-fixer
$ chmod a+x /usr/local/bin/php-cs-fixer
$ php-cs-fixer --version
安装 Redis 服务
$ cd /root/Downloads/
$ wget http://download.redis.io/releases/redis-4.0.8.tar.gz
$ tar -zxvf ./redis-4.0.8.tar.gz 
$ rm -rf ./redis-4.0.8.tar.gz 
$ cd ./redis-4.0.8/
$ make

// 安装完成
$ cp ./src/redis-server ./src/redis-cli /usr/local/bin/
$ cp ./src/redis-sentinel ./src/redis-benchmark ./src/redis-check-aof /usr/local/bin/
$ cp redis.conf /etc/redis-6379.conf

修改 /etc/redis-6379.conf 参数:

// 设置为后台运行
daemonize yes

// 如果需要设置密码
// requirepass PASSWORD
创建存储目录
$ mkdir -p /var/lib/redis/6379

修改 /etc/redis-6379.conf 参数

dir /var/lib/redis/6379
开放防火墙 6379 端口
$ firewall-cmd --permanent --zone=public --add-port=6379/tcp
$ systemctl restart firewalld
设置自启动本
$ cp ./utils/redis_init_script /etc/init.d/redis_6379
$ vi /usr/lib/systemd/system/redis-6379.service

修改 /etc/init.d/redis_6379 启动脚本中的配置文件路径:

CONF="/etc/redis-${REDISPORT}.conf"

输出以下内容(注意路径):

[Unit]
Description=Redis on port 6379
[Service]
Type=forking
ExecStart=/etc/init.d/redis_6379 start
ExecStop=/etc/init.d/redis_6379 stop
[Install]
WantedBy=multi-user.target
设置自启动
$ systemctl enable redis-6379.service
启动 redis 服务
$ systemctl start redis-6379.service
安装 redis PHP 客户端
$ cd /root/Downloads/
$ wget http://pecl.php.net/get/redis-3.1.6.tgz
$ tar -zxvf redis-3.1.6.tgz
$ rm -rf ./redis-3.1.6.tgz
$ cd ./redis-3.1.6
$ phpize
$ ./configure --with-php-config=/usr/local/php/bin/php-config
$ make
$ make install
在 php.ini 结尾添加:
extension=redis.so
重启 php-fpm
$ systemctl reload php-fpm.service 
创建 redis-6379.conf 软连接
$ ln -s /etc/redis-6379.conf /data/conf/
设置权限 设置 /data/www 目录所有者为 www
$ chown -R www /data/www/
安装 Git 安装依赖
yum install -y expat-devel perl-ExtUtils-MakeMaker
下载并安装
$ cd /root/Downloads
$ wget https://github.com/git/git/archive/v2.16.2.tar.gz
$ tar -zxvf ./v2.16.2.tar.gz
$ rm -rf ./v2.16.2.tar.gz
$ cd git-2.16.2
$ make prefix=/usr/local/git all
$ make prefix=/usr/local/git install
添加 Git 环境变量

在 /etc/profile 文件结尾添加:

export PATH=$PATH:/usr/local/git/bin
执行 source 使其生效
$ source /etc/profile
$ git --version
安装 Nodejs 下载并安装
$ cd /root/Downloads
$ wget https://nodejs.org/dist/v8.10.0/node-v8.10.0-linux-x64.tar.xz
$ xz -d ./node-v8.10.0-linux-x64.tar.xz
$ tar -xvf ./node-v8.10.0-linux-x64.tar -C /usr/local/
$ mv /usr/local/node-v8.10.0-linux-x64/ /usr/local/node
添加 Nodejs 环境变量

在 /etc/profile 文件结尾添加:

export PATH=$PATH:/usr/local/node/bin
执行 source 使其生效
$ source /etc/profile
$ node -v
$ npm -v
安装 yarn

下载并安装

// 下载 yarn repository
$ curl --silent --location https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo
// yum 安装 yarn
$ yum install -y yarn
$ yarn --version

如果出现 You could try using --skip-broken xxx 错误,执行 yum install -y epel-release 后再运行 yum install yarn

自动清理 /tmp 目录
$ yum install tmpwatch
// 清理 3 小时内没被使用过的文件
$ /usr/sbin/tmpwatch -afv 3 /tmp

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号