Nginx+PHP+MySQL安装参考

Nginx+PHP+MySQL安装参考


CentOS7环境
配置CentOS7网络:

CentOS(最小安装)默认是不打开网络的

启动网络
vi打开:/etc/sysconfig/network-scripts/ifcfg-ens33 文件
将 “ONBOOT:no”属性修改为:“ONBOOT:yes”

重启网络服务
# sudo service network restart 

查看IP
# ip addr


Nginx服务:
安装依赖包

    安装:
    # yum -y install build-essential
    
    安装:更多依赖包
    # yum -y install gcc automake autoconf libtool make

    安装:g++
    # yum -y install gcc gcc-c++

安装PCRE库
选定源码目录 ==> ftp下载PCRE库 ==> 安装PCRE库
# cd /usr/local/src
# wget ftp://ftp.pcre.org/pub/pcre/pcre-8.42.tar.gz
# tar -zxvf pcre-8.42.tar.gz
# cd ./pcre-8.42
# ./configure
# make && make install

安装zlib源码包
下载zlib源码包 ==> 安装zlib包
# cd /usr/local/src
# wget http://zlib.net/zlib-1.2.11.tar.gz
# tar -zxvf zlib-1.2.11.tar.gz
# cd zlib-1.2.11
# ./configure
# make && make install

安装openssl源码包
# cd /usr/local/src
# wget https://www.openssl.org/source/openssl-1.1.0k.tar.gz
# tar -zxvf openssl-1.1.0k.tar.gz
# cd ./openssl-1.1.0k
# mkdir /usr/local/openssl
# ./configure --prefix=/usr/local/openssl
# make && make install

安装Nginx
# cd /usr/local/src
# wget http://nginx.org/download/nginx-1.16.1.tar.gz
# tar -zxvf nginx-1.16.1.tar.gz
# cd nginx-1.16.1
# groupadd -r nginx
# useradd -s /bin/false -M nginx -g nginx

# ./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--pid-path=/usr/local/nginx/logs/nginx.pid \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_mp4_module  \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-pcre=/usr/local/src/pcre-8.42 \
--with-zlib=/usr/local/src/zlib-1.2.11 \
--with-openssl=/usr/local/src/openssl-1.1.0k \
# make
执行make 进行编译,如果编译成功的话会在objs中出现一个nginx文件
特别注意:
在已安装的nginx上进行添加模块的话执行到这里就行了,把objs中的nginx替换掉之前的安装的nginx/sbin/中的nginx文件,
然后重启nginx就行了,如果执行下一步的install,会导致之前安装的nginx被覆盖,比如之前配置好的nginx.conf文件

# make install

参数说明:
--prefix 用于指定nginx编译后的安装目录
--add-module 为添加的第三方模块,如添加fdfs的nginx模块,则--add-module=/home/fastdfs-nginx-module/src
--with..._module 表示启用的nginx模块,如此处启用了http_ssl_module模块

可能出现的错误:
出现:./configure:  error: the HTTP rewrite module requires the PCRE library.
解决方法:yum -y install pcre-devel

出现:SSL modules require the OpenSSL library
解决方法:yum install openssl-devel 

或是下载源码,指定对应路径,如:
--with-pcre=/usr/local/src/pcre-8.41 指的是pcre-8.42 的源码路径
--with-zlib=/usr/local/src/zlib-1.2.11 指的是zlib-1.2.11 的源码路径
--with-openssl=/usr/local/src/openssl-1.1.0g 指的是openssl-1.1.0k 的源码路径

nginx编译选项
make是用来编译的,它从Makefile中读取指令,然后编译。
make install是用来安装的,它也从Makefile中读取指令,安装到指定的位置。
configure命令是用来检测你的安装平台的目标特征的。它定义了系统的各个方面,包括nginx的被允许使用的连接处理的方法,比如它会检测你是不是有CC或GCC,
并不是需要CC或GCC,它是个shell脚本,执行结束时,它会创建一个Makefile文件。nginx的configure命令支持以下参数:
    --prefix=*path* 定义一个目录,存放服务器上的文件 ,也就是nginx的安装目录。默认使用 /usr/local/nginx。
    --sbin-path=*path* 设置nginx的可执行文件的路径,默认为 *prefix*/sbin/nginx.
    --conf-path=*path* 设置在nginx.conf配置文件的路径。nginx允许使用不同的配置文件启动,通过命令行中的-c选项。默认为*prefix*/conf/nginx.conf.
    --pid-path=*path* 设置nginx.pid文件,将存储的主进程的进程号。安装完成后,可以随时改变的文件名 , 在nginx.conf配置文件中使用 PID指令。默认情况下,文件名 为``*prefix*/logs/nginx.pid.
    --error-log-path=*path* 设置主错误,警告,和诊断文件的名称。安装完成后,可以随时改变的文件名 ,在nginx.conf配置文件中 使用 的error_log指令。默认情况下,文件名 为*prefix*/logs/error.log.
    --http-log-path=*path* 设置主请求的HTTP服务器的日志文件的名称。安装完成后,可以随时改变的文件名 ,在nginx.conf配置文件中 使用 的access_log指令。默认情况下,文件名 为*prefix*/logs/access.log.
    --user=*name* 设置nginx工作进程的用户。安装完成后,可以随时更改的名称在nginx.conf配置文件中 使用的 user指令。默认的用户名是nobody。
    --group=*name* 设置nginx工作进程的用户组。安装完成后,可以随时更改的名称在nginx.conf配置文件中 使用的 user指令。默认的为非特权用户。
    --with-select_module --without-select_module 启用或禁用构建一个模块来允许服务器使用select()方法。该模块将自动建立,如果平台不支持的kqueue,epoll,rtsig或/dev/poll。
    --with-poll_module --without-poll_module 启用或禁用构建一个模块来允许服务器使用poll()方法。该模块将自动建立,如果平台不支持的kqueue,epoll,rtsig或/dev/poll。
    --without-http_gzip_module — 不编译压缩的HTTP服务器的响应模块。编译并运行此模块需要zlib库。
    --without-http_rewrite_module 不编译重写模块。编译并运行此模块需要PCRE库支持。
    --without-http_proxy_module — 不编译http_proxy模块。
    --with-http_ssl_module — 使用https协议模块。默认情况下,该模块没有被构建。建立并运行此模块的OpenSSL库是必需的。
    --with-pcre=*path* — 设置PCRE库的源码路径。PCRE库的源码(版本4.4 - 8.30)需要从PCRE网站下载并解压。其余的工作是Nginx的./ configure和make来完成。正则表达式使用在location指令和 ngx_http_rewrite_module 模块中。
    --with-pcre-jit —编译PCRE包含“just-in-time compilation”(1.1.12中, pcre_jit指令)。
    --with-zlib=*path* —设置的zlib库的源码路径。要下载从 zlib(版本1.1.3 - 1.2.5)的并解压。其余的工作是Nginx的./ configure和make完成。ngx_http_gzip_module模块需要使用zlib 。
    --with-cc-opt=*parameters* — 设置额外的参数将被添加到CFLAGS变量。例如,当你在FreeBSD上使用PCRE库时需要使用:--with-cc-opt="-I /usr/local/include。.如需要需要增加 select()支持的文件数量:--with-cc-opt="-D FD_SETSIZE=2048".
    --with-ld-opt=*parameters* —设置附加的参数,将用于在链接期间。例如,当在FreeBSD下使用该系统的PCRE库,应指定:--with-ld-opt="-L /usr/local/lib".

安装完成后,按照安装的参数,安装的启动目录在/usr/local/nginx

[root@localhost nginx]# ls -l
总用量 76
drwxr-xr-x. 2 root root 4096 9月   8 09:46 conf
-rw-r--r--. 1 root root 1077 9月   8 10:34 fastcgi.conf
-rw-r--r--. 1 root root 1077 9月   8 10:34 fastcgi.conf.default
-rw-r--r--. 1 root root 1007 9月   8 10:34 fastcgi_params
-rw-r--r--. 1 root root 1007 9月   8 10:34 fastcgi_params.default
drwxr-xr-x. 2 root root   40 9月   8 09:46 html
-rw-r--r--. 1 root root 2837 9月   8 10:34 koi-utf
-rw-r--r--. 1 root root 2223 9月   8 10:34 koi-win
drwxr-xr-x. 2 root root   41 9月   8 10:37 logs
-rw-r--r--. 1 root root 5231 9月   8 10:34 mime.types
-rw-r--r--. 1 root root 5231 9月   8 10:34 mime.types.default
-rw-r--r--. 1 root root 2656 9月   8 10:34 nginx.conf
-rw-r--r--. 1 root root 2656 9月   8 10:34 nginx.conf.default
-rw-r--r--. 1 root root    6 9月   8 10:37 nginx.pid
drwxr-xr-x. 2 root root   36 9月   8 10:34 sbin
-rw-r--r--. 1 root root  636 9月   8 10:34 scgi_params
-rw-r--r--. 1 root root  636 9月   8 10:34 scgi_params.default
-rw-r--r--. 1 root root  664 9月   8 10:34 uwsgi_params
-rw-r--r--. 1 root root  664 9月   8 10:34 uwsgi_params.default
-rw-r--r--. 1 root root 3610 9月   8 10:34 win-utf
[root@localhost nginx]# pwd
/usr/local/nginx


启动Nginx服务:

由于CentOS-7防火墙不开发端口,所以在本地测试中,可以选择关闭防火墙或者允许开发80端口
CentOS防火墙

# systemctl status firewalld     ==> 防火墙状态
# systemctl start firewalld       ==> 开启防火墙
# systemctl stop firewalld        ==> 关闭防火墙
# systemctl restart firewalld     ==> 重启防火墙
# firewall-cmd --reload          ==> 防火墙重载
# firewall-cmd --permanent --zone=public --add-port=80/tcp
    permanent: 永久有效
    zone:作用域
    --add-port=80/tcp:添加-端口=端口/通信协议

开放端口或关闭防火墙后就可以启动nginx服务
服务启动

[root@localhost nginx]# netstat -ano | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      off (0.00/0/0)
unix  3      [ ]         STREAM     CONNECTED     80900    
unix  3      [ ]         STREAM     CONNECTED     80899    
[root@localhost nginx]# /usr/local/nginx/sbin/nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()

通过netstat查看端口网络状态,是否有服务占用80端口;通过调用nginx的启动目录实现nginx服务启动

Nginx服务维护
为了避免每次开机手动启动,可以使用命令脚本,注册服务,开机自启动等

创建nginx启动命令脚本
#vi /etc/init.d/nginx
插入以下内容, 注意修改PATH和NAME字段, 匹配自己的安装路径 (这段是从网上copy的)
#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f nginx defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add nginx'

### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO

# Author:   licess
# website:  http://lnmp.org

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=nginx
NGINX_BIN=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/$NAME.conf
PIDFILE=/usr/local/nginx/logs/$NAME.pid

case "$1" in
    start)
        echo -n "Starting $NAME... "

        if netstat -tnpl | grep -q nginx;then
            echo "$NAME (pid `pidof $NAME`) already running."
            exit 1
        fi

        $NGINX_BIN -c $CONFIGFILE

        if [ "$?" != 0 ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
        ;;

    stop)
        echo -n "Stoping $NAME... "

        if ! netstat -tnpl | grep -q nginx; then
            echo "$NAME is not running."
            exit 1
        fi

        $NGINX_BIN -s stop

        if [ "$?" != 0 ] ; then
            echo " failed. Use force-quit"
            exit 1
        else
            echo " done"
        fi
        ;;

    status)
        if netstat -tnpl | grep -q nginx; then
            PID=`pidof nginx`
            echo "$NAME (pid $PID) is running..."
        else
            echo "$NAME is stopped"
            exit 0
        fi
        ;;

    force-quit)
        echo -n "Terminating $NAME... "

        if ! netstat -tnpl | grep -q nginx; then
            echo "$NAME is not running."
            exit 1
        fi

        kill `pidof $NAME`

        if [ "$?" != 0 ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
        ;;

    restart)
        $0 stop
        sleep 1
        $0 start
        ;;

    reload)
        echo -n "Reload service $NAME... "

        if netstat -tnpl | grep -q nginx; then
            $NGINX_BIN -s reload
            echo " done"
        else
            echo "$NAME is not running, can't reload."
            exit 1
        fi
        ;;

    configtest)
        echo -n "Test $NAME configure files... "

        $NGINX_BIN -t
        ;;

    *)
        echo "Usage: $0 {start|stop|force-quit|restart|reload|status|configtest}"
        exit 1
        ;;

esac

设置执行权限
chmod a+x /etc/init.d/nginx
注册成服务
chkconfig --add nginx
设置开机启动
chkconfig nginx on

重启
shutdown -h 0 -r

查看nginx服务是否自动启动
ss -apn|grep nginx

对nginx服务执行停止/启动/重新读取配置文件操作
#启动nginx服务
systemctl start nginx.service
#停止nginx服务
systemctl stop nginx.service
#重启nginx服务
systemctl restart nginx.service
#重新读取nginx配置(这个最常用, 不用停止nginx服务就能使修改的配置生效)
systemctl reload nginx.service

如出现:nginx: [error] invalid PID number "" in "/usr/local/nginx/logs/nginx.pid"
则需通过nginx –c ../conf/nginx.conf    命令指定nginx的配置

关于nginx.conf配置文件
在安装完nginx后会在conf目录中产生一个nginx.conf的配置文件
里面有些默认配置,可根据自己的需求进行更改
示例:
#user  nobody;
worker_processes  1;
events {
     use epoll;
        worker_connections  51200;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    proxy_connect_timeout 600s;
    proxy_read_timeout 600s;
    proxy_send_timeout 600s;
    proxy_buffer_size 64k;
    proxy_buffers 4 32k;
    proxy_busy_buffers_size 64k;
    proxy_temp_file_write_size 64k;
    proxy_ignore_client_abort on;
    client_max_body_size 200m;  #此参数在使用fdfs上传可控制上传文件的大小
#日志的输出格式,如需打印请求的body参数信息,可在$body_bytes_sent后添加 $request_body 
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; #设置日志输出的位置
access_log on;  #是否开启日志,开启on,关闭off
#负载均衡配置
upstream test.com {
ip_hash;
server 192.168.68.9:8080;
server 192.168.68.72:8080;
}
    server {
        listen       80; #监听的端口,http默认监听端口为80
        server_name  localhost; #监听的主机名
        location / {
#设置请求的头部中主机名为请求的主机名,而不是代理的nginx的主机名
      proxy_set_header Host $host:$server_port;
#代理的目标地址,如果要进行负载均衡,目标地址可添test.com
         proxy_pass http://192.168.68.9:8080;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
#https配置,https默认监听端口为443
    server {
        listen  443;
        server_name system.test.com;
        ssl on;
        ssl_certificate_key cert/system.key; #ssl key文件的位置,此处使用配置文件的相对路径
        ssl_certificate cert/system.pem; #证书文件,此处为阿里云云盾证书生成的.pem也可修改扩展名为熟悉的.crt
        ssl_session_timeout 5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        location / {
             proxy_pass http://192.168.68.9:8080;
        }
    }
    #以下为我的fdfs文件配置,没有使用fdfs可以不用配置
    server {
        listen       9300;        
        server_name  localhost;   
    #location /group1/M01 {
         #   root   /home/fdfs/storage2/data;
          #  ngx_fastdfs_module;         
        #}
        
        location /group1/M00 {
           root   /home/fdfs/storage1/data;
           ngx_fastdfs_module;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
#include vhost/*.conf;
}

关于https配置ssl
如果在安装nginx的时候没有安装 --with-http_ssl_module模块要先安装该模块
nginx –V 可查看已经安装的模块
添加一个https的server大概如下面这个样子
server {
         listen 443;   ##默认的监听端口为443
         server_name  localhost;
         ssl on;
         ssl_certificate_key  xxx.key; ##私钥
         ssl_certificate  xxx.crt; ##证书,证书中包含公钥和私钥加密后的签名信息
 
        location / {
            root   html;
            index  index.html index.htm;
             proxy_pass http://xxx.xxx.xxx.xxx:xxx;
        }
         
    }

--------------------------------MySQL安装---------------------------

--------------------------------PHP环境安装------------------------------
编译安装php-fpm
PHP-FPM是一个PHP FastCGI管理器,是只用于PHP的

安装依赖包
# yum -y install libmcrypt-devel mhash-devel libxslt-devel \
libjpeg libjpeg-devel libpng libpng-dvevl 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

源码安装PHP:
# cd /usr/local/src
# wget http://php.net/get/php-5.6.27.tar.gz/from/a/mirror
# tar -zxvf php-5.6.27.tar.gz 
# ./configure \
--prefix=/usr/local/php56 \
--with-config-file-path=/usr/local/php56/etc \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-opcache \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-gettext \
--enable-mbstring \
--with-iconv \
--with-mcrypt \
--with-mhash \
--with-openssl \
--enable-bcmath \
--enable-soap \
--with-libxml-dir \
--enable-pcntl \
--enable-shmop \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-sockets \
--with-curl \
--with-zlib \
--enable-zip \
--with-bz2 \
--with-readline \
--enable-gd-native-ttf \
--with-gd
#make
#make install
安装的参数说明:
""" 安装路径 """
--prefix=/usr/local/php56 \

""" php.ini 配置文件路径 """
--with-config-file-path=/usr/local/php56/etc \

""" 优化选项 """
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \

""" 启用 opcache,默认为 ZendOptimizer+(ZendOpcache) """
--enable-opcache \

""" FPM """
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \

""" MySQL """
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \

""" 国际化与字符编码支持 """
--with-gettext \
--enable-mbstring \
--with-iconv \

""" 加密扩展 """
--with-mcrypt \
--with-mhash \
--with-openssl \

""" 数学扩展 """
--enable-bcmath \

""" Web 服务,soap 依赖 libxml """
--enable-soap \
--with-libxml-dir \

""" 进程,信号及内存 """
--enable-pcntl \
--enable-shmop \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \

""" socket & curl """
--enable-sockets \
--with-curl \

""" 压缩与归档 """
--with-zlib \
--enable-zip \
--with-bz2 \

""" GNU Readline 命令行快捷键绑定 """
--with-readline

""gd库支持""
--enable-gd-native-ttf \
--with-gd

注:依赖包要同时安装依赖包的本身,以及该依赖包的开发版,例如:zlib和zlib-devel。配置过程中可能会出现一些问题,
主要是依赖包没有安装开发板,或者安装的依赖包版本过低导致的,更多编译参数请使用 ./configure --help 查看。

执行configure时,发生报错:
1. Please reinstall the libzip distribution 
删除了旧版的libzip(“yum remove libzip”),之后下载libzip源码包进行本地执行configure+make&&make install
libzip的安装:
# cd /usr/local/src
# wget https://libzip.org/download/libzip-1.5.2.tar.gz
# tar -zxf libzip-1.2.0.tar.gz
# cd libzip-1.2.0
# ./configure
# make && make install

2. configure: error: mcrypt.h not found. Please reinstall libmcrypt
libmcrypt的安装:
# cd /usr/local/src
# wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/attic/libmcrypt/libmcrypt-2.5.7.tar.gz
# tar -zxvf libmcrypt-2.5.7.tar.gz
# cd libmcrypt-2.5.7
# ./configure --prefix=/usr/local
# make && make install

3. Don't know how to define struct flock on this system, set --enable-opcache=no
查看--enble-opchahe的作用
#./configure --help|grep opcache--enable-opcache
Enable Zend OPcache support
可知这个是与 php 的加速模块 zend相关的,不能省略

vim /etc/ld.so.conf.d/local.conf     # 编辑库文件
/usr/local/lib
/usr/local/lib64
:wq                                  # 保存退出
ldconfig -v                          # 使之生效

4. Please reinstall readline - I cannot find readline.h
yum -y install readline-devel


至此!PHP-fpm安装的基本流程结束了,下一步就需要配置文件
用户配置文件

    为php提供配置文件:php.ini

# cp php.ini-production /usr/local/php/lib/php.ini

    为php-fpm提供配置文件

# cd /usr/local/php
# cp etc/php.fpm.conf.default etc/php-fpm.conf
# vi etc/php-fpm.conf

vi打开php-fpm.conf文件:

将文件的尾部的索引;修改成实际的目录

include=/usr/local/php/etc/php-fpm.d/*.conf

根据参数--with-fpm-user=www,添加用户和组:

useradd www
groupadd -g www www

默认情况下,etc/php-fpm.d/目录下有一个“www .conf.defalut”用户配置文件

# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
# vi /usr/local/php/etc/php-fpm.d/www.conf

修改“www.conf"文件中的user和group的value;添加用户和组

user = www
group = www

    启动php-fpm服务

# /usr/local/php/sbin/php-fpm
# ps aux | grep php-fpm [验证服务启动]
# netstat -tln | grep 9000 [验证网络端口是否使用]

[root@localhost /]# ps aux | grep php-fpm
root      41831  0.0  0.3 221264  6220 ?        Ss   08:54   0:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
mirror    41832  0.0  0.2 221264  5748 ?        S    08:54   0:00 php-fpm: pool www
mirror    41833  0.0  0.2 221264  5748 ?        S    08:54   0:00 php-fpm: pool www
root      41835  0.0  0.0 110292   916 pts/0    R+   08:54   0:00 grep --color=auto php-fpm
[root@localhost /]# netstat -tln | grep 9000
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN   

至此!php-fpm服务启动成功!
Nginx+PHP环境配置

    打开nginx.conf(nginx配置文件)

[root@localhost nginx]# vi ./nginx.conf


#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 {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #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;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        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;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}

修改server配置块中的location和php后端请求配置块

  server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm index.php
        }

        #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;
        #}
    }

在location配置块中添加index.php首页

php请求和后端php-fpm模块进行通信,需要配置location ~\ .php$配置块

​ root:配置php程序文件的根目录

*** 修改配置文件的第一行:”user“属性为我们之前配置的用户**,表示nginx的权限

至此!我们的Nginx和php的环境完成简单的配置!
大功告成
启动步骤:

    启动Nginx服务

    # /usr/local/nginx/sbin/nginx

    启动php-fpm服务

    # /usr/local/php/sbin/php-fpm 或 systemctl restart php-fpm

    启动mysql服务

    # systemctl start mysqld

phpinfo():

在Nginx的目录html中添加一个php文件:”index.php“

<?php
    phpinfo();
?>

测试数据库连接:

编写一个连接数据库行为的php文件:”mysql.php“

php和mysql之间的连接操作依靠的是”mysqli“

<?php
    $conn = mysqli_connect("127.0.0.1","root","926498");
    if(! $conn ) {
        echo "连接失败".mysqli_connect_error();
    } else {
         echo "连接成功";
    }
?>

至此!PHP+Nginx+MySQL环境完成了基本的搭建!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/web/84943.shtml
繁体地址,请注明出处:http://hk.pswp.cn/web/84943.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

JavaScript中的反射魔法:揭秘Reflect对象的核心方法(下)

JavaScript中的Reflect对象&#xff1a;高级方法解析&#xff08;下&#xff09; 在JavaScript中&#xff0c;Reflect对象不仅提供了基础的对象操作方法&#xff08;如get、set等&#xff09;&#xff0c;还包含了许多高级API&#xff0c;用于更精细地控制对象行为。本文将继续…

【数字人开发】Unity+百度智能云平台实现长短文本个性化语音生成功能

一、创建自己的应用 百度智能云控制台网址&#xff1a;https://console.bce.baidu.com/ 1、创建应用 2、获取APIKey和SecretKey 3、Api调试 调试网址&#xff1a;https://console.bce.baidu.com/support/?timestamp1750317430400#/api?productAI&project%E8%AF%AD%E9%…

银河麒麟 | ubuntu 搭建属于自己的邮件服务器

目录 遇权不绝就转root 更新系统 安装 Postfix 配置 Postfix 重启 Postfix 安装 Dovecot 配置 Dovecot 编辑 Dovecot 的 IMAP 配置文件 编辑 Dovecot 的用户认证配置文件 编辑 Dovecot 的服务配置文件 重启 Dovecot 安装发送邮箱功能 发送邮件 测试 遇权不绝就转…

嵌入式通信协议框架的四层架构设计与实现

文章目录 一、硬件抽象层&#xff1a;数据收发的基石1.1 设计要点1.2 代码示例 二、协议管理层&#xff1a;智能路由中枢2.1 设计要点2.2 代码示例 三、协议处理层&#xff1a;协议具体实现3.1 设计要求3.2代码示例3.2.1 协议公共定义3.2.2 协议一设计3.2.3 协议二设计 四、应用…

RA信号处理

ra_snr_gui.m 作用&#xff1a;统计不同信噪比下&#xff0c;五种信号的峰值旁瓣比RA和低高频均值比RM&#xff0c;绘制结果&#xff0c;参考图3.11和3.12 DFCW_RA_SNR.m 作用&#xff1a;产生正交离散频率编码信号&#xff0c;并计算峰值旁瓣比RA和低高频均值比 RM LFM_RA_S…

【go的测试】单测之gomock包与gomonkey包

目录 使用gomock包 1. 安装mockgen 2. 定义接口 3. 生成mock文件 4. 在单测中使用mock的函数 5. gomock 包的使用问题 使用gomonkey包 1. mock 一个包函数 2. mock 一个公有成员函数 3. mock 一个私有成员函数 使用gomock包 1. 安装mockgen go get -u github.com/go…

html实现登录与注册功能案例(不写死且只使用js)

目录 案例需求 实现思路 代码参考 login.html register.html 运行效果 升级思路 案例需求 需要一个登录界面和注册页面实现一个较为完整的登录注册功能 1.登录界面没有登录限制需求&#xff08;降低难度&#xff09;&#xff0c;实现基本的登录判断需求&#xff0c;弹窗…

PHP is the best language.

PHP很好写。 众所周知Python很好写&#xff0c;Python 也能开发 Web 应用&#xff0c;但和 PHP 相比&#xff0c;在“直接处理网页”这件事上&#xff0c;PHP 更加贴近底层和原生。 想快速搭建原型或者 B 端后台工具&#xff0c;不妨用 PHP Laravel 来搞&#xff0c;真的很香…

Mybatis-Plus 在 getOne() 的时候要加上 .last(“limit 1“)

1.先写结论: 1.为了确保 SQL 查询只返回一条记录&#xff08;当查询返回多条时会报错->多为代码本身问题&#xff09;。 2.防止数据库执行全表扫描 3.参考网址&#xff1a;问题记录&#xff1a;MyBatis-Plus 中 ServiceImpl 类的 getOne_mybatis_无他&唯手熟尔-2048…

C语言:二分搜索函数

一、二分搜索基本概念 二分搜索&#xff08;Binary Search&#xff09;是一种在有序数组中查找特定元素的高效算法&#xff0c;时间复杂度为O(log n)。 基本特点&#xff1a; 仅适用于有序数组&#xff08;升序或降序&#xff09; 每次比较将搜索范围减半 比线性搜索(O(n))…

[前端AI]LangChain.js 和 Next.js LLM构建——协助博客撰写和总结助手

LangChain.js 和 Next.js LLM 后端应用于协助博客撰写和总结领域是一个非常实用的方向&#xff01;这涉及到理解和处理文本内容&#xff0c;并生成新的、有结构的信息。 根据您之前提供的代码和需求&#xff0c;我们可以在此基础上进行更具针对性的功能规划和技术实现。 博客…

用 GitHub Issues 做任务管理和任务 List,简单好用!

说实话&#xff0c;我平时也是一个人写代码&#xff0c;每次开完会整理任务最麻烦&#xff1a; 一堆事项堆在聊天里、文档里&#xff0c;或者散落在邮件里…… 为了理清这些&#xff0c;我通常会做一份 List&#xff0c;标好优先级&#xff0c;再安排到每日的工作里 虽然这个…

每日算法刷题Day35 6.22:leetcode枚举技巧枚举中间2道题,用时1h

枚举中间 对于三个或者四个变量的问题&#xff0c;枚举中间的变量往往更好算。 为什么&#xff1f;比如问题有三个下标&#xff0c;需要满足 0≤i<j<k<n&#xff0c;对比一下&#xff1a; 枚举 i&#xff0c;后续计算中还需保证 j<k。 枚举 j&#xff0c;那么 i 和…

【教学类-18-06】20250623蒙德里安黑白七款合并WORD(500张、无学号)

背景需要 客户买了蒙德里安黑白格子7种尺寸,但是不需要学号方块,并指定要WORD 设计思路 【教学类-18-05】20241118正方形手工纸(蒙德里安-风格派-红黄蓝黑白)-CSDN博客文章浏览阅读1.3k次,点赞29次,收藏18次。【教学类-18-05】20241118正方形手工纸(蒙德里安-风格派-红…

langchain--(4)

7 Embedding文本向量化 Embedding文本向量化是一种将非结构化文本转化为低维、连续数值向量的技术,旨在通过数学方式捕捉文本的语义、语法或特征信息,从而让机器更高效地处理语言任务。其核心思想源于流形假设(Manifold Hypothesis),即认为高维原始数据(如文本)实际隐含…

DMDRS部署实施手册(ORACLE=》DM)

DMDRS部署实施手册&#xff08;ORACLE》DM&#xff09; 1 同步说明2 DMDRS安装3 数据库准备3.1 源端准备3.1.1 开启归档日志和附加日志3.1.2 关闭回收站3.1.3 创建同步用户 3.2 目标准备3.2.1 创建同步用户 4 DMDRS配置4.1 源端配置4.2 目标配置 5 DMDRS启动5.1 启动源端服务5.…

十(1)作业:sqli-labs重点关卡

参考文章&#xff1a;详细sqli-labs&#xff08;1-65&#xff09;通关讲解-CSDN博客 第1关&#xff1a; 输入 &#xff1a; ?id3 输入 &#xff1a; ?id2 当输入的数字不同&#xff0c;页面的响应也不同&#xff0c;说明&#xff0c;输入的内容被带入到数据库里查询了 输…

Python 爬虫入门 Day 7 - 复盘 + 实战挑战日

Python 第二阶段 - 爬虫入门 &#x1f3af; 本周知识回顾 网络请求与网页结构基础 HTML解析入门&#xff08;使用 BeautifulSoup&#xff09; 实现爬虫多页抓取与翻页逻辑 模拟登录爬虫与 Session 维持 使用 XPath 进行网页解析&#xff08;lxml XPath&#xff09; 反爬虫应对…

WebRTC(七):媒体能力协商

目的 在 WebRTC 中&#xff0c;每个浏览器或终端支持的音视频编解码器、分辨率、码率、帧率等可能不同。媒体能力协商的目的就是&#xff1a; 确保双方能“听得懂”对方发的媒体流&#xff1b;明确谁发送、谁接收、怎么发送&#xff1b;保障连接的互操作性和兼容性。 P2P的基…

可信启动方案设计

安全之安全(security)博客目录导读 目录 一、引言 二、关键数据(Critical Data) 三、度量槽(Measurement Slot) 四、可信启动后端 1、事件日志(Event Log) 2、离散型 TPM(Discrete TPM) 3、RSE(运行时安全引擎) 五、平台接口 平台接口的职责: 1、函数:b…