您的位置:首页 > 运维架构 > Nginx

centOS7 安装nginx+php+mysql

2017-06-09 15:22 686 查看
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 包:

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


3.安装zlib库
http://zlib.net/zlib-1.2.8.tar.gz 下载最新的 zlib 源码包,使用下面命令下载编译和安装 zlib包:

1 cd /usr/local/src
2 wget http://zlib.net/zlib-1.2.11.tar.gz 3 tar -zxvf zlib-1.2.11.tar.gz
4 cd zlib-1.2.8
5 ./configure
6 make && make install


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

1 cd /usr/local/src
2 wget https://www.openssl.org/source/openssl-1.1.0b.tar.gz 3 tar -zxvf openssl-1.1.0b.tar.gz


5.安装nginx

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

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

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


1、添加nginx用户和用户组

1 groupadd -r nginx
2 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


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

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

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



3、启动

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

重启(首次启动需:/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf)
        /usr/local/nginx/sbin/nginx -s reload

1 netstat -ano|grep 80


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

nginx编译选项

nginx编译选项

make是用来编译的,它从Makefile中读取指令,然后编译。

make install是用来安装的,它也从Makefile中读取指令,安装到指定的位置。

configure命令是用来检测你的安装平台的目标特征的。它定义了系统的各个方面,包括nginx的被允许使用的连接处理的方法,比如它会检测你是不是有CC或GCC,并不是需要CC或GCC,它是个shell脚本,执行结束时,它会创建一个Makefile文件。nginx的configure命令支持以下参数:

--prefix=
path
    定义一个目录,存放服务器上的文件 ,也就是nginx的安装目录。默认使用 [code]/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。
[code]--group=
name
  设置nginx工作进程的用户组。安装完成后,可以随时更改的名称在nginx.conf配置文件中 使用的 user指令。默认的为非特权用户。
[code]--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 模块中。
[code]--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 。
[code]--with-cc-opt=
parameters
 — 设置额外的参数将被添加到CFLAGS变量。例如,当你在FreeBSD上使用PCRE库时需要使用:[code]--with-cc-opt="-I /usr/local/include。
.如需要需要增加 
select()支持的文件数量
:
--with-cc-opt="-D FD_SETSIZE=2048".

--with-ld-opt=
parameters
 —设置附加的参数,将用于在链接期间。例如,当在FreeBSD下使用该系统的PCRE库,应指定:[code]--with-ld-opt="-L /usr/local/lib".



php安装

1.下载php7
wget -O php7.tar.gz http://cn2.php.net/get/php-7.0.4.tar.gz/from/this/mirror[/code] 
2.解压php7
tar -xvf php7.tar.gz


3.进入php目录
cd php-7.0.4


4.安装依赖包
# 直接复制下面一行(不包括本行)
yum install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel


**5.编译配置(如果出现错误,基本都是上一步的依赖文件没有安装所致)  嫌麻烦的可以从这一步起参考PHP官方安装说明:http://php.net/manual/zh/install.unix.nginx.php**
./configure \
--prefix=/usr/local/php \
--with-config-file-path=/etc \
--enable-fpm \
--with-fpm-user=nginx  \
--with-fpm-group=nginx \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared  \
--enable-soap \
--with-libxml-dir \
--with-xmlrpc \
--with-openssl \
--with-mcrypt \
--with-mhash \
--with-pcre-regex \
--with-sqlite3 \
--with-zlib \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--with-cdb \
--enable-dom \
--enable-exif \
--enable-fileinfo \
--enable-filter \
--with-pcre-dir \
--enable-ftp \
--with-gd \
--with-openssl-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib-dir  \
--with-freetype-dir \
--enable-gd-native-ttf \
--enable-gd-jis-conv \
--with-gettext \
--with-gmp \
--with-mhash \
--enable-json \
--enable-mbstring \
--enable-mbregex \
--enable-mbregex-backtrack \
--with-libmbfl \
--with-onig \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-zlib-dir \
--with-pdo-sqlite \
--with-readline \
--enable-session \
--enable-shmop \
--enable-simplexml \
--enable-sockets  \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-wddx \
--with-libxml-dir \
--with-xsl \
--enable-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-fastcgi \
--enable-opcache


如果不添加–enable-fastcgi会造成PHP无法添加新的模块

如果报错--错误描述:

#yum install libmcrypt libmcrypt-devel mcrypt mhash

Setting up Install Process

No package php-mcrypt available.

Error: Nothing to do

我们会看到centos yum从仓库中根本找不到这几个包。但我不想使用源码编译就想使用yum安装,怎么办?

解决方法:

yum  install epel-release  //扩展包更新包

yum  update //更新yum源

yum install libmcrypt libmcrypt-devel mcrypt mhash  就ok了

如果再报错:

make: *** No targets specified and no makefile found. Stop.

重新“编译配置”

6.正式安装
make && make install


7.配置环境变量
vim /etc/profile


在末尾追加
export PATH=/usr/local/php/bin:$PATH


执行命令使得改动立即生效
source /etc/profile


8.配置php-fpm  需要在安装软件包目录
cp php.ini-production /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 sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm


启动php-fpm
/etc/init.d/php-fpm start

[/code]Starting php-fpm  done

[align=left]使用如下命令来验正(如果此命令输出有中几个php-fpm进程就说明启动成功了):[/align]

1 ps aux | grep php-fpm


9、nginx和php-fpm整合

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

1 vim /usr/local/nginx/nginx.conf


修改如下:

红框中“#”号去掉



红框中“#”号去掉



添加红框中内容



改成如下



重新载入nginx的配置文件:

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


4、测试php文件

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

1 <?php
2     phpinfo();
3 ?>


5、浏览器访问

访问http://你的服务器ip/index.php,皆可以见到php信息了。



安装Mysql5.7(解压缩版)

#### 1、下载安装包 

wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.16-linux-glibc2.5-x86_64.tar
推荐下载通用安装方法的TAR包

2.检查库文件是否存在,如有删除。

[root@localhost Desktop]$ rpm -qa | grep mysql
mysql-libs-5.1.52-1.el6_0.1.x86_64
[root@localhost ~]$ rpm -e mysql-libs-5.1.52.x86_64 --nodeps
[root@localhost ~]$

3.检查mysql组和用户是否存在,如无创建。

[root@localhost ~]$ cat /etc/group | grep mysql
mysql:x:490:
[root@localhost ~]$ cat /etc/passwd | grep mysql
mysql:x:496:490::/home/mysql:/bin/bash

以上为默认存在的情况,如无,执行添加命令:

[root@localhost ~]$groupadd mysql
[root@localhost ~]$useradd -r -g mysql mysql
//useradd -r参数表示mysql用户是系统用户,不可用于登录系统。

4.解压TAR包,更改所属的组和用户

[root@localhost ~]$ cd /usr/local/
[root@localhost local]$ tar xvf mysql-5.7.16-linux-glibc2.5-x86_64.tar
[root@localhost local]$ ls -l
total 1306432
-rwxr--r--. 1 root root  668866560 Jun  1 15:07 mysql-5.7.16-linux-glibc2.5-x86_64.tar
-rw-r--r--. 1 7161 wheel 638960236 Mar 28 12:54 mysql-5.7.16-linux-glibc2.5-x86_64.tar.gz
-rw-r--r--. 1 7161 wheel  29903372 Mar 28 12:48 mysql-test-5.7.12-linux-glibc2.5-x86_64.tar.gz
[root@localhost local]$ tar xvfz mysql-5.7.16-linux-glibc2.5-x86_64.tar.gz
[root@localhost local]$ mv mysql-5.7.16-linux-glibc2.5-x86_64 mysql
[root@localhost local]$ ls -l
total 1306436
drwxr-xr-x. 2 root root       4096 Dec  4  2009 bin
drwxr-xr-x. 2 root root       4096 Dec  4  2009 etc
drwxr-xr-x. 2 root root       4096 Dec  4  2009 games
drwxr-xr-x. 2 root root       4096 Dec  4  2009 include
drwxr-xr-x. 2 root root       4096 Dec  4  2009 lib
drwxr-xr-x. 3 root root       4096 Dec  2 14:36 lib64
drwxr-xr-x. 2 root root       4096 Dec  4  2009 libexec
drwxr-xr-x. 9 7161 wheel      4096 Mar 28 12:51 mysql
-rwxr--r--. 1 root root  668866560 Jun  1 15:07 mysql-5.7.12-linux-glibc2.5-x86_64.tar
-rw-r--r--. 1 7161 wheel 638960236 Mar 28 12:54 mysql-5.7.12-linux-glibc2.5-x86_64.tar.gz
-rw-r--r--. 1 7161 wheel  29903372 Mar 28 12:48 mysql-test-5.7.12-linux-glibc2.5-x86_64.tar.gz
drwxr-xr-x. 2 root root       4096 Dec  4  2009 sbin
drwxr-xr-x. 6 root root       4096 Dec  2 14:36 share
drwxr-xr-x. 2 root root       4096 Dec  4  2009 src
[root@localhost local]$ chown -R mysql mysql/
[root@localhost local]$ chgrp -R mysql mysql/
[root@localhost local]$ cd mysql/

5.安装和初始化数据库

[root@localhost mysql]$ bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
2016-06-01 15:23:25 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2016-06-01 15:23:30 [WARNING] The bootstrap log isn't empty:
2016-06-01 15:23:30 [WARNING] 2016-06-01T22:23:25.491840Z 0 [Warning] --bootstrap is deprecated. Please consider using --initialize instead
2016-06-01T22:23:25.492256Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000)
2016-06-01T22:23:25.492260Z 0 [Warning] Changed limits: table_open_cache: 431 (requested 2000)


如果改变默认安装路径,则需要  1)/etc/my.cnf、/etc/init.d/mysqld中修改  basedir=’/apps/mysql’  datadir=’/apps/mysql/data’  2)创建ln  mkdir -p /usr/local/mysql/bin  ln -s /apps/mysql/bin/mysqld /usr/local/mysql/bin/mysqld

[root@localhost mysql]$

[root@localhost mysql]$ cp -a ./support-files/my-default.cnf /etc/my.cnf
[root@localhost mysql]$ cp -a ./support-files/mysql.server  /etc/init.d/mysqld

[root@localhost mysql]$ cd bin/
[root@localhost bin]# ./mysqld_safe --user=mysql &
[1] 2932
[root@localhost bin]# 2016-06-01T22:27:09.708557Z mysqld_safe Logging to '/usr/local/mysql/data/localhost.localdomain.err'.
2016-06-01T22:27:09.854913Z mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data

[root@localhost bin]# /etc/init.d/mysqld restart
Shutting down MySQL..2016-06-01T22:27:50.498694Z mysqld_safe mysqld from pid file /usr/local/mysql/data/localhost.localdomain.pid ended
SUCCESS!
Starting MySQL. SUCCESS!
[1]+  Done                    ./mysqld_safe --user=mysql
[root@localhost bin]$
//设置开机启动
[root@localhost bin]$ chkconfig --level 35 mysqld on
[root@localhost bin]$

6.初始化密码

mysql5.7会生成一个初始化密码,而在之前的版本首次登陆不需要登录。

[root@localhost bin]$ cat /root/.mysql_secret
# Password set for user 'root@localhost' at 2016-06-01 15:23:25
,xxxxxR5H9
[root@localhost bin]$./mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.12

Copyright (c) 2000, 2016, 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> SET PASSWORD = PASSWORD('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

7.添加远程访问权限

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set host = '%' where user = 'root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select host, user from user;
+-----------+-----------+
| host      | user      |
+-----------+-----------+
| %         | root      |
| localhost | mysql.sys |
+-----------+-----------+
//重启生效
/etc/init.d/mysqld restart

登陆/usr/local/mysql/bin/mysql -uroot -p

转至:http://www.cnblogs.com/flywind/p/6019631.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: