Centos 6.8编译安装LNMP环境(Nginx+MySQL+PHP)教程

  • Post category:Linux

Centos6.8编译安装LNMP环境(Nginx+MySQL+PHP)教程

本教程将介绍如何在CentOS 6.8系统中编译安装LNMP环境,LNMP环境包括:Nginx、MySQL、PHP。使用源码编译的方式安装,可以更好地了解LNMP环境的配置和运行原理,并且可以根据需要自由配置Nginx、MySQL、PHP等组件的选项,以便最大程度地发挥系统的性能和稳定性。

准备工作

  • 系统:CentOS 6.8
  • 软件:Nginx 1.10.2、MySQL 5.6.38、PHP 7.2.9
  • 软件下载地址:

  • Nginx:http://nginx.org/en/download.html

  • MySQL:https://dev.mysql.com/downloads/mysql/5.6.html
  • PHP:http://php.net/downloads.php

  • 安装必要软件:

yum -y install gcc gcc-c++ make automake autoconf libtool wget zlib zlib-devel openssl openssl-devel pcre pcre-devel libjpeg libjpeg-devel libpng libpng-devel libxml2 libxml2-devel bzip2 bzip2-devel libmcrypt libmcrypt-devel readline readline-devel bison ncurses ncurses-devel libcurl libcurl-devel openldap openldap-devel libtidy libtidy-devel libxslt libxslt-devel

安装 MySQL

  1. 创建MySQL用户和用户组:

shell
groupadd mysql
useradd -g mysql -s /bin/false mysql

  1. 下载MySQL源码包,解压缩并编译安装:

shell
cd /opt/
wget https://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.38.tar.gz
tar zxvf mysql-5.6.38.tar.gz
cd mysql-5.6.38
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DSYSCONFDIR=/etc \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_DEBUG=0 \
-DMYSQL_TCP_PORT=3306 \
-DWITH_SSL=system \
-DWITH_LIBWRAP=0 \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci
make && make install

  1. 修改MySQL配置文件my.cnf:

shell
cp /opt/mysql-5.6.38/support-files/my-medium.cnf /etc/my.cnf

/etc/my.cnf中的内容修改为:

“`
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

[mysqld_safe]
log-error=/usr/local/mysql/data/mysqld.log
pid-file=/usr/local/mysql/data/mysqld.pid
“`

  1. 初始化MySQL:

shell
/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

  1. 启动MySQL:

shell
/usr/local/mysql/bin/mysqld_safe &

如果出现乱码的情况,可以先设置环境变量:

shell
export LANG=en_US.utf8
export LC_ALL=$LANG
export LANGUAGE=$LANG

接下来输入以下命令来设置MySQL root 用户密码:

shell
/usr/local/mysql/bin/mysql_secure_installation

根据提示输入密码,修改 root 密码,并选择是否进行其他设置。

安装 PHP

  1. 下载PHP源码包,解压缩:

shell
cd /opt/
wget http://php.net/distributions/php-7.2.9.tar.gz
tar zxvf php-7.2.9.tar.gz
cd php-7.2.9

  1. 编译安装PHP:

shell
./configure \
--prefix=/usr/local/php7 \
--with-config-file-path=/etc \
--enable-fpm \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv \
--with-zlib \
--with-ssl \
--enable-xml \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--enable-gd \
--with-jpeg-dir=/usr/lib \
--with-png-dir=/usr/lib \
--with-freetype-dir=/usr/lib \
--enable-gd-native-ttf \
--enable-pcntl \
--enable-sockets \
--with-ldap \
--with-ldap-sasl \
--with-mcrypt \
--enable-zip \
--enable-soap \
--without-pear \
--with-gettext \
--disable-fileinfo \
--enable-opcache \
--enable-intl
make && make install

按照以上配置,将PHP安装到/usr/local/php7目录下。

  1. 配置PHP:

shell
cp /opt/php-7.2.9/php.ini-development /etc/php.ini
cp /opt/php-7.2.9/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm

修改/etc/php.ini这个配置文件,设置默认时区和禁用函数:

date.timezone = Asia/Shanghai
disable_functions = passthru,exec,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source

修改/etc/init.d/php-fpm这个文件,设置PHP-FPM的运行参数。

修改PIDFILE的值为/usr/local/php7/var/run/php-fpm.pid

PIDFILE=/usr/local/php7/var/run/php-fpm.pid

  1. 启动PHP-FPM:

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

安装 Nginx

  1. 下载Nginx源码包,解压缩:

shell
cd /opt/
wget http://nginx.org/download/nginx-1.10.2.tar.gz
tar zxvf nginx-1.10.2.tar.gz
cd nginx-1.10.2

  1. 编译Nginx:

shell
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx.pid \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-ipv6 \
--with-http_sub_module \
--without-http_fastcgi_module \
--without-http_uwsgi_module \
--without-http_scgi_module \
--user=nginx \
--group=nginx
make && make install

按照以上配置,将Nginx安装到/usr/local/nginx目录下。

  1. 创建Nginx运行用户和用户组:

shell
groupadd nginx
useradd -g nginx -s /sbin/nologin nginx

  1. 修改Nginx配置文件:

shell
cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak

接着,使用vim编辑工具打开/usr/local/nginx/conf/nginx.conf文件,并将其中的配置替换为如下内容:

“`
user nginx;
worker_processes auto;

error_log /usr/local/nginx/logs/error.log warn;
pid /var/run/nginx.pid;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;

   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  /usr/local/nginx/logs/access.log  main;

   sendfile        on;
   #tcp_nopush     on;

   keepalive_timeout  65;

   #gzip  on;

   server {
       listen       80;
       server_name  localhost;

       #charset koi8-r;

       #access_log  logs/host.access.log  main;

       location / {
           root   /usr/local/nginx/html;
           index  index.html index.htm;
       }

       error_page   500 502 503 504  /50x.html;
       location = /50x.html {
           root   /usr/local/nginx/html;
       }

       # proxy the PHP scripts to Apache listening on 127.0.0.1:80
       #
       #location ~ \.php$ {
       #    proxy_pass   http://127.0.0.1;
       #}

       # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
       #
       location ~ \.php$ {
           root           html;
           fastcgi_pass   127.0.0.1:9000;
           fastcgi_index  index.php;
           fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
           include        fastcgi_params;
       }

       # deny access to .htaccess files, if Apache's document root
       # concurs with nginx's one
       #
       #location ~ /\.ht {
       #    deny  all;
       #}
   }


   # another virtual host using mix of IP-, name-, and port-based configuration
   #
   #server {
   #    listen       8000;
   #    listen       somename:8080;
   #    server_name  somename  alias  another.alias;
   #    root         html;
   #    index        index.html index.htm;
   #
   #    location / {
   #        try_files $uri $uri/ /index.html;
   #    }
   #}


   # HTTPS server
   #
   #server {
   #    listen       443 ssl;
   #    server_name  localhost;
   #
   #    ssl_certificate      cert.pem;
   #    ssl_certificate_key  cert.key;
   #
   #    ssl_session_cache    shared:SSL:1m;
   #    ssl_session_timeout  5m;
   #
   #    ssl_ciphers  HIGH:!aNULL:!MD5;
   #    ssl_prefer_server_ciphers  on;
   #
   #    location / {
   #        root   html;
   #        index  index.html index.htm;
   #    }
   #}

}
“`

  1. 启动Nginx:

shell
/usr/local/nginx/sbin/nginx

至此,LNMP环境的安装和配置完成。

示例

示例一:使用Nginx搭建静态网站

  1. /usr/local/nginx/html目录下新建一个静态页面:

shell
cd /usr/local/nginx/html
echo "Hello, World!" > index.html

  1. 访问网站:

打开浏览器,输入服务器IP地址,即可访问静态页面。

示例地址:http://192.168.1.100/

示例二:使用PHP搭建动态网站

  1. /usr/local/nginx/html目录下新建一个PHP文件:

shell
cd /usr/local/nginx/html
echo "<?php echo 'Hello, World!'; ?>" > index.php

  1. 访问网站:

打开浏览器,输入服务器IP地址,即可访问PHP页面。

示例地址:http://192.168.1.100/

以上就是本次的完整攻略,希望能对你有所帮助。