centos 搭建 lnmp

来源:互联网 发布:eve有mac版吗 编辑:程序博客网 时间:2024/06/11 18:02

Centos7手工配置lnmp环境

本文都是按顺序执行命令,没有截图。注:相关软件可以在各个官网下载。


首先确定系统版本

cat /etc/redhat-release

一、配置防火墙,开启80端口、3306端口
CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙。
其实firewall的是另外一个防火墙,但是这里我们用更熟悉的iptables防火墙了,后面慢慢研究firewall功能。
1、关闭firewall:

systemctl stop firewalld.service          #停止firewall
systemctl disable firewalld.service        #禁止firewall开机启动

2、安装iptables防火墙

yum install iptables-services -y          #安装
vi /etc/sysconfig/iptables              #编辑防火墙配置文件

可以看到目前只开放了22端口。下面我们开放80和3306端口
以下规则中请注意80和3306处。

# sample configuration for iptables service# you can edit this manually or use system-config-firewall# please do not ask us to add additional ports/services to this default configuration*filter:INPUT ACCEPT [0:0]:FORWARD ACCEPT [0:0]:OUTPUT ACCEPT [0:0]-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT-A INPUT -p icmp -j ACCEPT-A INPUT -i lo -j ACCEPT-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT-A INPUT -j REJECT --reject-with icmp-host-prohibited-A FORWARD -j REJECT --reject-with icmp-host-prohibitedCOMMIT
`:wq!`           #保存退出
systemctl restart iptables.service        #最后重启防火墙使配置生效
systemctl enable iptables.service         #设置防火墙开机启动

安装nginx

1 下载pcre,是Nginx的重写Rewrite,zlib为了网页Gzip压缩,openssl加密传输
在用户目录下建立安装文件夹

#mkdir install# cd /home/tiger/install#下载依赖文件pcre,openssl,zlibwget  -c   http://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.38.tar.bz2wget  -c https://www.openssl.org/source/openssl-1.0.2j.tar.gzwget -c http://zlib.net/zlib-1.2.8.tar.gz#下载nginxwget -c http://nginx.org/download/nginx-1.10.2.tar.gz

解压文件

tar -zxvf nginx-1.10.2.tar.gztar -jxvf pcre-8.38.tar.bz2tar -zxvf zlib-1.2.8.tar.gztar -zxvf openssl-1.0.2j.tar.gz

1、安装pcre

    ​[root@localhost src]# cd pcre-8.38    ​[root@localhost pcre-8.38]# ./configure --prefix=/usr/local/pcre    ​[root@localhost pcre-8.38]# make    ​[root@localhost pcre-8.38]# make install

2 安装zlib

​[root@localhost src]# cd zlib-1.2.8​[root@localhost zlib-1.2.8]# ./configure --prefix=/usr/local/zlib​[root@localhost zlib-1.2.8]# make && make install​

3、安装openssl

​[root@localhost src]# cd openssl-1.0.2j​[root@localhost openssl-1.0.1t]# ./config --prefix=/usr/local/openssl​[root@localhost openssl-1.0.1t]# make && make install​[root@localhost openssl-1.0.1t]# export PATH=$PATH:/usr/local/openssl/bin​[root@localhost openssl-1.0.1t]# source /etc/profile

这里安装是用config不是configure哦~~编译相对比上面久一点
4、安装gd库

[root@localhost src]# yum -y install gd-devel​

5、安装Nginx

[root@localhost nginx-1.10.1]# groupadd​ nginx​[root@localhost nginx-1.10.1]# useradd -g nginx nginx -s /bin/false[root@localhost nginx-1.10.1]# tar -zxf nginx-1.10.1.tar.gz​[root@localhost nginx-1.10.1]# cd nginx-1.10.1[root@localhost nginx-1.10.1]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_sub_module --with-http_gzip_static_module --with-http_stub_status_module --with-pcre=/home/tiget/install/pcre-8.38 --with-zlib=/home/tiget/install/zlib-1.2.8 --with-openssl=/home/tiget/install/openssl-1.0.2j[root@localhost nginx-1.10.1]# ​make && make install

小技巧:

   ./configure --help #可以查看编译选项,如上面的--with-http_ssl_module​

① 修改配置文件

​[root@localhost nginx-1.6.2]# vi /usr/local/nginx/conf/nginx.conf

将http -> server -> server_name改为服务器的外网ip地址,或你的网站域名​

② 启动Nginx

[root@localhost nginx-1.6.2]# /usr/local/nginx/sbin/nginx

Nginx安装完毕,这个是在虚拟机上面安装的,ip地址是192.168.1.202,这是我们用浏览器访问192.168.1.202,访问失败,什么原因,你应该很快想到是防火墙

③ 开机自启动​​

方法一:在/etc/rc.d/rc.local文件里面增加一行,如下

/usr/local/nginx/sbin/nginx

方法二:将nginx加入服务

在/etc/​rc.d/init.d/下面增加一个脚本,名称 nginx,脚本内容查看这里​

④ 防火墙配置

Centos6】​

1、要么关闭防火墙 [root@localhost nginx-1.8.1]# service iptables stop

2、要么配置防火墙

[root@localhost202 nginx-1.8.1]# vim /etc/sysconfig/iptables

​在里面添加一句-A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT

保存退出,

[root@localhost202 nginx-1.8.1]#service iptables restart

重启防火墙使配置生效​​

​Centos7】

​systemctl stop firewalld.service //停止firewall

systemctl disable firewalld.service //禁止firewall开机启动

​yum -y install iptables-services​

[root@localhost202 nginx-1.8.1]# vim /etc/sysconfig/iptables

​在里面添加一句-A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT

​systemctl restart iptables.service #重启防火墙使配置生效

systemctl enable iptables.service #设置防火墙开机启动

⑤加入环境变量

​​[root@localhost ~]# vim /etc/profile​ # 在文件末尾增加如下内容

​#add by tomener ~ php mysql nginx openssl

​export PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/lib:/usr/local/openssl/bin:/usr/local/nginx/sbin:/usr/local/php/bin
​​[root@localhost ~]# source /etc/profile

安装mysql

-

(1)配置YUM源

# 下载mysql源安装包# wget http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm# 安装mysql源# yum localinstall mysql57-community-release-el7-11.noarch.rpm

检查mysql源是否安装成功

# yum repolist enabled | grep "mysql.*-community.*"

可以修改
#vim /etc/yum.repos.d/mysql-community.repo
改变默认安装的mysql版本。比如要安装5.6版本,将5.7源的enabled=1改成enabled=0。然后再将5.6源的enabled=0改成enabled=1即可。

安装MySQL server

#yum install mysql-community-server

1 启动MySQL服务

#systemctl start mysqld

2查看MySQL的启动状态

systemctl status mysqld● mysqld.service - MySQL Server   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)   Active: active (running) since 五 2016-06-24 04:37:37 CST; 35min ago Main PID: 2888 (mysqld)   CGroup: /system.slice/mysqld.service           └─2888 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid624 04:37:36 localhost.localdomain systemd[1]: Starting MySQL Server...624 04:37:37 localhost.localdomain systemd[1]: Started MySQL Server.

3开机启动

# systemctl enable mysqld# systemctl daemon-reload

修改root本地登录密码
4、开机启动

# systemctl enable mysqld# systemctl daemon-reload

5、修改root默认密码

mysql安装完成之后,在/var/log/mysqld.log文件中给root生成了一个默认密码。通过下面的方式找到root默认密码,然后登录mysql进行修改:

# grep 'temporary password' /var/log/mysqld.log# mysql -uroot -pmysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!'; 

或者

mysql> set password for 'root'@'localhost'=password('MyNewPass4!'); 

注意:mysql5.7默认安装了密码安全检查插件(validate_password),默认密码检查策略要求密码必须包含:大小写字母、数字和特殊符号,并且长度不能少于8位。否则会提示ERROR 1819 (HY000): Your password does not satisfy the current policy requirements错误,如下图所示:

通过msyql环境变量可以查看密码策略的相关信息:

mysql> show variables like '%password%';

validate_password_policy:密码策略,默认为MEDIUM策略 validate_password_dictionary_file:密码策略文件,策略为STRONG才需要 validate_password_length:密码最少长度 validate_password_mixed_case_count:大小写字符长度,至少1个 validate_password_number_count :数字至少1个 validate_password_special_char_count:特殊字符至少1个 上述参数是默认策略MEDIUM的密码检查规则。

共有以下几种密码策略:

策略 检查规则
0 or LOW Length
1 or MEDIUM Length; numeric, lowercase/uppercase, and special characters
2 or STRONG Length; numeric, lowercase/uppercase, and special characters; dictionary file
MySQL官网密码策略详细说明:http://dev.mysql.com/doc/refman/5.7/en/validate-password-options-variables.html#sysvar_validate_password_policy

修改密码策略

在/etc/my.cnf文件添加validate_password_policy配置,指定密码策略

# 选择0(LOW),1(MEDIUM),2(STRONG)其中一种,选择2需要提供密码字典文件

validate_password_policy=0
如果不需要密码策略,添加my.cnf文件中添加如下配置禁用即可:

validate_password = off
重新启动mysql服务使配置生效:

systemctl restart mysqld
6、添加远程登录用户

默认只允许root帐户在本地登录,如果要在其它机器上连接mysql,必须修改root允许远程连接,或者添加一个允许远程连接的帐户,为了安全起见,我添加一个新的帐户:

mysql> GRANT ALL PRIVILEGES ON . TO ‘yangxin’@’%’ IDENTIFIED BY ‘Yangxin0917!’ WITH GRANT OPTION;
7、配置默认编码为utf8

修改/etc/my.cnf配置文件,在[mysqld]下添加编码配置,如下所示:

[mysqld]
character_set_server=utf8
init_connect=’SET NAMES utf8’
重新启动mysql服务,查看数据库默认编码如下所示:

默认配置文件路径: 配置文件:/etc/my.cnf 日志文件:/var/log//var/log/mysqld.log 服务启动脚本:/usr/lib/systemd/system/mysqld.service socket文件:/var/run/mysqld/mysqld.pid

安装php

四、编译安装php7.0.0

1.下载php7源码包

# cd /root & wget -O php7.tar.gz http://cn2.php.net/get/php-7.0.1.tar.gz/from/this/mirror

2.解压源码包

# tar -xvf php7.tar.gz

3.进入目录

# cd php-7.0.1

4.安装php依赖包 

# 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.编译配置,这里如果上一步的某些依赖包没有安装好,就会遇到很多configure error,我们一一解决,安装上相关软件开发包就可以

# ./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-opcache
这里写代码片

1.configure: error: xml2-config not found. Please check your libxml2 installation.

解决:

# yum install libxml2 libxml2-devel

2.configure: error: Cannot find OpenSSL’s

# yum install bzip2 bzip2-devel

4.configure: error: Please reinstall the libcurl distribution - easy.h should be in /include/curl/

解决:

# yum install libcurl libcurl-devel

5.If configure fails try –with-webp-dir=

configure: error: jpeglib.h not found.

解决:

# yum install libjpeg libjpeg-devel

6.If configure fails try –with-webp-dir=

checking for jpeg_read_header in -ljpeg… yes

configure: error: png.h not found.

解决:

# yum install libpng libpng-devel

7.If configure fails try –with-webp-dir=

checking for jpeg_read_header in -ljpeg… yes

checking for png_write_image in -lpng… yes

If configure fails try –with-xpm-dir=

configure: error: freetype-config not found.

解决:

# yum install freetype freetype-devel

8.configure: error: Unable to locate gmp.h

解决:

# yum install gmp gmp-devel

9.configure: error: mcrypt.h not found. Please reinstall libmcrypt.

解决:

  # yum install libmcrypt libmcrypt-devel

10.configure: error: Please reinstall readline - I cannot find readline.h

解决:

# yum install readline readline-devel

11.configure: error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution

解决:

# yum install libxslt libxslt-devel

12 configure: error: Don’t know how to define struct flock on this system,set –enable-opcache=no
解决:
在编译php时报错:

configure: error: Don’t know how to define struct flock on this system,set –enable-opcache=no

这个错误第一次见到,估计是新版本的问题,也没找到具体解释,解决方法:

编辑

# vi /etc/ld.so.conf

添加一行

/usr/local/lib

保存退出,执行:

ldconfig

重新编译即可。

估计是新版本php编译的时候没有自动去/usr/local/lib下搜寻库文件。

6.编译与安装

# make && make install

这里要make好久,要耐心一下

7.添加 PHP 命令到环境变量

# vim /etc/profile

在末尾加入

PATH=$PATH:/usr/local/php/binexport PATH

要使改动立即生效执行

# source /etc/profile

查看环境变量

# echo $PATH

查看php版本

# php -v

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

9.启动php-fpm

# /etc/init.d/php-fpm start

五、配置nginx虚拟机,绑定域名

# vim /etc/nginx/conf.d/php7.aaa.com.conf

这里可以把php7.com.conf改成自己的域名
把下面的内容复制到php7.com.conf里

server{    listen 80;    server_name  php7.com;    root /var/www/html/php7.com; # 该项要修改为你准备存放相关网页的路径    location / {        index  index.php index.html index.htm;         #如果请求既不是一个文件,也不是一个目录,则执行一下重写规则         if (!-e $request_filename)         {            #地址作为将参数rewrite到index.php上。            rewrite ^/(.*)$ /index.php/$1;            #若是子目录则使用下面这句,将subdir改成目录名称即可。            #rewrite ^/subdir/(.*)$ /subdir/index.php/$1;         }    }    #proxy the php scripts to php-fpm    location ~ \.php {            include fastcgi_params;            ##pathinfo支持start            #定义变量 $path_info ,用于存放pathinfo信息            set $path_info "";            #定义变量 $real_script_name,用于存放真实地址            set $real_script_name $fastcgi_script_name;            #如果地址与引号内的正则表达式匹配            if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {                    #将文件地址赋值给变量 $real_script_name                    set $real_script_name $1;                    #将文件地址后的参数赋值给变量 $path_info                    set $path_info $2;            }            #配置fastcgi的一些参数            fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;            fastcgi_param SCRIPT_NAME $real_script_name;            fastcgi_param PATH_INFO $path_info;            ###pathinfo支持end        fastcgi_intercept_errors on;        fastcgi_pass   127.0.0.1:9000;    }    location ^~ /data/runtime {    return 404;    }    location ^~ /application {    return 404;    }    location ^~ /simplewind {    return 404;    }}

2.重启nginx

# service nginx reload

3.

# vim /var/www/html/php7.aaa.com/index.php

把下面的代码复制到这个文件 里

原创粉丝点击