这篇文章给大家介绍CentOS 7中怎么搭建LNMP环境,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
yum install -y epel-release
yum install -y gcc automake autoconf libtool make gcc-c++ glibc libxslt-devel \ libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 \ libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 \ bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel \ krb5-devel libidn libidn-devel openssl openssl-devel pcre pcre-devel libmcrypt \ libmcrypt-devel cmake
cat > /usr/local/src/url << "EOF" http://nginx.org/download/nginx-1.14.2.tar.gz http://jp2.php.net/distributions/php-5.6.39.tar.gz https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-boost-5.7.25.tar.gz EOF
wget -P /usr/local/src -i /usr/local/src/url
cd /usr/local/src for i in *.tar.gz ; do tar zxvf $i; done
安装依赖包
yum install -y gcc gcc-c++ cmake ncurses ncurses-devel bison
mysql5.7 编译安装需要boost 库,这里官网下载含boost的源码包
添加用户
useradd -s /sbin/nologin mysql
建立所需目录并更改所有者为mysql
mkdir -p /data/mysql/data chown -R mysql:mysql /data/mysql
将下载好的mysql 解压到/usr/local/mysql 目录下
mv /usr/local/src/mysql-boost-5.7.25 /usr/local/mysql
切换到/usr/local/mysql-5.7.25 目录下,编译安装
cmake -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_BOOST=boost
make && make install
安装好后的/usr/local/mysql目录结构
配置
编辑/etc/my.cnf ( 如果没有则创建)
我这边的一个配置文件内容如下
[client] port = 3306 socket = /tmp/mysql.sock [mysqld] port = 3306 socket = /tmp/mysql.sock user = mysql basedir = /usr/local/mysql datadir = /data/mysql/data pid-file = /data/mysql/mysql.pid log_error = /data/mysql/mysql-error.log slow_query_log = 1 long_query_time = 1 slow_query_log_file = /data/mysql/mysql-slow.log skip-external-locking key_buffer_size = 32M max_allowed_packet = 1024M table_open_cache = 128 sort_buffer_size = 768K net_buffer_length = 8K read_buffer_size = 768K read_rnd_buffer_size = 512K myisam_sort_buffer_size = 8M thread_cache_size = 16 query_cache_size = 16M tmp_table_size = 32M performance_schema_max_table_instances = 1000 explicit_defaults_for_timestamp = true #skip-networking max_connections = 500 max_connect_errors = 100 open_files_limit = 65535 log_bin=mysql-bin binlog_format=mixed server_id = 232 expire_logs_days = 10 early-plugin-load = "" default_storage_engine = InnoDB innodb_file_per_table = 1 innodb_buffer_pool_size = 128M innodb_log_file_size = 32M innodb_log_buffer_size = 8M innodb_flush_log_at_trx_commit = 1 innodb_lock_wait_timeout = 50 [mysqldump] quick max_allowed_packet = 16M [mysql] no-auto-rehash [myisamchk] key_buffer_size = 32M sort_buffer_size = 768K read_buffer = 2M write_buffer = 2M
以上配置内容非必须,可依据实际情况,做相应的调整!
把安装目录用户和组更改为mysql
chown -R mysql:mysql /usr/local/mysql
把数据库数据目录用户和组更改为mysql ( 数据库数据目录:/data/mysql/data),方法同上!
注:/data/mysql/data目录下一定要为空才行
初始化mysql
./mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/data
注:到这一步很容易出问题,在初始化的时候一定要加上面的参数,而且在执行这一步操作前/data/mysql/data 这个目录必须是空的;在这里指定的basedir 和 datadir 目录必须要和/etc/my.cnf 配置的目录一直才行。
拷贝可执行配置文件
cp mysql.server /etc/init.d/mysqld
注:有些网友文章在这一步中会写到从/usr/local/mysql/support-files 这个目录复制my-default.cnf 到/etc/下作为my.cnf 配置文件,我这里没有这个文件,所以直接在配置环节,自己配置了一个my.cnf文件。
启动
service mysqld start
注:这一步也是很容易出问题的地方,当这里出现问题的时候,不要慌,看mysql 错误日志的内容,一般根据mysql 错误日志的提示都能很好的解决问题。
测试连接
./mysql -hlocalhost -uroot -p
无需密码即可连接成功
修改环境变量
在/etc/profile 中新增一行
PATH=/usr/local/mysql/bin:$PATH
保存退出
source /etc/profile
设置开机自启动
systemctl enable mysqld
登录MySql并修改root密码
#mysql -uroot -p mysql>ALTER USER 'root'@'localhost' IDENTIFIED BY 'new psd';
4,安装PHP; 创建php账户进入目录
useradd -s /sbin/nologin php-fpm cd /usr/local/src/php-5.6.39
./configure \ --prefix=/usr/local/php \ --with-config-file-path=/usr/local/php/etc \ --enable-fpm \ --with-fpm-user=php-fpm \ --with-fpm-group=php-fpm \ --with-mysqli=mysqlnd \ --with-pdo-mysql=mysqlnd \ --with-libxml-dir \ --enable-pcntl \ --enable-mysqlnd \ --enable-opcache \ --enable-sockets \ --enable-sysvmsg \ --enable-sysvsem \ --enable-sysvshm \ --enable-shmop \ --enable-zip \ --enable-soap \ --enable-xml \ --enable-mbstring \ --disable-rpath \ --disable-debug \ --disable-fileinfo \ --with-pcre-regex \ --with-iconv \ --with-zlib \ --with-mcrypt \ --with-gd \ --with-jpeg-dir \ --with-png-dir \ --enable-gd-native-ttf \ --with-freetype-dir \ --with-openssl \ --with-mhash \ --with-xmlrpc \ --with-curl \ --with-pear \ --enable-ftp \ --enable-exif \ --disable-ipv6 \ --with-imap-ssl
解决php编译报错configure: error: mcrypt.h not found. Please reinstall libmcrypt. configure: error: mcrypt.h not found. Please reinstall libmcrypt. 方法一: yum install -y epel-release yum install -y libmcrypt-devel 两个不能一起安装,因为CentOs6默认的yum源没有 libmcrypt-devel这个包,只能借助epel的yum源,所以先安装epel,再安装 libmcrypt。 方法二: 报错内容:configure: error: mcrypt.h not found. Please reinstall libmcrypt 网上搜索了很多,包括自带的 yum install libmcrypt libmcrypt-devel,这个是没有效果的。 去SourceForget下载libmcrypt http://sourceforge.net/projects/mcrypt/files/Libmcrypt/ ,最新版是mcrypt 2.6.7.gz。下载下来安装仍旧报错libmcrypt没有找到! 继续测试,最后安装成功,感觉是版本太高。 方法(关键点在用 2.5.7版本): #使用wget可以通过以下路径下载 wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/attic/libmcrypt/libmcrypt-2.5.7.tar.gz #解压 tar -zxvf libmcrypt-2.5.7.tar.gz #进入目录 cd libmcrypt-2.5.7 #编译(默认安装到/usr/local/lib/) ./configure --prefix=/usr/local/libmcrypt #执行安装 make && make install 再回到PHP-5.5.13解压缩的ext目录的mcrypt里面 phpize ./configure --with-php-config=/usr/local/php5/bin/php-config \ --with-libmcrypt=/usr/lib/libmcrypt make make test make install vim /etc/php.ini #加入extension=mcrypt.so
make && make install
cp php.ini-production /usr/local/php-fpm/etc/php.ini vi /usr/local/php-fpm/etc/php-fpm.conf
[global] pid = /usr/local/php-fpm/var/run/php-fpm.pid error_log = /usr/local/php-fpm/var/log/php-fpm.log [www] listen = /tmp/php-fcgi.sock listen.mode = 666 user = php-fpm group = php-fpm pm = dynamic pm.max_children = 50 pm.start_servers = 20 pm.min_spare_servers = 5 pm.max_spare_servers = 35 pm.max_requests = 500 rlimit_files = 1024
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm chmod 755 /etc/init.d/php-fpm chkconfig --add php-fpm chkconfig php-fpm on service php-fpm start ps aux |grep php-fpm
cd /usr/local/src/nginx-1.14.2 ./configure --prefix=/usr/local/nginx
make && make install
vim /etc/init.d/nginx
#!/bin/bash # chkconfig: - 30 21 # description: http service. # Source Function Library . /etc/init.d/functions # Nginx Settings NGINX_SBIN="/usr/local/nginx/sbin/nginx" NGINX_CONF="/usr/local/nginx/conf/nginx.conf" NGINX_PID="/usr/local/nginx/logs/nginx.pid" RETVAL=0 prog="Nginx" start() { echo -n $"Starting $prog: " mkdir -p /dev/shm/nginx_temp daemon $NGINX_SBIN -c $NGINX_CONF RETVAL=$? echo return $RETVAL } stop() { echo -n $"Stopping $prog: " killproc -p $NGINX_PID $NGINX_SBIN -TERM rm -rf /dev/shm/nginx_temp RETVAL=$? echo return $RETVAL } reload() { echo -n $"Reloading $prog: " killproc -p $NGINX_PID $NGINX_SBIN -HUP RETVAL=$? echo return $RETVAL } restart() { stop start } configtest() { $NGINX_SBIN -c $NGINX_CONF -t return 0 } case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) restart ;; configtest) configtest ;; *) echo $"Usage: $0 {start|stop|reload|restart|configtest}" RETVAL=1 esac exit $RETVAL
chmod 755 /etc/init.d/nginx chkconfig --add nginx chkconfig nginx on
mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak vi /usr/local/nginx/conf/nginx.conf
user nobody nobody; worker_processes 2; error_log /usr/local/nginx/logs/nginx_error.log crit; pid /usr/local/nginx/logs/nginx.pid; worker_rlimit_nofile 51200; events { use epoll; worker_connections 6000; } http { include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 3526; server_names_hash_max_size 4096; log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]' ' $host "$request_uri" $status' ' "$http_referer" "$http_user_agent"'; sendfile on; tcp_nopush on; keepalive_timeout 30; client_header_timeout 3m; client_body_timeout 3m; send_timeout 3m; connection_pool_size 256; client_header_buffer_size 1k; large_client_header_buffers 8 4k; request_pool_size 4k; output_buffers 4 32k; postpone_output 1460; client_max_body_size 10m; client_body_buffer_size 256k; client_body_temp_path /usr/local/nginx/client_body_temp; proxy_temp_path /usr/local/nginx/proxy_temp; fastcgi_temp_path /usr/local/nginx/fastcgi_temp; fastcgi_intercept_errors on; tcp_nodelay on; gzip on; gzip_min_length 1k; gzip_buffers 4 8k; gzip_comp_level 5; gzip_http_version 1.1; gzip_types text/plain application/x-javascript text/css text/htm application/xml; server { listen 80; server_name localhost; index index.html index.htm index.php; root /usr/local/nginx/html; location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/tmp/php-fcgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; } } }
/usr/local/nginx/sbin/nginx -t /etc/init.d/nginx start netstat -lntp |grep 80
测试php解析
vi /usr/local/nginx/html/1.php //加入如下内容
<?php echo "test php scripts."; ?>
curl localhost/1.php
附:设置默认虚拟主机和多站点配置
增加vhost文件夹用于配置多站点
vim /usr/local/nginx/conf/nginx.conf
include vhost/*.conf
mkdir /usr/local/nginx/conf/vhost
cd !$; vim default.conf //加入如下内容
server
{
listen 80 default_server; // 有这个标记的就是默认虚拟主机
server_name aaa.com;
index index.html index.htm index.php;
root /data/wwwroot/default;
}
mkdir -p /data/wwwroot/default/
echo “This is a default site.”>/data/wwwroot/default/index.html
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
curl localhost
curl -x127.0.0.1:80 123.com
关于CentOS 7中怎么搭建LNMP环境就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。