zhangguanzhang's Blog

源码编译安装lnmp

字数统计: 4.4k阅读时长: 22 min
2017/03/15

自己总结的安装步骤,仅供参考

系统是centos 6.8 64位,全部是源码编译安装,理论上来说6.5-7.4都适用
环境搭建是我自己写了个一键脚本,下载地址www.zhangguanzhang.com/download/lnmp.sh可以拿去参考或者修改下,个人需求来写的,废话不多说

开始,创建目录存放源码包(不要存在root目录里,防止读取权限不够)

[root@guan ~]# mkdir -p /usr/local/download
[root@guan ~]# cd !$
cd /usr/local/download

安装基本依赖

[root@guan download]# yum -y install gcc gcc-c++ make

然后看看动态链接库的路径是否完整

[root@guan download]# cat /etc/ld.so.conf
include ld.so.conf.d/*.conf

这样就不完全

[root@guan download]# cat>>/etc/ld.so.conf<<EOF
/usr/local/lib64
/usr/local/lib
/usr/lib
/usr/lib64
EOF
[root@guan download]# ldconfig -v

首先我们来安装nginx

先给nginx安装依赖方便给nginx开启一些参数
[root@guan download]# yum -y install openssl openssl-devel psmisc
[root@guan download]# yum search GeoIP
Loaded plugins: fastestmirror
Determining fastest mirrors
base                                                                                                                | 3.7 kB     00:00     
base/primary_db                                                                                                     | 4.7 MB     00:01     
epel                                                                                                                | 4.3 kB     00:00     
epel/primary_db                                                                                                     | 5.9 MB     00:01     
extras                                                                                                              | 3.4 kB     00:00     
extras/primary_db                                                                                                   |  29 kB     00:00     
updates                                                                                                             | 3.4 kB     00:00     
updates/primary_db                                                                                                  | 2.0 MB     00:00     
=========================================================== N/S Matched: GeoIP ============================================================
GeoIP-devel.i686 : Development headers and libraries for GeoIP
GeoIP-devel.x86_64 : Development headers and libraries for GeoIP
geoipupdate.x86_64 : Update GeoIP2 and GeoIP Legacy binary databases from MaxMind
geoipupdate-cron.noarch : Cron job to do weekly updates of GeoIP databases
geoipupdate-cron6.noarch : Cron job to do weekly updates of GeoIP IPv6 databases
lighttpd-mod_geoip.x86_64 : GeoIP module for lighttpd to use for location lookups
mod_geoip.x86_64 : GeoIP module for the Apache HTTP Server
nginx-mod-http-geoip.x86_64 : Nginx HTTP geoip module
opensips-mmgeoip.x86_64 : Wrapper for the MaxMind GeoIP API
python-GeoIP.x86_64 : Python bindings for the GeoIP geographical lookup libraries
python-pygeoip.noarch : Pure Python GeoIP API
uwsgi-plugin-geoip.x86_64 : uWSGI - Plugin for GeoIP support
GeoIP.i686 : Library for country/city/organization to IP address or hostname mapping
GeoIP.x86_64 : Library for country/city/organization to IP address or hostname mapping
GeoIP-GeoLite-data.noarch : Free GeoLite IP geolocation country database
GeoIP-GeoLite-data-extra.noarch : Free GeoLite IP geolocation databases
geoip-geolite.noarch : Free IP geolocation databases
php-pecl-geoip.x86_64 : Extension to map IP addresses to geographic places

  Name and summary matches only, use "search all" for everything.

如果像我这样yum能搜到GeoIP就执行下面的

[root@guan download]# yum -y install GeoIP GeoIP-devel

如果搜不到这个依赖就下面手动安装

[root@guan download]# wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP-1.4.6.tar.gz
[root@guan download]# tar -zxvf GeoIP-1.4.6.tar.gz
[root@guan download]# cd GeoIP-1.4.6
[root@guan GeoIP-1.4.6]# ./configure
[root@guan GeoIP-1.4.6]# make && make install
[root@guan GeoIP-1.4.6]# cd ..
[root@guan download]# wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
[root@guan download]# mv GeoIP.dat.gz /usr/local/share/GeoIP/

下载pcre,和我一样下载失败的话再下一次,下载完成后解压

[root@guan download]# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz
--2017-06-29 18:34:58--  ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz
           => “pcre-8.40.tar.gz”
Resolving ftp.csx.cam.ac.uk... failed: Name or service not known.
wget: unable to resolve host address “ftp.csx.cam.ac.uk”
[root@guan download]# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz
--2017-06-29 18:35:17--  ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz
           => “pcre-8.40.tar.gz”
Resolving ftp.csx.cam.ac.uk... 131.111.8.115
Connecting to ftp.csx.cam.ac.uk|131.111.8.115|:21... connected.
Logging in as anonymous ... Logged in!
==> SYST ... done.    ==> PWD ... done.
==> TYPE I ... done.  ==> CWD (1) /pub/software/programming/pcre ... done.
==> SIZE pcre-8.40.tar.gz ... 2065161
==> PASV ... done.    ==> RETR pcre-8.40.tar.gz ... done.
Length: 2065161 (2.0M) (unauthoritative)

100%[=================================================================================================>] 2,065,161   12.5K/s   in 3m 36s  

2017-06-29 18:39:01 (9.36 KB/s) - “pcre-8.40.tar.gz” saved [2065161]

[root@guan download]# tar -zxvf pcre-8.40.tar.gz

添加www作为nginx的用户组和用户

[root@guan download]# groupadd www
[root@guan download]# useradd -M -s /sbin/nologin -g www www

进入官网后,选择一个版本右击获取下载地址链接
t1
我选的是中间的稳定版nginx-1.12.0,编译参数自己根据需要添加和去掉,字面自己看那些参数不能去掉,我就不说了

[root@guan download]# wget http://nginx.org/download/nginx-1.12.0.tar.gz
[root@guan download]# tar -zxvf nginx-1.12.0.tar.gz
[root@guan download]# cd nginx-1.12.0
[root@guan nginx-1.12.0]# ./configure \
--user=www \
--group=www \
--prefix=/usr/local/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-http_ssl_module \
--with-http_v2_module \
--with-pcre=../pcre-8.40 \
--with-http_geoip_module \
--with-mail \
--with-mail_ssl_module

configure不报错的话就是下图里的样子,一般不会报错的
t2

然后编译安装nginx

1
[root@guan nginx-1.12.0]# make && make install

Centos6的话下载nginx官方的启动脚本(脚本方便下载我存在了我的七牛云空间里)解压到启动路径里后改权限

1
2
3
4
[root@guan nginx-1.12.0]# cd ..
[root@guan download]# wget http://ojt8x896o.bkt.clouddn.com/nginx.tar.gz
[root@guan download]# tar -zxf nginx.tar.gz -C /etc/init.d/
[root@guan download]# chmod 755 /etc/init.d/nginx

Centos7的话执行下面的代码并重新导入systemctl

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
[root@guan download]# cat>/usr/lib/systemd/system/nginx.service<<-'EOF'
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
# Nginx will fail to start if /var/run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /var/run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF
[root@guan download]# systemctl daemon-reload

修改nginx的配置文件/etc/nginx/nginx.conf,设置nginx的运行用户和运行用户组,把#user nobody;改为user www www;
这里我是用sed修改的,偷懒可以复制,自己用vim也可以

[root@guan download]# sed -i 's/#user  nobody;/user www www;/' /etc/nginx/nginx.conf

在配置文件里有下面这一段

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

也就是80端口的配置,root html;是nginx的web的目录,默认这样表示是nginx安装目录的html文件夹
如果要改为其他的话自己创建个目录,然后那个html改为目录的据绝对路径就行了
无论你使用默认的html文件夹还是自己创建一个文件夹来作为web的目录,你都要改所属者和组
这里我以默认的作为示例

[root@guan download]# chown -R www:www /usr/local/nginx/html

然后通过启动脚本启动nginx,用curl来查看本地的web server的默认index页面
Centos6是service nginx start,Centos7是systemctl start nginx

[root@guan download]# service nginx start
Starting nginx:                                            [  OK  ]
[root@guan download]# curl localhost
#此处省略curl的结果........

如果启动提示can not found /etc/sysconfig/network file啥的话(这种情况应该不存在,不过我在安卓安装centos看到就没有这个网卡文件)

[root@guan download]# cat>/etc/sysconfig/network<<EOF
NETWORKING=yes
HOSTNAME=$HOSTNAME
EOF

云主机的话外网ip访问看看能不能看到nginx的默认页面,看不到的话去主机供应商的官网登陆后到web控制面板里找安全规则啥的之类的,看看80端口规则是不是被关闭了


接下来安装mysql

rpm安装点我 5.7以及以上安装有点不同 这里我安装的是5.6.35
链接: https://pan.baidu.com/s/1kVc7e9T 密码: wf5v
可以自行用ftp上传 先安装基本依赖,mysql5.5以后采用cmake编译 这里我的mysql源码包我是放在我的七牛云空间,所以这里我用wget下载
[root@guan download]# yum -y install cmake ncurses-devel bison bison-devel
[root@guan download]# cd /usr/local/mysql
[root@guan download]# wget http://ojt8x896o.bkt.clouddn.com/mysql-5.6.35.tar.gz
[root@guan download]# tar -zxvf mysql-5.6.35.tar.gz
[root@guan download]# cd mysql-5.6.35

添加用户作为mysql的运行用户

[root@guan mysql-5.6.35]# groupadd mysql
[root@guan mysql-5.6.35]# useradd -r -g mysql -s /bin/false -M mysql
[root@guan mysql-5.6.35]# cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DSYSCONFDIR=/etc \
-DMYSQL_USER=mysql \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DMYSQL_UNIX_ADDR=/var/run/mysql/mysql.sock \
-DMYSQL_TCP_PORT=3306 \
-DENABLED_LOCAL_INFILE=1 \
-DENABLE_DOWNLOADS=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_DEBUG=0 \
-DMYSQL_MAINTAINER_MODE=0 \
-DWITH_SSL:STRING=bundled \
-DWITH_ZLIB:STRING=bundled

后面懒得敲了,直接从我的lamp那个文章里复制吧



编译安装mysql

[root@guan mysql-5.6.35]# make && make install

更改权限和所属者

[root@guan mysql-5.6.35]# chown -R mysql:mysql /usr/local/mysql
[root@guan mysql-5.6.35]# cd /usr/local/mysql

初始化mysql

[root@guan mysql]# ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
完成后如图上面有些信息来叫你如何完善mysql以及安全
mysql默认的配置文件路径是/etc/my.cnf,我们复制安装自带的默认配置文件过去
[root@guan mysql]# echo yes|cp support-files/my-default.cnf /etc/my.cnf

配置/etc/my.cnf,把相关路径信息加到配置文件里,这里我用sed修改的
[root@guan mysql]# sed -i 's/# basedir = \.\.\.\.\./ basedir = \/usr\/local\/mysql/' /etc/my.cnf
[root@guan mysql]# sed -i 's/# datadir = \.\.\.\.\./ datadir = \/usr\/local\/mysql\/data/' /etc/my.cnf
[root@guan mysql]# sed -i 's/# port = \.\.\.\.\./ port = 3306/' /etc/my.cnf
[root@guan mysql]# sed -i 's/# socket = \.\.\.\.\./ socket = \/var\/run\/mysql\/mysql.sock/' /etc/my.cnf
[root@guan mysql]# num=$(sed -n '/socket/=' /etc/my.cnf)
[root@guan mysql]# sed -i "$num a[mysqld_safe]\nlog-error=/var/log/mysqld.log\npid-file=/var/run/mysql/mysqld.pid\n" /etc/my.cnf

cmake的时候把mysql的sock文件指定到/var/run/mysql里符合linux文件目录规范方便日后管理,但是默认没有这个文件夹,手动创建

[root@guan mysql]# mkdir /var/run/mysql
[root@guan mysql]# chown mysql:mysql /var/run/mysql

启动mysql

[root@guan mysql]# ./support-files/mysql.server start

进入mysql客户端

[root@guan mysql]# bin/mysql -uroot -p

回车后会有个输出提示没有设置密码

此时直接回车,然后进去mysql客户端里




进入mysql的客户端后,我们来在客户端里的命令行里给mysql设置root密码

use mysql;
delete from mysql.user where user='';
update user set password=password("123456") where user="root";
flush privileges;
exit

其中123456为你要设置的密码

第二行是删除mysql的匿名用户
flush privileges 命令本质上的作用是将当前user和privilige表中的用户信息/权限设置从mysql库(MySQL数据库的内置库)中提取到内存里。MySQL用户数据和权限有修改后,希望在不重启MySQL服务的情况下直接生效


Centos6的话执行下面把启动脚本添加到service里并设置权限

[root@guan mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@guan mysql]# chmod u+x /etc/init.d/mysqld

以后我们启动mysql服务直接输入

service mysqld start

如果要开机自启就输入下面的命令

chkconfig --add mysqld

Centos7的话执行下面代码

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
[root@guan mysql]# cat>/usr/lib/systemd/system/mysqld.service<<-'EOF'
[Unit]
Description=MySQL Community Server
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
Alias=mysql.service
[Service]
User=mysql
Group=mysql
#systemctl status就是根据pid来判断服务的运行状态的
PIDFile=/var/run/mysqld/mysqld.pid
# 以root权限来启动程序
PermissionsStartOnly=true
# 设置程序启动前的必要操作。例如初始化相关目录等等
#ExecStartPre=/usr/local/mysql/bin/mysql-systemd-start pre
# 启动服务
ExecStart=/usr/local/mysql/bin/mysqld_safe
# Don't signal startup success before a ping works
#ExecStartPost=/usr/local/mysql/bin/mysql-systemd-start post
# Give up if ping don't get an answer
TimeoutSec=600
#Restart配置可以在进程被kill掉之后,让systemctl产生新的进程,避免服务挂掉
Restart=always
PrivateTmp=false
EOF
[root@guan mysql]# systemctl daemon-reload

启动和停止mysqld的话用

1
systemctl start[stop|restart] mysqld

开机自启的话

1
systemctl enable mysqld

如果需要把mysql的安装目录下的bin加入环境变量里下面俩条命令就行了

echo 'export PATH=/usr/local/mysql/bin:$PATH'>>/etc/profile
source /etc/profile

以后进入mysql客户端输入下面命令就可以了

mysql -uroot -p


接下来安装php

先安装依赖
[root@guan mysql]# yum -y install openssl openssl-devel bzip2-devel libxslt libxslt-devel libxml2 libxml2-devel autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel  zlib zlib-devel glibc glibc-devel procps-ng

然后如果php要开启mcrypt的话,查看下yum能安装libmcrypt不,也就是搜索

[root@guan mysql]# yum search libmcrypt  2>&1 |grep '^No'

如果上面命令有输出,也就是搜索到no,说明yum源里没有libmcrypt,我们就手动安装libmcrypt

[root@guan mysql]# cd /usr/local/download
[root@guan download]# wget http://ojt8x896o.bkt.clouddn.com/libmcrypt-2.5.8.tar.gz -O libmcrypt-2.5.8.tar.gz
[root@guan download]# tar -zxvf libmcrypt-2.5.8.tar.gz
[root@guan download]# cd libmcrypt-2.5.8
[root@guan libmcrypt-2.5.8]# ./configure
[root@guan libmcrypt-2.5.8]# make && make install
[root@guan libmcrypt-2.5.8]# cd ..

如果没有输出,也就是yum源存在libmcrypt,那就用yum安装

[root@guan mysql]# cd /usr/local/download
[root@guan download]# yum install libmcrypt libmcrypt-devel -y

先安装curl为了后面给php添加curl扩展点我进入curl下载页面
注意curl页面里的最新版本在网页里的最下面

[root@guan mysql]# cd /usr/local/download
[root@guan download]# wget http://ojt8x896o.bkt.clouddn.com/curl-7.52.1.tar.gz
[root@guan download]# tar -zxvf curl-7.52.1.tar.gz
[root@guan download]# cd curl-7.52.1
[root@guan curl-7.52.1]# ./configure --prefix=/usr/local/curl
[root@guan curl-7.52.1]# make && make install
[root@guan curl-7.52.1]# cd ..

进入php官网,进入download页面点击tar.gz包



进去后我们找香港的源,右击后选择复制链接





然后粘贴用wget下载

[root@guan download]# wget http://hk1.php.net/get/php-5.6.29.tar.gz/from/this/mirror -O php-5.6.29.tar.gz

上面的wget下载失败的话尝试下面这个

[root@guan download]# wget http://ojt8x896o.bkt.clouddn.com/php-5.6.29.tar.gz -O php-5.6.29.tar.gz

解压编译安装,不需要的参数可以自己删掉
编译php需要资源,make的时候如果主机配置差有时候会因为内存不够而中断出现memory字样,所以建议先关闭nginx和mysqld,centos7关闭时用systemctl stop nginx(mysqld)

[root@guan download]# service nginx stop
[root@guan download]# service mysqld stop
[root@guan download]# tar -zxvf php-5.6.29.tar.gz
[root@guan download]# cd php-5.6.29
[root@guan php-5.6.29]# ./configure --prefix=/usr/local/php \
--enable-bcmath \
--enable-calendar \
--enable-fpm \
--enable-ftp \
--enable-mbstring \
--enable-pcntl \
--enable-shmop \
--enable-soap \
--enable-sockets \
--enable-sysvsem \
--enable-zip \
--with-bz2 \
--with-curl=/usr/local/curl \
--with-freetype-dir=/usr/lib64 \
--with-gettext \
--with-gd \
--with-jpeg-dir=/usr/lib64 \
--with-openssl \
--with-png-dir=/usr/lib64 \
--with-xmlrpc \
--with-xsl \
--with-mcrypt \
--with-mysql \
--with-mysqli \
--with-pdo-mysql \
--with-zlib \
--with-config-file-path=/usr/local/php/etc/
[root@guan php-5.6.29]# make 
[root@guan php-5.6.29]# make install

php默认的配置文件php.ini是不存在的我们需要手背拷贝过去
在源码包里有俩ini,php.ini-development和php.ini-production
我是拷贝的development,并且修改配置文件里的时区为PRC

[root@guan php-5.6.29]# cp /usr/local/php/etc/{php-fpm.conf.default,php-fpm.conf}
[root@guan php-5.6.29]# echo yes|cp php.ini-development /usr/local/php/etc/php.ini
[root@guan php-5.6.29]# sed -ri '/;date.timezone =/s#;date.timezone =#date.timezone = PRC#' /usr/local/php/etc/php.ini

Centos6的话拷贝php-fpm的启动脚本并且修改权限

1
2
[root@guan php-5.6.29]# echo yes|cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@guan php-5.6.29]# chmod 755 /etc/init.d/php-fpm

Centos7的话创建systemctl脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@guan php-5.6.29]# cat>/usr/lib/systemd/system/php-fpm.service<<-'EOF'
[Unit]
Description=php
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/php/sbin/php-fpm
ExecStop=/bin/pkill -9 php-fpm
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
[root@guan php-5.6.29]# systemctl daemon-reload

php-fpm是读取.conf后缀的,所以复制一份重命名
然后修改配置,其中包括pid位置,运行的用户和用户组,进程数量还有一些其他的

cp /usr/local/php/etc/{php-fpm.conf.default,php-fpm.conf}
[root@guan php-5.6.29]# sed -i 's/;pid/pid/' /usr/local/php/etc/php-fpm.conf
[root@guan php-5.6.29]# sed -i '/user/s#nobody#www#' /usr/local/php/etc/php-fpm.conf
[root@guan php-5.6.29]# sed -i '/group/s#nobody#www#' /usr/local/php/etc/php-fpm.conf
[root@guan php-5.6.29]# sed -i '/pm.max_children/s#5#10#' /usr/local/php/etc/php-fpm.conf
[root@guan php-5.6.29]# sed -i '/pm.max_spare_servers/s#3#6#' /usr/local/php/etc/php-fpm.conf
[root@guan php-5.6.29]# sed -i '/;request_slowlog_timeout/s#;##' /usr/local/php/etc/php-fpm.conf
[root@guan php-5.6.29]# sed -i '/;request_terminate_timeout/s#;##' /usr/local/php/etc/php-fpm.conf
[root@guan php-5.6.29]# sed -ri '/^;slowlog/{s#;##;s#= .+#= var/log/slow.log#}' /usr/local/php/etc/php-fpm.conf

如果要把php加到环境变量的话

echo 'export PATH=/usr/local/php/bin:$PATH'>>/etc/profile

如果要开启redis扩展的话

[root@guan php-5.6.29]# cd /usr/local/download
[root@guan download]# wget http://pecl.php.net/get/redis-3.1.1.tgz
[root@guan download]# tar -zxf redis-3.1.1.tgz
[root@guan download]# cd redis-3.1.1
[root@guan redis-3.1.1]# /usr/local/php/bin/phpize
[root@guan redis-3.1.1]# ./configure --with-php-config=/usr/local/php/bin/php-config
[root@guan redis-3.1.1]# make

测试发现php的so路径有时候是no-debug-zts-20160303,有时候是no-debug-non-zts-20131226
具体是哪个看make install的最后一行的输出就知道了,这里是我之前写脚本的时候应对这个情况用管道给正则提取拼接后输出路径重定向到配置文件里

[root@guan redis-3.1.1]# echo "extension=$(make install | grep 'extensions'|grep -E -o '/.+')redis.so" >> /usr/local/php/etc/php.ini
[root@guan redis-3.1.1]# cd 

然后修改nginx的配置文件/etc/nginx/nginx.conf支持php解析

第一个是根的index,后面加上index.php

1
2
3
location / {
index index.html index.htm index.php;
}

下面是php的部分全部是注释,取消这几行注释,把scripts那改成$document_root
一般来说html和php都存放在一起的,所以静态80的web根和php的根没必要写成俩个,所以写一个就行了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
root /data/web/w;  #网页文件目录
location / {
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

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

如果你是要以其他目录作为web目录,那么这里的root后面写你的目录就行了,目录记得改成www所有者

然后重启nginx看看,Centos7重启是systemctl restart nginx|php-fpm

1
2
[root@guan ~]# service nginx reload
[root@guan ~]# service php-fpm restart</code></pre>

你可以在你的web目录写个php文件测试下看看
至此lnmp安装完成

CATALOG
  1. 1. 首先我们来安装nginx
  2. 2. 接下来安装mysql
  3. 3. 接下来安装php