分享交流
合作共赢!

linux 安装nginx+mysql+php

一、nginx安装

正式开始前,编译环境gcc g++ 开发库之类的需要提前装好。

安装make:

yum -y install gcc automake autoconf libtool make

安装g++:

yum install gcc gcc-c++

一般我们都需要先装pcre, zlib,前者为了重写rewrite,后者为了gzip压缩。

1.选定源码目录

可以是任何目录,本文选定的是/usr/local/src

cd /usr/local/src

2.安装PCRE库

ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz 下载最新的 PCRE 源码包,使用下面命令下载编译和安装 PCRE 包:

cd /usr/local/src
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz
tar -zxvf pcre-8.39.tar.gz
cd pcre-8.39
./configure
make && make install

3.安装zlib库

http://zlib.net/zlib-1.2.11.tar.gz 下载最新的 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

4.安装ssl(某些vps默认没装ssl)

cd /usr/local/src
wget https://www.openssl.org/source/openssl-1.1.0b.tar.gz
tar -zxvf openssl-1.1.0b.tar.gz
./config
make && make install

5.安装nginx

以安装最新稳定版ngixn-1.10.2为例子,其他版本也一样。

下面是把 Nginx 安装到 /usr/local/nginx 目录下的详细步骤

 cd /usr/local/src
 wget http://nginx.org/download/nginx-1.10.2.tar.gz
 tar -zxvf nginx-1.10.2.tar.gz
 cd nginx-1.10.2

1、添加nginx用户和用户组

groupadd -r nginx
useradd -r -g nginx nginx

你可能收到提示已经有了nginx 用户和组这个无所谓有了更好。

2、配置nginx安装参数

  ./configure \
  --prefix=/usr/local/nginx \
  --sbin-path=/usr/local/nginx/sbin/nginx \
  --conf-path=/usr/local/nginx/nginx.conf \
  --pid-path=/usr/local/nginx/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.39 \
 --with-zlib=/usr/local/src/zlib-1.2.11 \
 --with-openssl=/usr/local/src/openssl-1.1.0b \

注:当前所在目录一定要是/usr/local/src/nginx-1.10.2

–with-pcre=/usr/local/src/pcre-8.39 指的是pcre-8.39 的源码路径。

–with-zlib=/usr/local/src/zlib-1.2.11 指的是zlib-1.2.11 的源码路径。

–with-openssl=/usr/local/src/openssl-1.1.0b 指的是openssl-1.1.0b 的源码路径。

编译安装

1 make && make install

安装成功后 /usr/local/nginx 目录下如下:

3、启动

确保系统的 80 端口没被其他程序占用,运行/usr/local/nginx/sbin/nginx 命令来启动 Nginx

 netstat -ano|grep 80
/usr/local/nginx/sbin/nginx

可能会出现mkdir /var/tmp/nginx/client failed的提示

这时手动创建就可以了 mkdir -p /var/tmp/nginx/client就可以了

打开浏览器访问此机器的 IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功

ps:

若nginx成功运行后,通过ip地址无法访问服务器 则可能是防火墙的问题 需要开放80端口或关闭防火墙

开启80端口 
firewall-cmd --zone=public --add-port=80/tcp --permanent 
命令含义: 
--zone #作用域 
--add-port=80/tcp #添加端口,格式为:端口/通讯协议 
--permanent #永久生效,没有此参数重启后失效 
重启防火墙 
firewall-cmd --reload

或者永久关闭防火墙
systemctl stop firewalld.service  //停止防火墙
systemctl disable firewalld.service //不再开机停止运行防火墙

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".

二、php-fpm安装配置

nginx本身不能处理PHP,它只是个web服务器,当接收到请求后,如果是php请求,则发给php解释器处理,并把结果返回给客户端。

nginx一般是把请求发fastcgi管理进程处理,fascgi管理进程选择cgi子进程处理结果并返回被nginx

本文以php-fpm为例介绍如何使nginx支持PHP

一、编译安装php-fpm

什么是PHP-FPM

PHP-FPM是一个PHP FastCGI管理器,是只用于PHP的,可以在 http://php-fpm.org/download下载得到.

PHP-FPM其实是PHP源代码的一个补丁,旨在将FastCGI进程管理整合进PHP包中。必须将它patch到你的PHP源代码中,在编译安装PHP后才可以使用。

新版PHP已经集成php-fpm了,不再是第三方的包了,推荐使用。PHP-FPM提供了更好的PHP进程管理方式,可以有效控制内存和进程、可以平滑重载PHP配置,比spawn-fcgi具有更多优点,所以被PHP官方收录了。在./configure的时候带 –enable-fpm参数即可开启PHP-FPM,其它参数都是配置php的,具体选项含义可以查看这里

安装前准备

添加 epel 源

# rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

安装依赖

# yum install gcc bison bison-devel zlib-devel libmcrypt-devel mcrypt mhash-devel openssl-devel libxml2-devel libcurl-devel bzip2-devel readline-devel libedit-devel sqlite-devel

 

1 yum -y install gcc gcc-c++ glibc
yum -y install libmcrypt-devel mhash-devel libxslt-devel \
libjpeg libjpeg-devel libpng libpng-devel 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
 cd /usr/local/src
 wget http://au1.php.net/get/php-7.1.10.tar.gz/from/this/mirror
 tar -zvxf php-7.1.10.tar.gz
 cd php-7.1.10
./configure --prefix=/usr/local/php --enable-fpm --with-mcrypt \
--enable-mbstring --enable-pdo --with-curl --disable-debug  --disable-rpath \
--enable-inline-optimization --with-bz2  --with-zlib --enable-sockets \
--enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex \
--with-mhash --enable-zip --with-pcre-regex --with-pdo-mysql --with-mysqli \
--with-gd --with-jpeg-dir --with-freetype-dir --enable-calendar
make && make install

以上就完成了php-fpm的安装

下面是对php-fpm运行用户进行设置

1、为php提供配置文件

1 cp php.ini-production /usr/local/php/etc/php.ini

2、为php-fpm提供配置文件

1 cd /usr/local/php
2 cp etc/php-fpm.conf.default etc/php-fpm.conf
3 vim etc/php-fpm.conf

修改
user = www
group = www

如果www用户不存在,那么先添加www用户

1 groupadd www
2 useradd -g www www
修改
pm.max_children = 150
pm.start_servers = 8
pm.min_spare_servers = 5
pm.max_spare_servers = 10
pid = /usr/local/php/var/run/php-fpm.pid
配置文件
# cp php.ini-production /usr/local/php/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
# cp -R ./sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
需要注意的是php7中www.conf这个配置文件配置phpfpm的端口号等信息,如果你修改默认的9000端口号需在这里改,再改nginx的配置

重启

# killall php-fpm

#  service php-fpm restart

 

使用如下命令来验正(如果此命令输出有中几个php-fpm进程就说明启动成功了):
1 ps aux | grep php-fpm

3、nginx和php-fpm整合

编辑/usr/local/nginx/nginx.conf

1 vim /usr/local/nginx/nginx.conf
user www www;
worker_processes auto;

#error_log /usr/local/wwwlogs/error_nginx.log crit;
pid /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 51200;

events {
  use epoll;
  worker_connections 51200;
  multi_accept on;
}

http {
  include mime.types;
  default_type application/octet-stream;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 1024m;
  sendfile on;
  tcp_nopush on;
  keepalive_timeout 120;
  server_tokens off;
  tcp_nodelay on;

  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 128k;

  #Gzip Compression
  gzip on;
  gzip_buffers 16 8k;
  gzip_http_version 1.1;
  gzip_min_length 256;
  gzip_proxied any;
  gzip_vary on;
  gzip_types
  text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
  text/javascript application/javascript application/x-javascript
  text/x-json application/json application/x-web-app-manifest+json
  text/css text/plain text/x-component
  font/opentype application/x-font-ttf application/vnd.ms-fontobject
  image/x-icon;
  gzip_disable "MSIE [1-6]\.(?!.*SV1)";

  #If you have a lot of static files to serve through Nginx then caching of the files' metadata (not the actual files' contents) can save some latency.
  open_file_cache max=1000 inactive=20s;
  open_file_cache_valid 30s;
  open_file_cache_min_uses 2;
  open_file_cache_errors on;

  ######################## default ############################
  server {
    listen 80;
    server_name _;
    #access_log /usr/local/wwwlogs/access_nginx.log combined;
    root /home/www/default;
    index index.html index.htm index.php;
    location /nginx_status {
      stub_status on;
      access_log off;
      allow 127.0.0.1;
      deny all;
    }

    location ~ [^/]\.php(/|$) {
      #fastcgi_pass remote_php_ip:9000;
      fastcgi_pass unix:/dev/shm/php-cgi.sock;
      fastcgi_index index.php;
      include fastcgi.conf;
    }

    #location ~ \.php$ {
      # root /home/www;
      # fastcgi_pass 127.0.0.1:9000;
      # fastcgi_index index.php;
      # fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      # include fastcgi_params;
    #}

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
      expires 30d;
      access_log off;
    }

    location ~ .*\.(js|css)?$ {
      expires 7d;
      access_log off;
    }
  }

  ########################## vhost #############################
  include vhost/*.conf;
}

项目路径

1 mkdir /usr/local/nginx/vhost
2 touch www.qyi.com.conf
3 vim www.qyi.com.conf
server {
  charset utf-8;
  client_max_body_size 200M;

  listen 80;
  #listen [::]:80 default_server ipv6only=on; ## listen for ipv6

  listen 443 ssl;
  # server_name localhost;
  # ssl on;
  ssl_certificate /usr/local/ssl/beta-v2.crt;
  ssl_certificate_key /usr/local/ssl/beta-v2.key;
  ssl_session_timeout 5m;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_ciphers ALL:!DH:!EXPORT:!RC4:+HIGH:+MEDIUM:-LOW:!aNULL:!eNULL;
  ssl_prefer_server_ciphers on;

  server_name www.qyi.com;
  root /home/www/www.qyi.com;

  #access_log /home/www/www.qyi.com/advanced.access.log main buffer=50k;
  error_log /usr/local/nginx/logs/bella.error.log warn;

  location / {
    root /home/www/www.qyi.com/frontend/web;

    try_files $uri /frontend/web/index.php?$args;

    # avoiding processing of calls to non-existing static files by Yii
    location ~ \.(js|css|ttf|woff|woff2|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
      access_log off;
      expires 360d;

      try_files $uri =404;
    }
  }

  location /admin {
    alias /home/www/www.qyi.com/backend/web;

    rewrite ^(/admin)/$ $1 permanent;
    try_files $uri /backend/web/index.php?$args;
  }

  # avoiding processing of calls to non-existing static files by Yii
  location ~ ^/admin/(.+\.(js|css|ttf|woff|woff2|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar))$ {
    access_log off;
    expires 360d;

    rewrite ^/admin/(.+)$ /backend/web/$1 break;
    rewrite ^/admin/(.+)/(.+)$ /backend/web/$1/$2 break;
    try_files $uri =404;
  }

  location /v1 {
    alias /home/www/www.qyi.com/api/v1/web;
    rewrite ^(/v1)/$ $1 permanent;
    try_files $uri /api/v1/web/index.php?$args;
  }

  location /v2 {
    alias /home/www/www.qyi.com/api/v2/web;
    rewrite ^(/v2)/$ $1 permanent;
    try_files $uri /api/v2/web/index.php?$args;
  }

  location ~ \.php$ {
    include fastcgi_params;
    # check your /etc/php5/fpm/pool.d/www.conf to see if PHP-FPM is listening on a socket or port
    #fastcgi_pass unix:/var/run/php5-fpm.sock; ## listen for socket
    #fastcgi_pass 127.0.0.1:9000; ## listen for port
    #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    #try_files $uri =404;
    fastcgi_pass unix:/dev/shm/php-cgi.sock;
    fastcgi_index index.php;
    proxy_set_header X-Real-IP $remote_addr; 
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    include fastcgi.conf;
  }

  #error_page 404 /404.html;

  location = /requirements.php {
    deny all;
  }
  location = /palmobo.net.html{
    try_files $uri =404;
  }

  location ~ \.(ht|svn|git) {
    deny all;
  }
}

phpmyadmin路径

1 touch www.mysql.com.conf
2 vim www.mysql.com.conf
server {
  listen 80;
  server_name www.mysql.com;
  access_log off;
  index index.html index.htm index.php;
  #include /usr/local/nginx/vhost/www.mysql.com.conf;
  root /home/www/www.mysql.com;

  location ~ [^/]\.php(/|$) {
    #fastcgi_pass remote_php_ip:9000;
    fastcgi_pass unix:/dev/shm/php-cgi.sock;
    fastcgi_index index.php;
    include fastcgi.conf;
  }

  location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
    expires 30d;
    access_log off;
  }
  location ~ .*\.(js|css)?$ {
    expires 7d;
    access_log off;
  }
}

修改如下:

红框中“#”号去掉

红框中“#”号去掉

添加红框中内容

改成如下

重新载入nginx的配置文件:

1 /usr/loca/nginx/sbin/nginx -s reload

4、测试php文件

在/usr/local/nginx/html下创建index.php文件,输入如下内容

三、安装Mysql5.6.37(二进制安装)

下载地址:

https://dev.mysql.com/downloads/mysql/5.6.html

1.下载并上传到服务器中,进行解压,解压到/usr/local下

[root@zhang ~]# tar xvf mysql-5.6.37-linux-glibc2.12-x86_64.tar.gz -C /usr/local/

改下文件名,这个太长了

[root@zhang local ]# mv  mysql-5.6.37-linux-glibc2.12-x86_64 mysql

2.创建mysql的用户和组

创建组:

[root@zhang ~]# groupadd -r -g 306 mysql

创建用户:

[root@zhang ~]# useradd -g 306  -r  -u 306 mysql

3.修改目录的所有者和所属组:

[root@zhang ~]# chown -R mysql.mysql /usr/local/mysql/* 

创建存储数据文件的目录(如果有的话就不用创建了)

[root@zhang ~]# mkdir -p /usr/local/mysql/mydata/data

修改所有者和所属组

[root@zhang ~]# chown -R mysql.mysql /usr/local/mysql/mydata

ll mydata

4.初始化MySQL:

[root@zhang mysql]# scripts/mysql_install_db –user=mysql –datadir=/usr/local/mysql/mydata/data/ 

5.创建初始化进程,并添加到/etc/init.d/下

[root@zhang mysql]# cp support-files/mysql.server /etc/init.d/mysqld

[root@zhang mysql]# chkconfig –add mysqld  

6.配置mysql的配置文件

[root@zhang mysql]# vim /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/mydata/data
#socket=/var/lib/mysql/mysql.sock
#user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
#symbolic-links=0
basedir=/usr/local/mysql
port=3306
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 

#[mysqld_safe]
#log-error=/var/log/mysqld.log
#pid-file=/var/run/mysqld/mysqld.pid

7.启动mysql进程

[root@zhang mysql]# service mysqld start

8.查看进程是否启动:

[root@zhang mysql]# lsof -i:3306

COMMAND   PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
mysqld  26961 mysql   10u  IPv6  67261      0t0  TCP *:mysql (LISTEN)

9.配置环境变量:

[root@zhang ~]# vim .bash_profile

修改下PATH变量

PATH=$PATH:$HOME/bin:/usr/local/mysql/bin

export PATH

10.给mysql添加密码

[root@zhang ~]# mysqladmin -u root password ‘zhang’

11.登录mysql

[root@zhang ~]# mysql -uroot -p
Enter password: zhang
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.37 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.02 sec)

基本就可以使用了,这样mysql就彻底配置完成了

此内容查看价格0.1立即购买
赞(0) 打赏
未经允许不得转载:琼杰笔记 » linux 安装nginx+mysql+php

评论 抢沙发

评论前必须登录!

 

分享交流,合作共赢!

联系我们加入QQ群

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏

登录

找回密码

注册