Home >Backend Development >PHP Tutorial >How to set up a PHP environment? PHP environment setup (detailed explanation)
How to build the PHP environment: first install wget through the command "yum install wget"; then install Nginx, and compile and depend on the gcc environment; then compile and install MySQL, and configure environment variables; finally compile and install PHP through the install command , and start "PHP-FPM".
How to set up a PHP environment? What this tutorial brings to you is how to set up a PHP environment? PHP environment setup (detailed explanation)
Follow the steps in the article, which can help you build an LNMP environment or LAMP environment on CentOS6.* system. These environments can be used as the online operating environment of the server.
related suggestion:
●《Recommended 6 best PHP environment building tools in 2019》
As a PHP Developers, we must know how to build a PHP development environment. The current mainstream PHP development environment combination is LAMP and LNMP. This article will introduce how to build an LNMP development environment on CentOS.
Directory:
##Five: /etc/rc.local does not execute when CentOS7 is started
1: Preparation work
1. Install wget
wget is a free tool that automatically downloads files from the Internet. It supports downloading through the three most common TCP/IP protocols: HTTP, HTTPS, and FTP, and can use HTTP proxy.
sudo yum install wget
2. Install net-tools
If you cannot use the ifconfig command when mini-installing CentOS7, you need to installnet-tools, if If you are installing the CentOS6 version, you do not need to install it
sudo yum install net-tools
3. Update the yum source
yum -y update
4. Install vim
sudo yum install vim
5. Configuration display line number
vim ~/.vimrc set nu #输入 set nu 后退出保存
2: Install Nginx
1. Install dependencies
(1) Installnginx You need to compile the source code downloaded from the official website first. The compilation depends on the gcc environment. If there is no gcc environment, you need to install gcc-c.
PCRE is a Perl library, Chinese "Perl compatible regular expression library". The purpose of installing Nginx is to enable Nginx to support the rewrite module with URI rewriting function. If the pcre library is not installed, Nginx cannot use the rewrite module function. The Rewrite module function of Nginx is almost necessary for enterprise applications.
zlib The library provides many compression and decompression methods. nginx uses zlib to gzip the contents of the http package, so the zlib library needs to be installed on Centos.
OpenSSL is a powerful secure socket layer cryptographic library, including major cryptographic algorithms, commonly used key and certificate encapsulation management functions and SSL protocols, and provides a wealth of application for testing or other purposes. nginx not only supports the http protocol, but also supports https (that is, transmitting http over the ssl protocol), so the OpenSSL library needs to be installed.
yum -y install gcc gcc-c++ zlib zlib-devel yum -y install pcre pcre-devel openssl openssl-develNote: The pcre version installed by the yum installation method is relatively low, but it basically does not affect the use of
Check the basic dependency package
The above dependency installation After completion, you can check whether each dependency installation is successful by using the following commandrpm -qa pcre pcre-devel rpm -qa zlib zlib-devel rpm -qa openssl openssl-devel rpm -qa pcre pcre-devel
2. Compile and install Nginx
# 这里我们把安装包都放到了/usr/src目录下,便于统一管理 cd /usr/src #切换到软件包目录 wget https://nginx.org/download/nginx-1.14.1.tar.gz #下载nginx源码包 useradd nginx -s /sbin/nologin -M #创建nginx用户用于管理nginx程序 tar zxvf nginx-1.14.1.tar.gz #解压nginx源码包 cd nginx-1.14.1 #预编译 ./configure \ --user=nginx \ --group=nginx \ --prefix=/usr/local/nginx-1.14.1 \ --with-http_v2_module \ --with-http_ssl_module \ --with-http_stub_status_module make && make install #编译 和 安装 cd /usr/local ln -s nginx-1.14.1 nginx #创建nginx的软链接
Installation instructions
--prefix=PATH #设置安装路劲 --user=USER #进程用户权限 --group=GROUP #进程用户组权限 --with-http_stub_status_module #激活状态信息 --with-http_ssl_module #激活ssl功能
3. Configure environment variables
vim /etc/profile export PATH=/usr/local/nginx/sbin:$PATH source /etc/profile
4. Configure auto-start at boot
vim /etc/rc.local # Nginx开机自启 /usr/local/nginx/sbin/nginx &
5.Nginx common commands
/usr/local/nginx/sbin/nginx -t # 检查Nginx配置语法是否有误 /usr/local/nginx/sbin/nginx #启动 /usr/local/nginx/sbin/nginx -s stop #立即停止 /usr/local/nginx/sbin/nginx -s quit #平滑停止 /usr/local/nginx/sbin/nginx -s reload #重载配置 /usr/local/nginx/sbin/nginx -s reopen #重开日志
Service startup check
You can use this command to check who is occupying port 80lsof -i :80If you cannot recognize this command, you need to install it
lsof
sudo yum install lsof
6. Turn off the firewall
CentOS6: service iptables stop 临时关闭 chkconfig --level 2345 iptables off 永久关闭 CentOS7: systemctl stop firewalld.service #令关闭防火墙 systemctl disable firewalld.service #关闭防火墙开机自启动 通过浏览器输入IP测试是否成功
3: Install MySQL
1. Install dependencies
(1)cmake is the compilation tool of the new version of MySQL, which must be installedsudo yum install gcc gcc-c++ cmake ncurses-develIf your system is CentOS7, you also need to install the following dependencies
sudo yum install perl perl-devel autoconf
2. Compile and install MySQL
useradd -s /sbin/nologin -M mysql # 添加MySQL用户 wget https://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.42.tar.gz tar zxvf mysql-5.6.42.tar.gz cd mysql-5.6.42 cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.6.42 \ -DMYSQL_UNIX_ADDR=/usr/local/mysql-5.6.42/tmp/mysql.sock \ -DMYSQL_DATADIR=/usr/local/mysql-5.6.42/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
3.MySQL configuration
cd /usr/local/mysql-5.6.42 chown mysql.mysql /usr/local/mysql-5.6.42/data mkdir tmp chown mysql.mysql /usr/local/mysql-5.6.42/tmp rm -f /etc/my.cnf cp support-files/my-default.cnf /etc/my.cnf scripts/mysql_install_db --defaults-file=/etc/my.cnf --user=mysql
4.Join the daemon process
cd /usr/local ln -s mysql-5.6.42 mysql cd /usr/local/mysql cp support-files/mysql.server /etc/init.d/mysqld chkconfig --add mysqld
5. Configure environment variables
vim /etc/profile export PATH=/usr/local/mysql/bin:$PATH source /etc/profile
6. Start MySQL
service mysqld start mysql -u root -p #第一次登陆不需要密码,回车即可 set password for root@localhost = password('root'); #修改密码
四:安装PHP
1.安装依赖
sudo yum 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
2.编译安装PHP
cd /usr/src wget http://hk1.php.net/get/php-7.2.12.tar.gz/from/this/mirror -O php-7.2.12.tar.gz tar zxvf php-7.2.12.tar.gz cd php-7.2.12 ./configure \ --prefix=/usr/local/php-7.2.12 \ --enable-fpm \ --with-fpm-user=nginx \ --with-fpm-group=nginx \ --with-zlib \ --enable-mysqlnd \ --with-pdo-mysql=mysqlnd \ --with-mysqli=mysqlnd \ --with-mysql-sock=/usr/local/mysql/tmp/mysql.sock \ --with-gd \ --with-png-dir \ --with-jpeg-dir \ --with-freetype-dir \ --with-iconv-dir \ --with-openssl \ --with-curl \ --enable-bcmath \ --enable-mbstring \ --enable-static \ --enable-zip \ --enable-sockets \ --enable-xml make && make install
3.PHP配置
cd /usr/local ln -s php-7.2.12 php cp /usr/src/php-7.2.12/php.ini-development /usr/local/php-7.2.12/lib/php.ini vim /usr/local/php/lib/php.ini date.timezone = PRC # 修改时区(大约在932行) expose_php = Off # 避免PHP信息暴露在http头中(大约369行) display_errors = Off# 生产环境设置为off,开发环境就设置为On,便于调试 说明:设置了dispaly_errors为off后,需要在php-fpm.conf中开启错误日志记录路径error_log = log/php-fpm.log cd 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 # 管理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` 如果在编译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
4.配置环境变量
vim /etc/profile export PATH=/usr/local/php/bin:$PATH source /etc/profile
5.配置开机自启
vim /etc/rc.local # PHP-FPM自动启动 /usr/local/php/sbin/php-fpm &
6.启动PHP-FPM
cd /usr/local/php sbin/php-fpm # 启动PHP-FPM ps -e | grep php-fpm
7.配置Nginx和PHP关联
#user nobody; 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 { use epoll; worker_connections 1024; } 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 16 8k; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/css text/xml text/plain 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; server { listen 80; server_name www.nginx.com; root html/blog/public; #access_log logs/host.access.log main; location / { index index.php index.html index.htm; if (!-e $request_filename) { rewrite ^/(.*)$ /index.php/$1 last; } } #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; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_split_path_info ^(.+\.php)(.*)$; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } }
五:CentOS7开机/etc/rc.local不执行问题
CentOS7中,默认开机不再执行/etc/rc.local,查询/etc/rc.local里的内容可以发现如下描述:
#!/bin/bash # THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES # # It is highly advisable to create own systemd services or udev rules # to run scripts during boot instead of using this file. # # In constrast to previous versions due to parallel execution during boot # this script will NOT be run after all other services. # # Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure # that this script will be executed during boot.
翻译过来就是
#这个文件是为了兼容性的问题而添加的。 # #强烈建议创建自己的systemd服务或udev规则来在开机时运行脚本而不是使用这个文件。 # #与以前的版本引导时的并行执行相比较,这个脚本将不会在其他所有的服务后执行。 # #请记住,你必须执行“chmod +x /etc/rc.d/rc.local”来确保确保这个脚本在引导时执行。
所以要解决开机不执行问题,只需要执行下面的命令然后重启服务器。
chmod +x /etc/rc.d/rc.local
完成上面这些步骤,一个LNMP环境就配好啦,重启服务器即可。
The above is the detailed content of How to set up a PHP environment? PHP environment setup (detailed explanation). For more information, please follow other related articles on the PHP Chinese website!