目录javascript
做为一名PHP开发者,咱们必定要懂得如何搭建PHP开发环境,目前主流的PHP开发环境组合是LAMP和LNMP,本文将介绍如何在CentOS7.*上搭建LNMP开发环境。php
各项版本说明:css
CentOS7: 7.7html
Nginx: 1.16.1java
MySQL:5.7.28node
PHP:7.4.0mysql
wget
是一个从网络上自动下载文件的自由工具,支持经过 HTTP、HTTPS、FTP 三个最多见的TCP/IP协议下载,并能够可使用HTTP代理。linux
sudo yum install wget
最小化安装CentOS7时若是没法使用ifconfig命令,则须要安装net-tools
,若是是安装的CentOS6版本则无需安装nginx
sudo yum -y install net-tools
sudo yum -y install vim
vim ~/.vimrc # 编辑.vimrc配置文件 set nu # 输入set nu 后退出保存
systemctl stop firewalld.service #令关闭防火墙 systemctl disable firewalld.service #关闭防火墙开机自启动 经过浏览器输入IP测试是否成功
(1) 安装 nginx
须要先将官网下载的源码进行编译,编译依赖 gcc 环境,若是没有 gcc 环境,则须要安装gcc-c++。c++
yum -y install gcc gcc-c++
(2) PCRE
是一个Perl库,中文"Perl兼容的正则表达式库"。安装Nginx是为了使Nginx支持具有URI重写功能的rewrite模块,若是不安装pcre库,则Nginx没法使用rewrite模块功能,Nginx的Rewrite模块功能几乎是企业应用必须。
yum -y install pcre pcre-devel
(3) zlib
库提供了不少种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,因此须要在 Centos 上安装 zlib 库。
yum -y install zlib zlib-devel
(4) OpenSSL
是一个强大的安全套接字层密码库,囊括主要的密码算法、经常使用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。 nginx 不只支持 http 协议,还支持 https(即在ssl协议上传输http),因此须要安装 OpenSSL 库 。
yum -y install openssl openssl-devel
说明: yum安装方式安装的pcre版本比较低,不过基本不影响使用,可是最好仍是手动编译安装官网最新稳定版的openssl。
检查基础依赖包
上面的依赖安装完成后能够经过以下命令检查各个依赖安装是否成功
rpm -qa pcre pcre-devel rpm -qa zlib zlib-devel rpm -qa pcre pcre-devel
# 这里咱们把安装包都放到了/usr/local/src目录下,便于统一管理 cd /usr/local/src #切换到软件包目录 wget http://nginx.org/download/nginx-1.16.1.tar.gz #下载nginx源码包 useradd nginx -s /sbin/nologin -M #建立nginx用户用于管理nginx程序 tar -zxvf nginx-1.16.1.tar.gz #解压nginx源码包 cd nginx-1.16.1 #注意,这里因为自定义了openssl位置,make时会报错,须要修改一个nginx源码里的一个conf文件 #/usr/local/src/nginx-1.16.1/auto/lib/openssl/conf,修改方法见印象笔记 #预编译 ./configure \ --user=nginx \ --group=nginx \ --prefix=/usr/local/nginx-1.16.1 \ --with-http_v2_module \ --with-http_ssl_module \ --with-http_stub_status_module make && make install #编译 和 安装 cd /usr/local ln -s nginx-1.16.1 nginx #建立nginx的软连接
安装说明
--prefix=PATH #设置安装路劲 --user=USER #进程用户权限 --group=GROUP #进程用户组权限 --with-http_v2_module # HTTP2 --with-http_stub_status_module #激活状态信息 --with-http_ssl_module #激活ssl功能
vim /etc/profile export PATH=/usr/local/nginx/sbin:$PATH source /etc/profile
新建并编辑/usr/lib/systemd/system/nginx.service
文件
vim /usr/lib/systemd/system/nginx.service
并添加以下内容(这里的配置是根据本身安装Nginx的路径来配置的,Nginx安装在了/usr/local
目录下)
[Unit] Description=The nginx HTTP and reverse proxy server After=network.target remote-fs.target nss-lookup.target [Service] Type=forking ExecStartPre=/usr/local/nginx/sbin/nginx -t 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
经过yum安装的nginx,默认的nginx.service配置以下,能够做为参考
# /usr/lib/systemd/system/nginx.service [Unit] Description=The nginx HTTP and reverse proxy server After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/run/nginx.pid # Nginx will fail to start if /run/nginx.pid already exists but has the wrong # SELinux context. This might happen when running `nginx -t` from the cmdline. # https://bugzilla.redhat.com/show_bug.cgi?id=1268621 ExecStartPre=/usr/bin/rm -f /run/nginx.pid ExecStartPre=/usr/sbin/nginx -t ExecStart=/usr/sbin/nginx ExecReload=/bin/kill -s HUP $MAINPID KillSignal=SIGQUIT TimeoutStopSec=5 KillMode=process PrivateTmp=true [Install] WantedBy=multi-user.target
重载daemon
执行下面的命令从新载入 systemd,扫描新的或有变更的单元便可
systemctl daemon-reload
设置开机自启
systemctl enable nginx.service # 设置开机自启 systemctl disable nginx.service # 取消开机自启服务
Nginx服务管理经常使用命令
systemctl status nginx.service # 查看Nginx状态 systemctl start nginx.service # 开启Nginx systemctl stop nginx.service # 关闭Nginx systemctl reload nginx.service # 重载配置 systemctl restart nginx.service # 重启Nginx(至关于stop&start)
服务启动检查
能够经过该命令查询80端口被谁占用
lsof -i :80
若是没法识别该命令,须要安装lsof
sudo yum -y install lsof
(1)cmake是新版MySQL的编译工具,必须安装
sudo yum -y install ncurses-devel perl perl-devel autoconf
不使用yum方式安装cmake,由于默认的cmake版本较低,这会致使在后面安装PHP时出现版本太低,没法安装的问题,由于安装MySQL须要cmake,因此在这里直接装最新稳定版cmake
cd /usr/local/src yum remove cmake wget https://github.com/Kitware/CMake/releases/download/v3.16.1/cmake-3.16.1.tar.gz tar -zxvf cmake-3.16.1.tar.gz cd cmake-3.16.1 ./configure \ --prefix=/usr/local/cmake make && make install cd /usr/local ln -s /usr/local/cmake/bin/cmake /usr/bin/cmake # ln -s /usr/local/cmake/share/cmake-3.16.1 /usr/share/cmake
若是安装的MySQL5.7及以上的版本,在编译安装以前须要安装boost,由于高版本mysql须要boots库的安装才能够正常运行。不然会报CMake Error at cmake/boost.cmake:81
错误
切换到/usr/local/src
目录,而后在这个目录下下载boost
MySQL5.7.28要求boost的版本是1.59,更高版本的不适用MySQL5.7.28
wget http://www.sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
# 添加MySQL用户 useradd -s /sbin/nologin -M mysql # 切换到/usr/src目录 cd /usr/local/src # 下载MySQL wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.28.tar.gz # 解压MySQL tar -zxvf mysql-5.7.28.tar.gz #解压boost,并移至mysql/boost tar -zxvf boost_1_59_0.tar.gz mv boost_1_59_0 mysql-5.7.28/boost # 进到MySQL目录 cd mysql-5.7.28 # 预编译 cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.7.28 \ -DWITH_BOOST=boost \ -DWITH_SYSTEMD=1 \ -DWITH_SSL=system \ -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \ -DMYSQL_DATADIR=/var/lib/mysql/data \ -DDEFAULT_CHARSET=utf8mb4 \ -DDEFAULT_COLLATION=utf8mb4_general_ci \ -DWITH_EXTRA_CHARSETS=all \ -DWITH_MYISAM_STORAGE_ENGINE=1 \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_MEMORY_STORAGE_ENGINE=1 \ -DWITH_READLINE=1 \ -DWITH_INNODB_MEMCACHED=1 \ -DWITH_DEBUG=OFF \ -DWITH_ZLIB=bundled \ -DENABLED_LOCAL_INFILE=1 \ -DENABLED_PROFILING=ON \ -DMYSQL_MAINTAINER_MODE=OFF \ -DMYSQL_TCP_PORT=3306 # 编译&安装 make && make install # 建立软连接 cd /usr/local ln -s mysql-5.7.28 mysql
# 添加到环境变量 vim /etc/profile export PATH=/usr/local/mysql/bin:$PATH source /etc/profile
在/var/lib
目录下建立一个mysql
文件夹
mkdir -p /var/lib/{mysql,mysql/data} touch /var/lib/mysql/mysqld.pid chown mysql.mysql -R /var/lib/mysql/
修改/etc/my.cnf
文件
# 修改/etc/my.cnf文件,编辑配置文件以下 [mysqld] character-set-server=utf8mb4 collation-server=utf8mb4_general_ci datadir=/var/lib/mysql/data socket=/var/lib/mysql/mysql.sock [mysqld_safe] log-error=/var/log/mysql/mysqld.log pid-file=/var/lib/mysql/mysqld.pid [client] default-character-set=utf8mb4
建立mysqld.log
和 mysqld.pid
文件,并修改文件权限
# 建立mysqld.log 和 mysqld.pid文件 mkdir /var/log/mysql touch /var/log/mysql/mysqld.log chown mysql.mysql -R /var/log/mysql/
初始化数据库
# 初始化数据库, –initialize 表示默认生成一个安全的密码,–initialize-insecure 表示不生成密码 mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/var/lib/mysql/data
建立一个/usr/lib/systemd/system/mysqld.service
文件,而后编辑内容以下
vim /usr/lib/systemd/system/mysqld.service
[Unit] Description=MySQL Server Documentation=man:mysqld(8) Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html After=network.target After=syslog.target [Install] WantedBy=multi-user.target [Service] User=mysql Group=mysql Type=forking PIDFile=/var/lib/mysql/mysqld.pid # Disable service start and stop timeout logic of systemd for mysqld service. TimeoutSec=0 # Execute pre and post scripts as root PermissionsStartOnly=true # Needed to create system tables ExecStartPre=/usr/local/mysql/bin/mysqld_pre_systemd # Start main service ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/var/lib/mysql/mysqld.pid $MYSQLD_OPTS # Use this to switch malloc implementation EnvironmentFile=/etc/my.cnf # Sets open_files_limit LimitNOFILE = 5000 Restart=on-failure RestartPreventExitStatus=1 PrivateTmp=false
重载daemon
执行下面的命令从新载入 systemd,扫描新的或有变更的单元便可
systemctl daemon-reload
启动MySQL
systemctl start mysqld.service # 启动MySQL systemctl stop mysqld.service # 关闭MySQL systemctl status mysqld.service # 查看MySQL状态
开机自启
systemctl enable mysqld.service # 设置开机自启 systemctl disable mysqld.service # 取消开机自启
mysql -u root -p #第一次登录不须要密码,回车便可 set password for root@localhost = password('root'); #修改密码
sudo yum -y install gcc gcc-c++ zip unzip libxml2 libxml2-devel curl-devel autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel gd-devel bzip2 bzip2-devel libsqlite3x libsqlite3x-devel oniguruma oniguruma-devel
升级libzip
yum remove libzip cd /usr/local/src wget https://libzip.org/download/libzip-1.5.2.tar.gz tar -zxvf libzip-1.5.2.tar.gz cd libzip-1.5.2 mkdir build cmake . make && make install echo '/usr/local/lib64 /usr/local/lib /usr/lib /usr/lib64'>>/etc/ld.so.conf ldconfig -v
cd /usr/local/src wget http://hk1.php.net/get/php-7.4.0.tar.gz/from/this/mirror -O php-7.4.0.tar.gz tar -zxvf php-7.4.0.tar.gz cd php-7.4.0 ./configure \ --prefix=/usr/local/php-7.4.0 \ --enable-fpm \ --with-fpm-user=nginx \ --with-fpm-group=nginx \ --with-zlib \ --enable-mysqlnd \ --enable-bcmath \ --enable-gd \ --with-pdo-mysql=mysqlnd \ --with-mysqli=mysqlnd \ --with-mysql-sock=/var/lib/mysql/mysql.sock \ --with-jpeg \ --with-freetype \ --with-iconv \ --with-openssl \ --with-curl \ --enable-mbstring \ --enable-static \ --enable-sockets \ --enable-xml make && make install
编译参数详解
./configure \ --prefix=/usr/local/php-7.4.0 \ # 指定安装路径 --enable-fpm \ # 表示激活PHP-FPM方式服务,即FactCGI方式运行PHP服务。 --with-fpm-user=nginx \ # 指定PHP-FPM进程管理的用户为www,此处最好和Nginx服务用户统一。 --with-fpm-group=nginx \ # 指定PHP-FPM进程管理用户组为www,此处最好和Nginx服务用户组统一。 --with-zlib \ # 打开zlib库的支持,用于http压缩传输 --enable-mysqlnd \ --with-pdo-mysql=mysqlnd \ --with-mysqli=mysqlnd \ --with-mysql-sock=/var/lib/mysql/mysql.sock \ --with-gd \ # 打开gd库的支持 --with-png-dir \ --with-jpeg-dir \ --with-freetype-dir \ --with-openssl \ # 打开openssl,加密传输时用到 --with-curl \ # 打开curl浏览工具的支持 --enable-mbstring \ # 多字节,字符串的支持 --enable-static \ # 生成静态连接库 --enable-zip \ # 打开对zip的支持 --enable-sockets \ # 打开 sockets 支持 --enable-xml
配置
cd /usr/local ln -s php-7.4.0 php cp /usr/local/src/php-7.4.0/php.ini-development /usr/local/php-7.4.0/lib/php.ini vim /usr/local/php/lib/php.ini date.timezone = PRC (大约在954行) expose_php = Off #避免PHP信息暴露在http头中(大约369行) display_errors = Off(生产环境设置为off,开发环境就设置为On,便于调试) 说明:设置了dispaly_errors为off后,须要在php-fpm.conf中开启错误日志记录路径error_log = log/php-fpm.log cd /usr/local/php cp etc/php-fpm.conf.default etc/php-fpm.conf cd /usr/local/php/etc/php-fpm.d/ cp www.conf.default www.conf cd /usr/local/php sbin/php-fpm ps -e | grep php-fpm 若是在编译PHP时指定了--with-mysql=mysqlnd和--with-pdo-mysql=mysqlnd的参数,那么在生产中可能会遇到socket链接问题,解决办法是在php.ini里加入命令: pdo_mysql.default_socket=/usr/local/mysql/tmp/mysql.sock 最好是在编译PHP的时候,指定mysql.socket的位置: --with-mysql-sock=/usr/local/mysql/tmp/mysql.sock
管理PHP-FPM
vim /usr/local/php/etc/php-fpm.conf pid = run/php-fpm.pid error_log = log/php-fpm.log #24行这个在php.ini设置display_errors = Off时启用 设置完以后重启服务器 向进程发送信号,就能够完成进程管理 中止: kill -INT `cat /usr/local/php/var/run/php-fpm.pid` 平滑中止: kill -QUIT `cat /usr/local/php/var/run/php-fpm.pid` 重启:kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid` 从新打开日志:kill -USR1 `cat /usr/local/php/var/run/php-fpm.pid`
配置环境变量
vim /etc/profile export PATH=/usr/local/php/bin:$PATH source /etc/profile
其实php-fpm.service文件php已经帮咱们配置好了,只须要咱们复制到指定位置,并启用就好了。
cp /usr/local/src/php-7.4.0/sapi/fpm/php-fpm.service /usr/lib/systemd/system/
并编辑该文件
vim /usr/lib/systemd/system/php-fpm.service
php-fpm.service文件内容以下:
# It's not recommended to modify this file in-place, because it # will be overwritten during upgrades. If you want to customize, # the best way is to use the "systemctl edit" command. [Unit] Description=The PHP FastCGI Process Manager After=network.target [Service] Type=simple PIDFile=/usr/local/php/var/run/php-fpm.pid ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.conf ExecReload=/bin/kill -USR2 $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
重载daemon
执行下面的命令从新载入 systemd,扫描新的或有变更的单元便可
systemctl daemon-reload
开机自启
systemctl enable php-fpm.service systemctl disable php-fpm.service
启动php-fpm
systemctl start php-fpm.service
nginx.conf配置
#user nobody; # 有一个工做的子进程,能够自行修改,但太大无益,由于要争夺CPU # 通常设置CPU数 * 核数 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 { #通常是配置Nginx进程与链接的特性 #若几个同时工做 multi_accept on; #打开同时接受多个新网络链接请求的功能。 use epoll; #使用epoll事件驱动,由于epoll的性能相比其余事件驱动要好不少 worker_connections 10240; #这是指一个子进程最大容许链接10240个链接 } http { # 这是配置http服务器的主要段 include mime.types; default_type application/octet-stream; #隐藏Nginx软件版本号 server_tokens off; #激活tcp_nodelay功能,提升I/O性能 tcp_nodelay on; # 设置读取客户端请求头数据的超时时间。此处的数值为15,其单位是秒,为经验参考值 client_header_timeout 15; # 设置读取客户端请求体的超时时间 client_body_timeout 15; # 指定响应客户端的超时时间 send_timeout 25; # 上传文件大小限制 client_max_body_size 8m; #压缩配置 gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.0; gzip_comp_level 2; gzip_types text/css text/xml application/javascript; gzip_vary on; #include extra/gzip.config; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; include extra/*.conf; }
# 解压源码文件 tar -zxf redis-5.0.6.tar.gz # 切换到解压目录 cd redis-5.0.6 # 编译安装 mkdir /usr/local/redis-5.0.6 make PREFIX=/usr/local/redis-5.0.6 install mkdir /usr/local/redis-5.0.6/etc cp redis.conf /usr/local/redis-5.0.6/etc/ # 建立软连接 cd /usr/local ln -sf redis-5.0.6 redis
vim /etc/profile export PATH=/usr/local/redis/bin:$PATH source /etc/profile # 使修改当即生效
让redis
之后台进程的形式运行
vim /usr/local/redis/etc/redis.conf # daeonize no(改成) # 改成 -> daemonize yes
在 /etc/systemd/system/
添加一个redis.service
文件,并添加以下内容
[Unit] Description=Redis After=network.target [Service] Type=forking PIDFile=/var/run/redis_6379.pid ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf ExecStop=/usr/local/redis/bin/redis-cli shutdown PrivateTmp=true [Install] WantedBy=multi-user.target
重载daemon
执行下面的命令从新载入 systemd,扫描新的或有变更的单元便可
systemctl daemon-reload
开机自启
systemctl enable redis.service
启动redis服务
systemctl start redis.service