阿里CentOS7搭建LNMP(Linux、Nginx、MySQL、PHP7)

供自己或有需要的朋友查询参考。如有错误请帮忙指明,谢谢

1、Nginx

yum安装命令

yum install nginx

中间会提示是否继续,直接 y回车 即可。

显示如下即为成功:

阿里CentOS7搭建LNMP(Linux、Nginx、MySQL、PHP7)

启动Nginx

systemctl start nginx.service

查看Nginx状态

systemctl status nginx.service

设置开机启动

systemctl enable nginx.service

2、MySQL5.7

命令

rpm -Uvh http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm

yum install mysql-community-server mysql-community-devel

中间会提示是否继续,直接 y回车 即可。

显示如下即为成功:

阿里CentOS7搭建LNMP(Linux、Nginx、MySQL、PHP7)

启动MySQL

systemctl start mysqld

查看MySQL状态

systemctl status mysqld

获取初始密码

grep 'temporary password' /var/log/mysqld.log

方框处即为密码

阿里CentOS7搭建LNMP(Linux、Nginx、MySQL、PHP7)

使用密码登陆MySQL并修改密码

mysql -u root -p

//修改密码
ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';

3、PHP7

命令

//下载
wget http://cn2.php.net/get/php-7.2.4.tar.xz/from/this/mirror

//解压
tar -xvf mirror
cd php-7.2.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

//编译配置
./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

成功

阿里CentOS7搭建LNMP(Linux、Nginx、MySQL、PHP7)

安装

make

make install

配置PHP

cp /root/php-7.2.4/php.ini-development /usr/local/php/lib/php.ini

vim /etc/profile

//在结尾添加
PATH=$PATH:/usr/local/php/bin
export PATH

//保存(:wq)后
source /etc/profile

输入php -v有以下内容即为成功

阿里CentOS7搭建LNMP(Linux、Nginx、MySQL、PHP7)

配置php-fpm

cp /root/php-7.2.4/sapi/fpm/php-fpm.service /usr/lib/systemd/system

cd /usr/local/php/etc/
cp php-fpm.conf.default php-fpm.conf

cd /usr/local/php/etc/php-fpm.d
cp www.conf.default www.conf

启动php-fpm

systemctl start php-fpm.service

查看php-fpm状态

systemctl status php-fpm.service

成功显示如下

阿里CentOS7搭建LNMP(Linux、Nginx、MySQL、PHP7)

简单配置Nginx

Nginx的初始配置大致如下

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;


include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}



http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

   include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

一般我们需要修改的地方有

//本人新手,如有错误请帮忙点明!

user Nginx;      =>       user 用户名;     //需要与启动用户相同

server{
    ...
    root 更改为你要设置的网站目录;
    index //网站的入口文件  例如:index index.php index.html;
    ...
}

//添加PHP解析
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;
}

配置完后别忘记了重启Nginx

systemctl restart nginx.service

403 forbidden及File not found 错误、404错误等新手可能出的错误,解决方法大家可以看https://blog.csdn.net/ye594008702/article/details/88946425

 

文章学习参考了很多前辈的博文,在此致谢!