全新Linux安装后必备操作流程

1. 创建初始化快照;

2. 关闭防火墙;vim /etc/selinux/config 配置项为disabled;

3. 设置YUM源(可以一次性复制粘贴以下全部的命令,以CentOS6为例)
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
yum clean all
yum list
yum makecache
yum -y install gcc gcc++
mkdir /soft

4. 一键安装lnmp.org安装包,下载软件包存放至/soft统一管理

备注:
https://opsx.alibaba.com/mirror //阿里巴巴镜像站
http://man.linuxde.net/ //linux命令大全
https://github.com/webjust/linux //我的linux课件

简化版的CentOS7 安装:ifconfig

LNMP1.3一键安装Linux环境,配置Nginx运行ThinkPHP3.2

你是否遇见过:安装LNMP1.3环境后,运行ThinkPHP 3.2,只能打开首页,不能访问控制器,报404错误。

按照以下3步设置,即可解决。

ThinkPHP支持的URL模式有四种:普通模式、PATHINFO、REWRITE和兼容模式,系统默认的PATHINFO模式。

LNMP1.3 一键安装完成后,默认支持REWRITE,需要手动开启 PATHINFO。

第1步修改:php.ini文件
位置:/usr/local/php/etc/php.ini

搜索查找到:cgi.fix_pathinfo 配置项,默认为0,修改为1,开启 pathinfo 选项。

如图1:

第2步修改:nginx的配置文件 (笔者使用的是虚拟域名配置文件:/usr/local/nginx/conf/vhost/*.conf)

找到 server 的配置选项:

默认只有 include enable-php.conf,请注释掉;
然后添加一行:include enable-php-pathinfo.conf

如:

Select Code
1
2
3
#error_page   404   /404.html;
#include enable-php.conf;             # 注册这一行
include enable-php-pathinfo.conf;     # 加入这行

如图2:

继续修改,在添加下面配置信息:

Select Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
location ~ .php
{
    set $path_info "";
    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;
}

在Nginx,可以通过在Nginx.conf中配置转发规则实现,解决其他不支持PATHINFO的WEB服务器环境。

#如果请求既不是一个文件,也不是一个目录,则执行一下重写规则

Select Code
1
2
3
4
5
6
7
if (!-e $request_filename)
{
	#地址作为将参数rewrite到index.php上。
	rewrite ^/(.*)$ /index.php/$1;
	#若是子目录则使用下面这句,将subdir改成目录名称即可。
	#rewrite ^/subdir/(.*)$ /subdir/index.php/$1;
}

官方出处:http://document.thinkphp.cn/manual_3_2.html#url_rewrite

第3步:重启LNMP环境,配置生效。

最终效果测试:
1. 去掉了 index.php
2. 可以访问控制器下的方法。
3. U 方法正确。

如图3:

参考:笔者配置文件示例:

Select Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
server
    {
        listen 80;
        #listen [::]:80;
        server_name tp32.com;
        index index.html index.htm index.php default.html default.htm default.php;
        root  /home/wwwroot/tp32.com;

        include other.conf;
        #error_page   404   /404.html;
        #include enable-php.conf;
        include enable-php-pathinfo.conf; #加入这行


        location ~ .php
        {
            set $path_info "";
            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;
        }

        #如果请求既不是一个文件,也不是一个目录,则执行一下重写规则
        if (!-e $request_filename)
        {
            #地址作为将参数rewrite到index.php上。
            rewrite ^/(.*)$ /index.php/$1;
            #若是子目录则使用下面这句,将subdir改成目录名称即可。
            #rewrite ^/subdir/(.*)$ /subdir/index.php/$1;
        }

        access_log  /home/wwwlogs/tp32.com.log;
    }

入口文件index.php

Select Code
1
2
//nginx环境下防止U方法输出错误
define('__APP__', '');

参考:
http://www.thinkphp.cn/topic/3138.html

CentOS 6.5源码安装PHP7.1

注意:本文章是作者实验安装记录日志。源码安装实验比较久,请勿尝试。

学习总结步骤:

1、源码安装httpd-2.2.*
2、源码安装php7.*

在执行./configure检测环境时,会报错提示:
configure: error: xml2-config not found. Please check your libxml2 installation.

解决办法:
rpm -qa | grep libxml2 # 检查是否安装了libxml2包

# 重新安装libxml2和libxml2-devel包
yum install libxml2
yum install libxml2-devel -y

解决:xml2-config文件不存在的问题。

这样,就在执行./configure就不会报错。然后make, make install

3、配置文件php.ini

4、检查httpd.conf是否开启了php7模块
LoadModule php7_module modules/libphp7.so

摘要:PHP7源码包中的INSTALL文件

Select Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
1. Obtain  the  Apache HTTP server from the location listed above, and
       unpack it:
gzip -d httpd-2_x_NN.tar.gz
tar -xf httpd-2_x_NN.tar

    2. Likewise, obtain and unpack the PHP source:
gunzip php-NN.tar.gz
tar -xf php-NN.tar

    3. Build  and install Apache. Consult the Apache install documentation
       for more details on building Apache.
cd httpd-2_x_NN
./configure --enable-so
make
make install

    4. Now  you  have  Apache  2.x.NN  available under /usr/local/apache2,
       configured  with  loadable  module  support  and  the  standard MPM
       prefork.  To  test  the  installation use your normal procedure for
       starting the Apache server, e.g.:
/usr/local/apache2/bin/apachectl start

       and stop the server to go on with the configuration for PHP:
/usr/local/apache2/bin/apachectl stop

    5. Now,  configure and build PHP. This is where you customize PHP with
       various  options,  like  which  extensions  will  be  enabled.  Run
       ./configure  --help for a list of available options. In our example
       we'll do a simple configure with Apache 2 and MySQL support.
       If  you  built  Apache  from  source, as described above, the below
       example  will match your path for apxs, but if you installed Apache
       some other way, you'll need to adjust the path to apxs accordingly.
       Note that some distros may rename apxs to apxs2.
cd ../php-NN
./configure --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql
make
make install

       If  you decide to change your configure options after installation,
       you'll  need to re-run the configure, make, and make install steps.
       You  only need to restart apache for the new module to take effect.
       A recompile of Apache is not needed.
       Note  that  unless told otherwise, 'make install' will also install
       PEAR,  various  PHP  tools such as phpize, install the PHP CLI, and
       more.
    6. Setup your php.ini
cp php.ini-development /usr/local/lib/php.ini

       You  may  edit  your  .ini  file  to set PHP options. If you prefer
       having php.ini in another location, use
       --with-config-file-path=/some/path in step 5.
       If  you  instead  choose php.ini-production, be certain to read the
       list of changes within, as they affect how PHP behaves.
    7. Edit  your httpd.conf to load the PHP module. The path on the right
       hand side of the LoadModule statement must point to the path of the
       PHP  module  on  your  system. The make install from above may have
       already added this for you, but be sure to check.
LoadModule php7_module modules/libphp7.so
    8. Tell  Apache to parse certain extensions as PHP. For example, let's
       have  Apache  parse  .php  files  as PHP. Instead of only using the
       Apache  AddType  directive,  we want to avoid potentially dangerous
       uploads  and  created  files  such  as  exploit.php.jpg  from being
       executed   as   PHP.   Using  this  example,  you  could  have  any
       extension(s)  parse as PHP by simply adding them. We'll add .php to
       demonstrate.
<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>
       Or,  if we wanted to allow .php, .php2, .php3, .php4, .php5, .php7,
       and  .phtml files to be executed as PHP, but nothing else, we'd use
       this:
<FilesMatch "\.ph(p[2-7]?|tml)$">
    SetHandler application/x-httpd-php
</FilesMatch>
       And  to  allow  .phps files to be handled by the php source filter,
       and displayed as syntax-highlighted source code, use this:
<FilesMatch "\.phps$">
    SetHandler application/x-httpd-php-source
</FilesMatch>
       mod_rewrite  may  be  used  To  allow any arbitrary .php file to be
       displayed  as  syntax-highlighted  source  code,  without having to
       rename or copy it to a .phps file:
RewriteEngine On
RewriteRule (.*\.php)s$ $1 [H=application/x-httpd-php-source]
       The  php source filter should not be enabled on production systems,
       where it may expose confidential or otherwise sensitive information
       embedded in source code.
    9. Use your normal procedure for starting the Apache server, e.g.:
/usr/local/apache2/bin/apachectl start

LINUX系统下LNMP一键安装步骤

一、前期准备:
下载LNMP一键安装包:http://lnmp.org/install.html
yum源正常
关闭rpm默认安装的apache和mysql(最好是新装的新系统)
关闭selinux和iptables
(//关闭selinux
vi /etc/selinux/config
SELINUX=disabled;

//关闭iptables
iptables  -F     * 如果没有禁用防火墙,默认80端口禁止访问
iptables     -Z
iptables  -X)

LNMP一键安装

二、一键安装的过程:
1、安装包:lnmp1.0-full.tar.gz,上传到/root,解压命令tar -zxvf 包全名
2、进入解压后的目录,执行命令:./centos.sh,按照提示符选择:y,整个安装过程约20分钟左右
3、安装其他。
默认安装软件:Nginx,MySQL,PHP,PHPMyAdmin,Zend Optimizer(用来优化zend引擎)
4、安装结果:
LNMP相关软件安装目录
Nginx 目录: /usr/local/nginx/
MySQL 目录 : /usr/local/mysql/
MySQL数据库所在目录:/usr/local/mysql/var/
PHP目录 : /usr/local/php/
PHPMyAdmin目录为/home/wwwroot/phpmyadmin/

LNMP相关配置文件位置
Nginx主配置文件:/usr/local/nginx/conf/nginx.conf
/root/vhost.sh添加的虚拟主机配置文件:/usr/local/nginx/conf/vhost/域名.conf
MySQL配置文件:/etc/my.cnf
PHP配置文件:/usr/local/php/etc/php.ini
…..此处省略其他配置文件路径

LNMPA相关目录文件位置
Apache目录:/usr/local/apache/
Apache配置文件:/usr/local/apache/conf/httpd.conf
Apache虚拟主机配置文件目录:/usr/local/apache/conf/vhost/

LNMP一键安装配置文件

三、修改Nginx配置文件(重点)
vim /usr/local/nginx/conf/nginx.conf

LNMP的虚拟域名配置非常简单,只需要修改这2处即可!示例如下:
server_name www.tom.com;
index index.html index.htm index.php;
root  /home/wwwroot/tom;

在WINDOWS下修改修改HOSTS文件:C:\Windows\System32\drivers\etc\hosts,即可访问!

补充:
每次修改Nginx配置文件后做语法检查:
/usr/local/nginx/sbin/nginx -t

之后重启:pkill -HUP nginx (平滑重启命令,毫秒重启)

其他:
关于301永久跳转,页面跳转,只需要修改配置文件即可!

Linux常用命令整理

(想到再更新吧!这里只记录常用的,而不是完整版本的)

tar压缩工具:
tar [-zjxcvfpP] filename
tar -zxvf 用来解压.tar.gz的压缩包
tar -zcvf 在打包的时候直接压缩

增加用户,用户组:
usseradd 用户名
passwd 用户名 //修改用户密码
su //切换用户
//用户的配置文件:
/etc/passwd和/etc/shadow
/etc/group

文件与目录管理:
cd
pwd
mkdir
rm -rf
cp -a
mv //移动文件或者修改文件名
cat
ls -l (缩写ll)
ls -a (显示全部)
chgrp [组名] [文件] (更改所属组)
chown [ -R ] 账户名 文件名 (更改文件所属组)
chown [ -R ] 账户名:组名 文件名 (更改文件所属组)
备注:-R选项只作用于目录,作用是级联更改,即不仅更改当前目录,连目录里的目录或者文件全部更改。
chmod [-R] xyz 文件名 (改变用户对文件的读写执行权限)
备注:linux使用数字去代替rwx, 具体规则为 ‘r’ 等于4, ‘w’ 等于2, ‘x’ 等于1, ‘-‘ 等于0
which
whereis
find [路径] [参数]
‘-atime +n/-n’ : 访问或执行时间大于/小于n天的文件
‘-ctime +n/-n’ : 写入、更改inode属性(例如更改所有者、权限或者链接)时间大于/小于n天的文件
‘-mtime +n/-n’ : 写入时间大于/小于n天的文件
‘-name filename’ 直接查找该文件名的文件
‘-type filetype’ 通过文件类型查找(filetype 包含了 f, b, c, d, l, s 等)

RPM包安装:
mount /dev/cdrom /mnt (命令把光驱挂载到/mnt目录下)
rpm -ivh /mnt/Packages/libjpeg-turbo-devel-1.2.1-1.el6.i686.rpm (安装一个rpm包)
备注:
“-i” : 安装的意思
“-v” : 可视化
“-h” : 显示安装进度
rpm -q rpm包名 (查询一个包是否安装)
yum list (列出所有可用的rpm包)
yum install [-y] [rpm包名] (安装一个rpm包)

安装源码包:
1. ./configure (一般常用的有 –prefix=PREFIX 这个选项的意思是定义软件包安装到哪里。)
2. make
3. make install

下载一个源码包:wget http://mirrors.hust.edu.cn/apache/httpd/httpd-2.2.27.tar.bz2
解码一个源码包:tar jxvf httpd-2.2.27.tar.bz2
配置相关的选项,并生成Makefile
备注:通常源码包都是安装在/usr/local/目录下的

gcc编译器,需要先安装一下:yum install -y gcc

网络配置,维护等:
netstat (查看网络状况)
ifconfig (查看网卡ip)
setup (设定ip, 防火墙等)
vim /etc/selinux/config (防火墙配置,关闭selinux的方法为,使 “SELINUX=disabled”, 默认为 enforcing)
iptables -F (关闭防火墙,3条指令一起执行)
iptables -Z
iptables -X

必备软件:
– 虚拟机:VMware
– 镜像:CentOs
– 远程工具:XShell, Putty
– WinSCP

>学习资料:
<http://www.apelearn.com/study_v2/>

>LNMP一键安装包
<http://lnmp.org/>