千家信息网

安装配置LNMP

发表于:2025-02-08 作者:千家信息网编辑
千家信息网最后更新 2025年02月08日,源码安装配置LNMP更新时间:2016-08-02系统环境:CentOS 6.5软件环境:nginx 1.8.1、mysql 5.6.22、php 5.6.19本机IP :192.168.1.88、1
千家信息网最后更新 2025年02月08日安装配置LNMP
  • 源码安装配置LNMP


更新时间:2016-08-02

系统环境:CentOS 6.5

软件环境:nginx 1.8.1、mysql 5.6.22、php 5.6.19

本机IP :192.168.1.88、192.168.1.36


  • 关闭防火墙和安全机制

#service iptables stop

#setenforce 0

#getenforce

【nginx】

1. 创建用户

#groupadd nginx

#useradd -M -s /sbin/nologin nginx -g nginx

2. 编译安装

#yum -y install pcre-devel zlib-devel gcc

# tar zvxf nginx-1.8.1.tar.gz -C /usr/src/

# cd /usr/src/nginx-1.8.1/

# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_flv_module --with-http_gzip_static_module

#make && make install

3. 创建软连接
#ln -s /usr/local/nginx/sbin/* /usr/local/sbin/

4.检查配置文件

# nginx -t

5. 启动nginx服务

#nginx

#netstat -anpt | grep nginx

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 17295/nginx

  • 浏览器访问本机IP

nginx配置优化

worker_processes auto //nginx
要开启的进程数,一般等于cpu的总核数 (设置为"auto"将尝试自动检测它)

worker_rlimit_nofile 102400 //
这个指令是指当一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(ulimit -n)与nginx进程数相除,但是nginx分配请求并不是那么均匀,所以最好与ulimit -n的值保持一致。

worker_connections 5000 //
每个进程允许的最多连接数,理论上每台nginx服务器的最大连接数为worker_processes*worker_connections

keepalive_timeout 60 //keepalive
超时时间

access_log /var/log/nginx/access.log //
设置nginx是否将存储访问日志

error_log /var/log/nginx/error.log //
告诉nginx只能记录严重的错误



【mysql】

1.创建用户

#useradd mysql
2.编译安装
# yum -y install ncurses ncurses-devel gcc libtool gcc-c++ make cmake
#tar zvxf mysql-5.6.22.tar.gz -C /usr/src

#cd /usr/src/mysql-5.6.22/

# 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_PARTITION_STORAGE_ENGINE=1 \

-DENABLE_DOWNLOADS=1 \

-DWITH_READLINE=1 \

-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \

-DMYSQL_TCP_PORT=3306 \

-DDEFAULT_CHARSET=utf8 \

-DDEFAULT_COLLATION=utf8_general_ci \

-DEXTRA_CHARSETS=all \

-DMYSQL_USER=mysql

#make && make install

3. 更改属主属组

#chown -R mysql:mysql /usr/local/mysql/

初始化:

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

拷贝文件:

# cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld

# cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf

添加启动mysqld服务:

[root@localhost ~]# chkconfig --add mysqld

[root@localhost ~]# chkconfig mysqld on

[root@localhost ~]# service mysqld start

Starting MySQL.. [确定]

软连接:

[root@localhost ~]# ln -s /usr/local/mysql/bin/* /usr/local/bin/


4. 设置mysql密码

#mysqladmin -uroot password 'newpass'
修改密码报错:
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost' (using password: YES)'
进数据库更改密码:
  • 1.service mysqld stop

  • 2. mysqld_safe --skip-grant-tables &

  • 3. mysql -uroot -p 回车进入

  • >use mysql;
    > update user set password=PASSWORD("newpass")where user="root";

    > flush privileges;
    更新权限
    > quit
    退出

  • 4.service mysqld restart

  • 5. mysql -uroot -pnewpass 进入


【PHP】

1. 编译安装

# yum -y install openssl openssl-devel libxml2 libxml2-devel bzip2 bzip2-devel curl curl-devel libjpeg libpng freetype libjpeg-devel libpng-devel freetype-devel libmcrypt-devel

# tar zvxf php-5.6.19.tar.gz -C /usr/src

# cd /usr/src/php-5.6.19/

# ./configure --prefix=/usr/local/php5 --with-config-file-path=/usr/local/php5 --enable-fpm --with-fpm-user=php --with-fpm-group=php --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir=/usr/local/freetype --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --disable-fileinfo --enable-opcache

报错:

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

#tar zvxf libmcrypt-2.5.7.tar.gz -C /usr/src/

#cd /usr/src/libmcrypt-2.5.7/

#./configure --prefix=/usr/local/libmcrypt && make && make install

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

#vim /etc/ld.so.conf.d/local.conf

/usr/local/lib # 添加该行

# ldconfig //使之生效

继续编译安装:
# make && make install


2. 配置php-fpm模块

[root@localhost php-5.6.19]# cp php.ini-development /usr/local/php5/php.ini

[root@localhost ~]# cd /usr/local/php5/etc/

[root@localhost etc]# ls

pear.conf php-fpm.conf.default

[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf

[root@localhost etc]# useradd -M -s /sbin/nologin php

编辑配置文件

# vim php-fpm.conf

25 pid = run/php-fpm.pid

131 user = php

132 group =php

236 pm.max_children = 50

241 pm.start_servers = 20

246 pm.min_spare_servers = 5

251 pm.max_spare_servers = 35

启动php-fpm

[root@localhost etc]# /usr/local/php5/sbin/php-fpm

[root@localhost etc]# netstat -anpt | grep php-fpm

tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 45716/php-fpm


3.将php-fpm模块加入nginx配置文件

#vim /usr/local/nginx/conf/nginx.conf

server {

listen 80;

server_name www.baidu.com;

charset utf-8;

access_log logs/host.access.log main;

location / {

root html/baidu;

index index.html index.htm index.php;

}

location ~ \.php$ {

root html/baidu;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

include fastcgi.conf;

}

}

重启nginx

[root@localhost etc]# killall nginx

[root@localhost etc]# netstat -anpt | grep nginx

[root@localhost etc]# nginx

[root@localhost etc]# netstat -anpt | grep nginx

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 45787/nginx


4. 测试

域名映射

# vim /etc/hosts

192.168.1.36 www.baidu.com

创建网页目录

#mkdir /usr/local/nginx/html/baidu

测试

# echo Baidu > /usr/local/nginx/html/baidu/index.html

# elinks --dump 192.168.1.36

Baidu


测试nginxPHP连通性

[root@localhost baidu]# pwd

/usr/local/nginx/html/baidu

[root@localhost baidu]# cat 2.php

phpinfo();

?>

测试数据库和PHP连通性


#vim /usr/local/nginx/html/baidu/test.php

$link=mysql_connect('localhost','root','123456'); //连接mysql数据库

if($link) echo "

恭喜你,大功告成!!

"; //连接成功则返回信息

mysql_close(); //关闭数据库连接

?>


[root@localhost html]# elinks --dump192.168.1.36/test.php

恭喜你,大功告成!!


在网上查询的,未实验过。
mysql_connect这个模块将在未来弃用,请你使用mysqli或者PDO来替代。

解决方法1:

禁止PHP报错

display_errors = On

改为

display_errors = Off

鉴于这个服务器都是给用户用的,有时候他们需要报错,不能这做,让他们改程序吧,看方案2.

解决方法2:

常用的php语法连接MySQL如下


$link = mysql_connect('localhost', 'user', 'password');

mysql_select_db('dbname', $link);


改成mysqi

$link = mysqli_connect('localhost', 'user', 'password', 'dbname');

常用mysql建表SQL如下

// 老的

mysql_query('CREATE TEMPORARY TABLE `table`', $link);

// 新的

mysqli_query($link, 'CREATE TEMPORARY TABLE `table`');

解决方法三:
在php程序代码里面设置报警级别

error_reporting(E_ALL ^ E_DEPRECATED);


有不对请指出,多多指教。qq:2632886623

0