LNMP就是Linux+Nginx+MySQL+PHP,Linux作为服务器的操作系统,MySQL即为数据库。本文主要介绍PHP和Nginx的关系。
Nginx为一款高性能Web服务器,本身是不能处理PHP的,当接收到请求时,判断如果是PHP请求就会将请求交给PHP解释器处理,然后将结果返回给Client。Nginx一般把请求转发给fast-cgi管理进程处理,fast-cgi管理进程再选择cgi子进程处理请求,然后把结果返给Nginx。
cd /usr/local/src //安装包存放路径 wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz //下载Mysql包 tar zxvf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz //解压 mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql //移动位置 cd /usr/local/mysql //进入mysql useradd mysql //创建用户 mkdir /data/ //创建数据存放位置 ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql //初始化 cp support-files/my-default.cnf /etc/my.cnf vi /etc/my.cnf //指定datadir和socket路径 cp support-files/mysql.server /etc/init.d/mysqld vi /etc/init.d/mysqld //定义basedir和datadir /etc/init.d/mysqld start //启动 ps -aux|grep mysqld //查看是否启动 chkconfig --add mysqld;chkconfig mysqld on //加入服务列表并添加至开机启动 service mysqld start //重启mysql服务 service mysqld stop //关闭mysql服务
cd /usr/local/src/ wget http://cn2.php.net/distributions/php-5.6.30.tar.gz tar zxf php-5.6.30.tar.gz useradd -s /sbin/nologin php-fpm cd php-5.6.30 ./configure --prefix=/usr/local/php-fpm --with-config-file-path=/usr/local/php-fpm/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-pdo-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --with-pear --with-curl --with-openssl 如果出现报错,根据提示安装所需要的软件包,直到配置完成。 make && make install /usr/local/php-fpm/sbin/php-fpm -m 查看加载的模块 /usr/local/php-fpm/sbin/php-fpm -i 查看php的相关信息 ## 修改配置文件 cp php.ini-production /usr/local/php-fpm/etc/php.ini ls /usr/local/php-fpm/etc/ vim /usr/local/php-fpm/etc/php-fpm.conf [global] 定义全局参数 pid = /usr/local/php-fpm/var/run/php-fpm.pid (pid路径) error_log = /usr/local/php-fpm/var/log/php-fpm.log (error_log路径) [www] 模块名 listen = /tmp/php-fcgi.sock 监听地址 listen.mode = 666 定义sock文件权限666 user = php-fpm 定义是那个用户 group = php-fpm 定义是那个组 pm = dynamic pm都是进程信息 pm.max_children = 50 pm.start_servers = 20 pm.min_spare_servers = 5 pm.max_spare_servers = 35 pm.max_requests = 500 rlimit_files = 1024 ## 保存配置文件后,检测配置是否正确: usr/local/php-fpm/etc/php-fpm -t 如果显示test is successful,则说明配置成功。 ## 启动php-fpm cp /usr/local/src/php-5.6.30/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm 拷贝启动脚本到etc下面 chmod 755 /etc/init.d/php-fpm 修改文件权限为755 chkconfig --add php-fpm 把php-fpm服务添加到服务列表去 chkconfig php-fpm on 设置开机自启动 service php-fpm start 开启php-fpm服务 ps aux |grep php-fpm\\检查php-fpm服务启动状态
Nginx不支持对外部程序的直接调用或者解析,所有的外部程序(包括PHP)必须通过Fast-CGI接口来调用。Fast-CGI接口在Linux下是socket(这个socket可以是文件socket,也可以是ip socket)。 wrapper:为了调用CGI程序,还需要一个Fast-CGI的wrapper(wrapper可以理解为用于启动另一个程序的程序),这个wrapper绑定在某个固定socket上,如端口或者文件socket。当Nginx将CGI请求发送给这个socket的时候,通过Fast-CGI接口,wrapper接收到请求,然后Fork(派生)出一个新的线程,这个线程调用解释器或者外部程序处理脚本并读取返回数据;接着,wrapper再将返回的数据通过Fast-CGI接口,沿着固定的socket传递给Nginx;最后,Nginx将返回的数据(html页面或者图片)发送给客户端。这就是Nginx+Fast-CGI的整个运作过程。
cd /usr/local/src/ wget http://nginx.org/download/nginx-1.12.1.tar.gz tar zxvf nginx-1.12.1.tar.gz cd nginx-1.12.1 ./configure --prefix=/usr/local/nginx make && make install ## 编写Nginx启动脚本,并加入系统服务: vim /etc/init.d/nginx #!/bin/bash # chkconfig: - 30 21 # description: http service. # Source Function Library . /etc/init.d/functions # Nginx Settings NGINX_SBIN="/usr/local/nginx/sbin/nginx" NGINX_CONF="/usr/local/nginx/conf/nginx.conf" NGINX_PID="/usr/local/nginx/logs/nginx.pid" RETVAL=0 prog="Nginx" start() { echo -n $"Starting $prog: " mkdir -p /dev/shm/nginx_temp daemon $NGINX_SBIN -c $NGINX_CONF RETVAL=$? echo return $RETVAL } stop() { echo -n $"Stopping $prog: " killproc -p $NGINX_PID $NGINX_SBIN -TERM rm -rf /dev/shm/nginx_temp RETVAL=$? echo return $RETVAL } reload() { echo -n $"Reloading $prog: " killproc -p $NGINX_PID $NGINX_SBIN -HUP RETVAL=$? echo return $RETVAL } restart() { stop start } configtest() { $NGINX_SBIN -c $NGINX_CONF -t return 0 } case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) restart ;; configtest) configtest ;; *) echo $"Usage: $0 {start|stop|reload|restart|configtest}" RETVAL=1 esac exit $RETVAL ## 保存脚本更改权限: chmod 755 /etc/init.d/nginx chkconfig --add nginx ; chkconfig nginx on //nginx加入开机启动 ## 更改Nginx的配置文件: > /usr/local/nginx/conf/nginx.conf\\重定向符号>,单独使用时,可以把一个文本文档快速清空。 编辑Nginx配置文件: vim /usr/local/nginx/conf/nginx.conf user nobody nobody; worker_processes 2; error_log /usr/local/nginx/logs/nginx_error.log crit; pid /usr/local/nginx/logs/nginx.pid; worker_rlimit_nofile 51200; events { use epoll; worker_connections 6000; } http { include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 3526; server_names_hash_max_size 4096; log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]' ' $host "$request_uri" $status' ' "$http_referer" "$http_user_agent"'; sendfile on; tcp_nopush on; keepalive_timeout 30; client_header_timeout 3m; client_body_timeout 3m; send_timeout 3m; connection_pool_size 256; client_header_buffer_size 1k; large_client_header_buffers 8 4k; request_pool_size 4k; output_buffers 4 32k; postpone_output 1460; client_max_body_size 10m; client_body_buffer_size 256k; client_body_temp_path /usr/local/nginx/client_body_temp; proxy_temp_path /usr/local/nginx/proxy_temp; fastcgi_temp_path /usr/local/nginx/fastcgi_temp; fastcgi_intercept_errors on; tcp_nodelay on; gzip on; gzip_min_length 1k; gzip_buffers 4 8k; gzip_comp_level 5; gzip_http_version 1.1; gzip_types text/plain application/x-javascript text/css text/htm application/xml; server { listen 80; server_name localhost; index index.html index.htm index.php; root /usr/local/nginx/html; location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/tmp/php-fcgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; } } } /usr/local/nginx/sbin/nginx -t 检查nginx语法错误 /etc/init.d/nginx start //启动nginx服务 ps aux |grep nginx 查看nginx进程 netstat -lntp 查看nginx端口 //80端口 echo "hello world">/usr/local/nginx/html/1.html\\curl测试 curl localhost/1 systemctl status nginx\\ 查看nginx状态